Pow inside a for loop, not showing value - c

So I wrote this code to find the relative error between an estimate of PI and a fixed theoretical value (3.14159). But whenever I run the program, I get this in the command line: "After 5 terms pi is approximately -1.#INF00", and the same for the relative error value. I'm not sure what's wrong
int main(void)
{
float i, n, error;
float term = 0;
printf("Enter the number of terms: ");
scanf("%f", &n);
for(i=0; i<=n; i++)
{
term=term + pow(-1.0, i+1)/(i*i);
}
error=fabs(term-3.141593)/(3.141593);
printf("The actual value of pi is %f\n", 3.141593);
printf("After %0.0f terms pi is apporximately %f\n", n, term);
printf("The relative error is %f %", error);
return 0;
}

Related

My code which is calculating sum and average of two numbers returns this error "[Error] ld returned 1 exit status" upon execution and compilation

This is my code. I've read the output log but can't see where the exact error is; I will appreciate if someone can kindly point me to it. The IDE I'm using is Dev C++.
int main(){
int n, i, a, average, sum;
printf("enter the number of integers to be picked\n");
scanf("%d", n);
sum=0;
printf("Enter a number.");
scanf("%d", &a);
for (i=0; i<=n;i++){
printf("%d", i);
sum=sum + a;
}
average=sum/n;
printf("The average is %d", average);
return 0;
}
You're just missing the & (address-of operator) in line 4 (scanf("%d",&n);). Putting this operator will tell the compiler, the address of n, where the value of n will be kept.

Why is one for-loop working as wished but the other not iterating?

Here two somehow similar problems (1.-Sum of uneven numbers until n; 2.-Sum of the series 1/n) to be solved using for-loops.
I chose the shown strategy and while for the 1st problem my loop works as expected, for the 2nd it does not iterate.
I can't see the difference to understand what is wrong in the last case.
----1----- :
int main()
{
// Example: Input=5, Output=1+3+5=9
int i, lastNum ,sumUneven;
printf("Until what n should the uneven numbers be added: ");
scanf("%d", &lastNum);
sumUneven=0;
for (i=1; 2*i-1<=lastNum; i++) {
sumUneven=sumUneven + (2*i-1);
}
printf("Sum of uneven numbers until %d: %d", lastNum, sumUneven);
}
----2------------:
int main()
{
//My goal: Input=n; Output= 1+ 1/2 + 1/3....+1/n.
int i, n, sum;
printf("Until what n should this series be calculated: ");
scanf("%d", &n);
sum=0;
for (i=1; i<=n; i++) {
sum = sum + 1/i;
}
printf("Sum of the series until 1/%d: %d", n, sum);
}
The expression 1/i performs integer division (truncating any fractional part) since both operands have type int. So if i is greater than 1 the division will result in 0. The variable sum is also of type int so it can't hold fractional numbers.
Perform the division as 1.0/i, which is floating point division since one argument has type double, and change the type of sum to double. Also use %f to print sum.
int main()
{
//My goal: Input=n; Output= 1+ 1/2 + 1/3....+1/n.
int i, n;
double sum;
printf("Until what n should this series be calculated: ");
scanf("%d", &n);
sum=0;
for (i=1; i<=n; i++)
{
sum = sum + 1.0/i;
}
printf("Sum of the series until 1/%d: %f", n, sum);
}
you're using an int to keep track of floats which will not work.
1/n will be a float a simple fix would be
int main()
{
//My goal: Input=n; Output= 1+ 1/2 + 1/3....+1/n.
int i, n;
float sum;
printf("Until what n should this series be calculated: ");
scanf("%d", &n);
sum=0;
for (i=1; i<=n; i++) {
sum = sum + 1/i;
}
printf("Sum of the series until 1/%d: %f", n, sum);
}
you could also use the double declaration
hope this was helpful

Average of Numbers in a Loop

In my C Program, I want to get the average of the sum of numbers being entered until the program stops. What should I add to check the average? Thank you!
#include <stdio.h>
int main (void)
{
int x;
int sum = 0;
int average;
int testEOF;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%d", &x);
if (testEOF !=EOF)
sum +=x;
} while (testEOF !=EOF);
printf ("\nTotal: %d\n", sum);
printf ("\nAverage: %d\n", average);
return 0;
//main
}
As other people explained, you have to initialize sum and count, they are never given their initial values. No need to initialize average and x because you assign sum/count to average and users will assign any value to x.
You have to put everything you want your if statement to do in {...}. But you didn't. Your if statement only does sum +=x and does not work for count++ and average = sum / count. So your program increases the value of count even after EOF. So you found 14/6 rather than 14/5.
You checked testEOF != EOF twice. One is in the if statement, other is in do-while.
I put the code below:
#include <stdio.h>
int main (void){
float x;
float sum = 0;
float count = 0;
float average;
float testEOF;
printf("Enter your numbers: <EOF> to stop.\n");
while(1){
testEOF = scanf("%f", &x);
if (testEOF ==EOF){
break;
}
sum +=x;
count++;
average = sum / count;
}
printf ("\nTotal: %f\n", sum);
printf ("\nAverage: %.2f\n", average);
return 0;
}
#include <stdio.h>
int main (void)
{
float x;
float sum;
float count;
float average;
float testEOF;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%f", &x);
if (testEOF !=EOF)
sum +=x;
count++;
average = sum / count;
} while (testEOF !=EOF);
printf ("\nTotal: %f\n", sum);
printf ("\nAverage: %.2f\n", average);
return 0;
}
This is now almost correct. The only thing is that the number is not getting divided by the number of values entered.
Example:
2
3
3
3
3
Sum is 14
Average: 2.3 (should be 2.8)
This seems that count is being added by 1 every time I get the average.
Well if you want your output to be printed with decimal point you can do the following , in that case
for inputs like 0, 1, 1 the out would be 0.666667 otherwise it will be simply 0 ignoring the decimal part.
the while part can be optimized as suggested by #David C. Rankin
double sum = 0;
double average = 0;
unsigned int count = 0;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%d", &x);
if (testEOF != EOF)
{
sum += x;
count++;
}
} while (testEOF != EOF);
printf ("\nTotal: %f\n", sum);
average = sum / count;
printf ("\nAverage: %f\n", average);

Average arrays type double

#include <stdio.h>
int main()
{
int n, i;
double num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter number %d: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %f", average);
return 0;
}
Hi, I have a problem with displaying the average, displaying numbers from space, how to fix it?
Correct format specifier for double would be (you will have to use this)
scanf("%lf", &num[i]);
For printf both %f or %lf will do. Note that when using scanf check the return value of scanf - in case of failure you will take necessary action. (Wrong input given or some other error).
Using the correct format specifier for double as stated by #coderredoc resolves it. Use %lf instead of %f.

Function not returning value to other function (C)

I'm writing this program for class in which I have to compute the equivalent resistance in series.
The description of the Homework is:
"The present homework is concerned with the use of pointers in computing the equivalent resistance to resistors connected in series. The number of resistors N connected in series and their resistance values R[0], R[1], …, R[N-1] are user-supplied...the inputting of the number of resistors N and the one-dimensional array elements R[0], R[1], …, R[N-1] is done within the “input” prototype function...these values in turn are passed to the “series” prototype function, where the computation of the equivalent resistance Req is performed... the input and output values are outputted to the console from within the “input” function.
-The prototype functions “input” and “series” are to be both placed before the “main” function, with the prototype function “series” placed between the “input” and “main” functions."
Code:
#include <stdio.h>
#define x 100
#define y 10000
float series(int N, float R[]);
void input() {
printf("\n---------------Compute equivalent resistance in series!---------------\n");
int N;
float R[y];
printf("\nPlease enter amount of resistors: \n");
scanf_s("%d", &N);
for (int i = 1; i <= N; i++) {
printf("\nEnter resistance for resistor %d: \n", i);
scanf_s("%f", &R[i]);
}
series(N, R);
printf("\n");
for (int i = 1; i <= N; i++) {
printf("The resistance of R[%d] is: %.2f.\n", i, R[i]);
}
printf("\nThe equivalent resistance is: %.2f Ohms.", Req);
}
float series(int N, float R[]) {
float Req = 0;
for (int i = 1; i <= N; i++) {
Req += R[i];
}
return Req;
}
int main() {
input();
getchar();
return 0;
}
My problem is that Req isn't being returned to the 'input' function in order to output the Equivalent resistance. Please help. Thank You
You're never assigning the result of series to a variable.
float req_ret;
req_ret = series (N, R);

Resources