View Full Version : 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.

Petr Schreiber

06-12-2010, 00:12

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

Michael Clease

06-12-2010, 00:15

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

danbaron

06-12-2010, 01:26

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.

:p

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;

}

What is this kind of Math used for guys?

danbaron

06-12-2010, 02:20

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.)

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.

LanceGary

06-12-2010, 11:54

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

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.

Dan, I re-read your reply, you're awesome !!

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

Thanks Petr, you're 100 percent correct.

danbaron

07-12-2010, 07:50

"e", is Euler's number. It can be defined so that the area under the curve, y = 1/x and above the x axis, and between the points x = 1 and x = e, equals 1. It is irrational - it's decimal expansion goes forever without repeating. It starts,

2.71828..

"pi", can be defined as the area of a circle with radius 1. It is irrational - it's decimal expansion goes forever without repeating. It starts,

3.14592..

"i", is defined as the square root of -1.

Here is maybe the most famous equation in mathematics, called, Euler's Identity.

e ^ (i * pi) + 1 = 0

(It is easy to prove, but it seems that no one understands what it means - why these three absolutely pivotal, but seemingly unrelated mathematical values, should be related in such a simple way. What does e have to do with pi?, - remember that both contain an infinity of digits, and neither has a repeating pattern in its infinity of digits.)

http://en.wikipedia.org/wiki/Euler%27s_identity

:p

Note.

A rational number can be written as the ratio of two integers, a/b, where b is unequal to 0 (that's the definition).

At some point, the decimal expansion of a rational number always begins to repeat, and then does so infinitely.

For instance, 2 is rational. Its decimal expansion is,

2.000000000000.. i.e, 0 repeats forever.

Another rational number is,

453678123.56471234634524558326489706849256792567925679256792567..

(I am unable to remove the space character between the 2 and 5 (above), I don't know why.)

If you notice, after awhile, the sequence 92567 begins to repeat. And, it does so forever.

In the decimal expansion of every rational number, sooner or later, there is a pattern of one or more digits which repeats over and over infinitely. As soon as you get to the first instance of repetition, you basically know the entire number.

On the other hand, an irrational number cannot be written as the ratio of two integers. And, in its decimal expansion, there is never a repeating pattern.

So, in a sense, no one knows what the value of e is. For instance, even if you calculate it to a trillion decimal places, you still won't know what the trillion and first digit is. The same is true for pi.

Very cool, thanks Dan. Your explanation of Euler's Identity is better than on wikipedia.

I think most of the math references there are for mathematicians and not good for the general public.

danbaron

07-12-2010, 09:50

I know you like math, Kent. In a certain way it is similar to space and the universe, big, mysterious, and surprising. And, by the way, while the universe is big, it is still finite. However, in 1931, Kurt Godel proved that math is infinite. There will never be a time when someone can (truthfully) say, "All of mathematics is now known.".

Einstein went with Godel, when Godel became a U.S. citizen. Einstein had to help shut him up, when he started spouting (what many people would consider to be) cuckoo talk. You can read about it here.

http://en.wikipedia.org/wiki/Kurt_Godel