Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having trouble using both the functions listed below in my c program main because of the implicit declaration error to the twos function. Could someone please advise.
Thanks!
#include <stdio.h>
#include <stdlib.h>
float twoxaverage(int n, float t__scores[]);
float getaverage(int n, float t_scores[])
{ int sum=0; float average=0; int i;
for (i=0; i<n; i++)
{
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
float twoxaverage(int n, float t__scores[])
{ float mult;
mult=2*(getaverage( n, t__scores));
return (mult);
}
int main()
{
int t_score[]={1,2,3,4};
float twox;
twox=twoaverage4,t_score);
float twoxaverage(int n, float t__scores[]);
return 0;
}
Your main should move from:
int main(){
int t_score[] = { 1, 2, 3, 4 }; float twox;
twox = twoaverage4, t_score); float twoxaverage(int n, float t__scores[]);
return 0;
}
TO:
int main(){
int t_score[] = { 1, 2, 3, 4 };
float twox;
twox = twoxaverage(4, t_score);
return 0;
}
as a start.
I might also simplify your:
float getaverage(int n, float t_scores[]){
int sum=0; float average=0; int i;
for (i=0; i<n; i++){
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
TO:
float getaverage(int n, float t_scores[]){
float average=0; int i;
for (i=0; i<n; i++){
average = average + t_scores[i];
}
return(average / (float)n);
}
This uses one less variable and performs the same math.
In this line you have at least two typos in one identifier
twox=twoaverage4,t_score);float twoxaverage(int n, float t__scores[]);
Instead of
twox=twoaverage4,t_score);
there shall be
twox = twoxaverage(4,t_score);
Also the declaration of the function that follows is unnecessary. So instead of
twox=twoaverage4,t_score);float twoxaverage(int n, float t__scores[]);
you can write simply
twox = twoxaverage(4,t_score);
I think you have not only to get the average but also to output it. So you could at least add statement
std::cout << twox << std::endl;
Or if it is a C program then you could use printf instead of C++ operator << to output twox
Also take into account that your function getaverage is wrong. You convert float numbers to int when add them to sum
float getaverage(int n, float t_scores[])
{ int sum=0; float average=0; int i;
for (i=0; i<n; i++)
{
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
The valid function could look the following way
float getaverage( int n, const float t_scores[] )
{
float sum = 0.0f;
for ( int i = 0; i < n; i++ )
{
sum += t_scores[i];
}
return ( n == 0 ? 0.0f : sum / n );
}
Or if you use an old C compiler then the function can be written as
float getaverage( int n, const float t_scores[] )
{
float sum = 0.0f;
int i = 0;
for ( ; i < n; i++ )
{
sum += t_scores[i];
}
return ( n == 0 ? 0.0f : sum / n );
}
Function twoxaverage could be simplified the following way
inline float twoxaverage( int n, const float t__scores[] )
{
return ( 2 * getaverage( n, t__scores ) );
}
Related
Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}
To summarize my problem,
i want an array to return two array pointers. One of these pointer arrays must be float or double, this will give me the quotient operation. Another should be int, and that should give the remainder of the division.
For examle if i have two arrays as: int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4}; when i want to reach my pointers result should be like: Quotient is: {1.5,2,3,3,4,4.5} Remainder is: {1,0,0,0,0,2}
Here is my codes:
#include<stdio.h>
void div(int a[], int b[], float *quotient, int *remainder) {
float quo[6];
int remain[6];
for(int i = 0; i< 6 ; i++)
{
quo[i] = a[i] / (double)b[i];
remain[i] = a[i] % b[i];
*remainder = remain[i];
*quotient = quo[i];
*remainder++;
*quotient++;
}
// quotient = quo;
// remainder = remain;
}
int main() {
int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4};
float q;
int r;
div(a, b, &q, &r);
for(int i = 0; i< 6 ; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", q, r);
}
// printf("Quotient is: %.1f\nRemainder is: %d\n", *q, *r);
return 0;
}
You need to pass an array for the quotient and remainder into the divide function.
Than you can read the values after the function returns.
#include<stdio.h>
void div(int* a, int* b, float* quotient, int* remainder, int count) {
for (int i = 0; i < count; i++)
{
quotient[i] = a[i] / (double)b[i];
remainder[i] = a[i] % b[i];
}
}
int main() {
int a[] = { 3,6,9,12,16,18 }, b[] = { 2,3,3,4,4,4 };
#define LENGTH (sizeof(a) / sizeof(int))
float quotient[LENGTH];
int remainder[LENGTH];
div(a, b, quotient, remainder, LENGTH);
for (int i = 0; i < LENGTH; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", quotient[i], remainder[i]);
}
return 0;
}
long double power1(double x, int n) {
long double power = 1;
int i;
for(i=1; i<=n; i++) {
power *= i;
}
return power;
}
long long int factorial(long long n) {
long long int pr = 1;
long long int i;
for(i=1; i<=n; i++){
pr *= i;
}
return pr;
}
double myExp(double x, double epsi) {
double sum=(double)1;
int i;
while(power1(x, i)/((1.0)*factorial(i)) - epsi <= 0) {
sum += power1(x, i)/(1.0*factorial(i));
i++;
}
return sum;
}
int main(int argc, char *argv[]) {
system("cls");
double x, epsi;
int n;
x=1.5;
epsi=0.00001;
n=1000;
printf("exp(%.lf,%f)=%f\n", x, epsi, myExp(x, epsi));
printf("\n");
}
I need help to fix my exponential function e^x given the epsi value and x value such that abs(x^n/n!) <= epsi, where n is the first integer satisfying the condition. It seems the while loop doesn't work. When I enter 1.5, the desired output is 4.481689, but the result is 1.0000.
I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", °);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}