1. ## C homework

You're given the series :

S = x + ( x+x^2) + (x + x^2 + x^3) + (x + x^2 + x^3 + x^4) . . .

Write a C program , which do the following :

If a gave you any value for x and n , which is the power the sum will end , say I gave you x = 2 , and x=3 , then the sum will be

(2) + (2+4) + (2 + 4 +8 ) = 22

Thanks for any help.

2. Just have a look at the series, you can see one very interesting relationship between powers in each "part":
S = (x) + (x + x^2) + (x + x^2 + x^3) + (x + x^2 + x^3 + x^4) . . .
If you are able to substitute "?" in the following equation and port it back to your problem, it will become even more obvious:
x = x^?
Then just reproduce it in the code, one of the possible approaches could be using two for loops (if the input is user defined value of "x" and "n").

Posting here more would be most probably equal to doing the homework for you, which would fix the problem with homework done (good!), but in the long term it would only do you harm to provide complete solution (very bad!).

I wish you good luck, every problem solved on your own will make you stronger. Every homework somebody else does for you will make you less sure during your decisions in future. And that is not something you would like to experience, am I right?

Petr

3. I think you need to look at your C books for the syntax and also if this is homework you should be asking your teacher if you cant do it not someone to do the work for you.

but here it is in TB.

Dim x As Long = 2
Dim sMsg As String

'S = x + ( x+x^2) + (x + x^2 + x^3) + (x + x^2 + x^3 + x^4) . . .

sMsg  = "Using C (XOR)"+\$CRLF(2)
sMsg += "x"+\$TAB(7)+" = "+Str\$(x) +\$CRLF
sMsg += "( x+x^2)"+\$TAB(7)+" = "+Str\$(x+(x XOR 2)) +\$CRLF
sMsg += "(x + x^2 + x^3)"+\$TAB(6)+" = "+Str\$(x+(x XOR 2)+(x XOR 3)) +\$CRLF
sMsg += "(x + x^2 + x^3 + x^4)"+\$TAB(5)+" = "+Str\$(x+(x XOR 2)+(x XOR 3)+(x XOR 4)) +\$CRLF
sMsg += "x + ( x+x^2) + (x + x^2 + x^3) + (x + x^2 + x^3 + x^4)"+\$TAB+" = "+Str\$(x + ( x+(x XOR 2)) + (x+(x XOR 2)+(x XOR 3)) + (x+(x XOR 2)+(x XOR 3)+(x XOR 4)))
sMsg += \$CRLF(2)
sMsg += "Using TB (powers)"+\$CRLF(2)
sMsg += "x"+\$TAB(7)+" = "+Str\$(x) +\$CRLF
sMsg += "( x+x^2)"+\$TAB(7)+" = "+Str\$(x+(x^2)) +\$CRLF
sMsg += "(x + x^2 + x^3)"+\$TAB(6)+" = "+Str\$(x+(x^2)+(x^3)) +\$CRLF
sMsg += "(x + x^2 + x^3 + x^4)"+\$TAB(5)+" = "+Str\$(x+(x^2)+(x^3)+(x^4)) +\$CRLF
sMsg += "x + ( x+x^2) + (x + x^2 + x^3) + (x + x^2 + x^3 + x^4)"+\$TAB+" = "+Str\$(x + ( x+(x^2)) + (x+(x^2)+(x^3)) + (x+(x^2)+(x^3)+(x^4)))

MsgBox 0, sMsg

4. I agree, it's better to do it yourself.

But, sometimes everyone needs help.

Here it is in C.

It's done twice, both ways should give the same answer.

Dan
// In C99, a single line comment, begins with "//".

// You need "stdio.h" for the function "printf", in the function "main".
#include <stdio.h>

// You need "math.h" for the function "pow", in the function "seriessum1".
#include <math.h>

// You need to list the two function prototypes.
double seriessum1(double x, int n);
double seriessum2(double x, int n);

int main(int argc, char *argv[])
{
double ss;

// Set the values of "x" and "n", here.
// "xm" means, "'x' in function 'main'".
// "nm" means, "'n' in function 'main'".
double xm = 3;
int    nm = 8;

ss = seriessum1(xm, nm);
printf("series sum = %20.10f\n", ss);
ss = seriessum2(xm, nm);
printf("series sum = %20.10f\n", ss);
return 0;
}

double seriessum1(double x, int n)
{
int i, j;
double sum;
if(n < 1) return 0;
sum = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= i; j++) sum += pow(x,(double)j);
return sum;
}

double seriessum2(double x, int n)
{
int i, j, k;
// "tsum" means, "temporary sum".
double sum, tsum;
if(n < 1) return 0;
sum = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= i; j++)
{
tsum = 1;
for(k = 1; k <= j; k++) tsum *= x;
sum += tsum;
}
return sum;
}

5. What is this kind of Math used for guys?

6. For instance, sin(x), is an infinite series. You just plug the value of x into the series.

sin(x) = x - x^3/3! + x^5/5! - ..

The series goes on forever.

In the general case, when you truncate the series, you only get an approximation of sin(x).

("!" means, "factorial".

1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
Etc.)

7. I woke up at 3 in the morning and decided to get on the computer until sleep takes me again. Now it is 4 in the morning and I just read that innocent nice and easy on the eyes sin(x) is equal to a beast that is x - x^3/3! + x^5/5! - .. with factorials thrown in for extra scariness.

I will have nightmares now, but at least know that you guys are here to help fight the math monsters when they rear their heads.

Thanks for the explanation Dan.

8. Originally Posted by danbaron
For instance, sin(x), is an infinite series. You just plug the value of x into the series.

sin(x) = x - x^3/3! + x^5/5! - ..

The series goes on forever.

In the general case, when you truncate the series, you only get an approximation of sin(x).

("!" means, "factorial".

1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
Etc.)
Dan's example is wonderful but doesn't really answer the Why? question. Why study such series, or show that innocent expressions like sin(x) can be written as a series? I think there are two answers. The pure mathematician just finds such facts about mathematical entities beuatiful and fascinating. A second reason is that calculus often depends on being able to express mathematical entities as series - especially series that have a limit. The notion of a limit is fundamental to calculus. My guess is that it is the second reason that dominates in most educational programs...

Lance

9. Thanks for the help.

But to be honest, I'm a first year engineering student, and the professor gave us this extra question and it's +4 marks graded.

After viewing the answers, I noticed that we're under the chapter of While , if and for funtions.So I'm confused at the moment.

Well, I appreciate those advice given, but as danbaron said " sometimes we need help "

Anyway, thanks again.

The next time I'll post , I'll show some work.

Thanks Petr, you're 100 percent correct.

Page 1 of 2 12 Last

There are no members to list at the moment.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•