Thread: summation in c

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    summation in c

    Hi, I'm relatively new to c, and understand most of the basics, but I'm have a problem with while and for loops in trying to implement an arithmetic series.

    I thought something like:

    Code:
    float sum=0.0;
    for(x=0;x<=5;x++)
      {
      sum+=a+(b*x);
      return sum;
      }
    printf("sum of series = %f\n",sum)
    -where variables have been declared before hand - would produce the equivalent of the mathematical series:

    sum=a+(a+b)+(a+2b)+(a+3b)+(a+4b)+(a+5b)

    ..since everything I have read about for loops, says that the block of code between the curly parenthesis is executed repeatedly, for each value of x (startign at 0 and incrementing by one per iteration) , until the condition x<=5 is true.

    By having the variable 'sum' that is initially 0, incrementing this value by 'a+(b*x)' for each iteration, and using 'return sum;' at the end of each iteration, I would have thought that on each loop between x=0 and x=5, the value a+xb would be added to 'sum' and then this new value returned back to the program, becoming the new value of 'sum' for the next iteration...such that when the loop has finished, the above series is yielded.

    I have compiled this code sucessfully, and yet when I run it there is no output....i.e nothing gets printed.

    What is wrong with this? thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    return always immediately stops execution of the current function. Edit: which is to say, take it out and we'll all be happy.
    Last edited by tabstop; 10-20-2008 at 01:44 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by tabstop View Post
    return always immediately stops execution of the current function.
    Oh I see, I thought 'return (); was like to reset the value of sum acoording to previous changes to it (such as incrementation).

    So how can I acheive this? Is there a command to use in place of 'return' that will do the job I'm looking for, or does it have to be done some other way? thanks for the replies

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bertazoid View Post
    Oh I see, I thought 'return (); was like to reset the value of sum acoording to previous changes to it (such as incrementation).
    Since doing so would completely undermine the whole project, I doubt you want to reset the value of sum. What you want to do is "nothing", which involves restraining yourself from typing a line of code (and in this case, deleting a line of code that does "something" instead).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by tabstop View Post
    Since doing so would completely undermine the whole project, I doubt you want to reset the value of sum. What you want to do is "nothing", which involves restraining yourself from typing a line of code (and in this case, deleting a line of code that does "something" instead).
    Ah, Ok thanks. When I said 'reset' I didn't mean return to 0, I meant change variable sum from what it was preiviously to sum+=a+(b*x), and 'save' the variable sum as this new value, ready for the next iteration...and incrementation/addition....doesn't matter anyway, as I obviously didn't properly uderstand 'return' anyway .

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bertazoid View Post
    Ah, Ok thanks. When I said 'reset' I didn't mean return to 0, I meant change variable sum from what it was preiviously to sum+=a+(b*x), and 'save' the variable sum as this new value, ready for the next iteration...and incrementation/addition....doesn't matter anyway, as I obviously didn't properly uderstand 'return' anyway .
    += always always always always always always always always changes the left-hand side.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Summation = for loop.

    I really tried hard to get a nice looking ascii art thing going on to show you... but its no use. If only the forum supported MathML.... *ahem* Kermi... if only...

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you find you cannot live without an extra line of code:

    Example:
    Code:
    float sum=0.0;
    for(x=0;x<=5;x++)
    {
      sum+=a+(b*x);
      continue;
    
      /* You could even put whatever you want down here. It will be skipped :) */
    }
    printf("sum of series = &#37;f\n",sum)

  9. #9
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Of course, if what you want to print the series, you just have to replace that return with a call to printf.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by IceDane View Post
    Of course, if what you want to print the series, you just have to replace that return with a call to printf.
    Hmmm, do you mean to use 'printf("%f\n")' within the curly parenthesis, or without? I think I only need to use it at the end to display the answer, but i guess if i put it inside as well, it would return a list of the terms, before the answer, right?

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by bertazoid View Post
    Hmmm, do you mean to use 'printf("%f\n")' within the curly parenthesis, or without? I think I only need to use it at the end to display the answer, but i guess if i put it inside as well, it would return a list of the terms, before the answer, right?
    Look at my post, homeboy. tabstop merely corrected your assertion about how the += operator works.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by master5001 View Post
    Look at my post, homeboy. tabstop merely corrected your assertion about how the += operator works.
    Ahh ok homeslice :P...this forum moves faster than abullet I can't keep up!

    Anyway, so in your code, you have used it within and without the parentheses...what is the difference between this, and just using it without?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    float sum=0.0;
    for(x=0;x<=5;x++)
    {
      sum+=a+(b*x);
      printf("\r&#37;f", sum);
    }
    printf("\rsum of series = %f\n",sum)

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you an SQL programmer, bertazoid? That is the sort of behavior tables exhibit.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by master5001 View Post
    Are you an SQL programmer, bertazoid? That is the sort of behavior tables exhibit.
    Nope, to tell the truth I've only ever used scripting languages before for various bits of software, and have been programming for a mere month or so as it's part of my electronics degree....I will have to learn assembly and MATLAB later too, as well as a ........ loads of advanced maths like fourier/laplace transforms. :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  2. partially summation
    By joerg in forum C Programming
    Replies: 0
    Last Post: 11-23-2007, 10:47 AM
  3. Help With Simple Summation...
    By TOPFLOR in forum C Programming
    Replies: 19
    Last Post: 08-09-2006, 10:50 AM
  4. Summation of an array using pointers
    By therminator53 in forum C Programming
    Replies: 3
    Last Post: 11-30-2003, 01:23 AM