Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C homework

  1. #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. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Last edited by Petr Schreiber; 06-12-2010 at 00:19.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159
    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
    
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  4. #4
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    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;
    }
    
    Last edited by danbaron; 06-12-2010 at 07:21.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    What is this kind of Math used for guys?
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  6. #6
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    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.)
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    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.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  8. #8
    Quote Originally Posted by danbaron View Post
    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. #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.

  10. #10
    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.
    Last edited by Aladdin; 06-12-2010 at 21:22.

Page 1 of 2 12 LastLast

Members who have read this thread: 0

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
  •