I'm trying to write a code that calculates something similar to a geometric sequence.
I thought i did it, but when i looked at the solution, it seemed like i was doing something completely different.
There is a little twist to it though, the X value is in range from Xmin to Xmax, which means xmin <= x <= xmax and has a step of Dx.
Here's my code:
#include <stdio.h>
#include <math.h>
void main(){
double xmin, xmax, dx, x, n = 1;
long s = 0;
printf("Input xmin, xmax, dx: ");
scanf("%lf%lf%lf", &xmin, &xmax, &dx);
printf("\n x s\n=============================");
for(x = xmin; x <= xmax; x = dx){
s = pow(x,n);
n ;
printf("\n.3lf.3ld", x, s);
}
}
Here is an example of what it outputs: (it goes from one to five, with a step of one)
Here's the actual solution, which i can't understand at all:
#include <stdio.h>
int main(){
int n,i;
double xmin, xmax, dx, x;
printf("Input n: ");
scanf("%d", &n);
printf("Input xmin, xmax, dx: ");
scanf("%lf%lf%lf", &xmin, &xmax, &dx);
printf("\n x s\n=============================");
for(x = xmin; x <= xmax; x = dx){
double s = 0, p = 1;
for(i = 1; i <= n; i )
s = (p *= x);
printf("\n.3f.3f", x, s);
}
return 0;
}
And here's what it outputs: (I put n as 3 randomly, because i can't understand what it does)
My question is, what did i do wrong in my code? And why is there another loop in the solution with two variables that aren't even used anywhere? It seems the output of my code isn't wrong, but i can't even understand the output of the solution.
CodePudding user response:
You have the main loop right, that being "for each value of x
, from xmin
to xmax
, using dx
as the step". However, you need to rethink how the algorithm works. Look back at the summation formula. Your answer is supposed to be calculated using the sum of x
to the power of i
, where i
is every integer from 1
to n
. So, when n = 3
, your output should be equivalent to x^1 x^2 x^3 for each value of x
.
1^1 1^2 1^3 = 1
2^1 2^2 2^3 = 14
3^1 3^2 3^3 = 39
4^1 4^2 4^3 = 84
5^1 5^2 5^3 = 155
Your first loop is to repeat the formula for every value of x
, but you need a nested loop because the formula you're using is a summation (i = 1 to n of x^i). You're also not supposed to carry the result of the last equation over to your new one.
Let me explain this: s = (p *= x);
a little better. s
(the sum) is initialized at 0 and p
is initialized at 1. p
is multiplied by x
each loop, and added to s
afterwards. The first time through the loop, pow(p, 1)
is added to the sum. The second time, pow(p, 2)
is added. The third time, x = pow(p, 3)
is added. So, if n = 4
and x = 2
, that would be s = pow(2, 1) pow(2, 2) pow(2, 3) pow(2, 4)
, or s = 2 4 8 16
, or s = 30
. This is equivalent to the sum of i from 1 to n of x^i.
I hope this helped, and if you want any further explanation, I'd be happy to give it.
CodePudding user response:
In your for loop
for(x = xmin; x <= xmax; x = dx){
s = pow(x,n);
n ;
printf("\n.3lf.3ld", x, s);
}
the value of n
is incremented in each iteration of the loop while in the second program
for(x = xmin; x <= xmax; x = dx){
double s = 0, p = 1;
for(i = 1; i <= n; i )
s = (p *= x);
printf("\n.3f.3f", x, s);
}
it is fixed for the given value of x
.
Another problem is that you do not reset the variable s in each iteration of the loop for a given value of x
.
s = pow(x,n);
Instead you need to write at least like
s = pow(x,n);
Though that will be in any case incorrect due to incrementing n
.
Actually the second program calculates the formula for each x
separately in the range [xmin, xmax]
while your code calculates the formula while your program calculates the formula as whole for different values of x.