summation program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

summation program

 
0
  #1
Oct 4th, 2005
Hi everyone,
I am writing a summation program for a programming class I am taking. The assignment's main purpose is to give me practice writing functions. I have gotten the functions down just fine, but I can't seem to figure out how to write the inside of the summation function. The program asks for two long long ints; the start (which must be >= 1000), and then the howMany (which must be >250). then it passes these values to the summation function. According to the assignment sheet, the summation function simply adds all of the numbers from the starting value (start) up until the required number of values have been added together. Maybe this is just worded wrong for me to understand. Can anyone help me? I have posted the code I have done so far below. Thank you in advance.
Nick

  1. #include <iostream>
  2. using namespace std;
  3. long long int summation(long long int start, long long int howMany);
  4.  
  5. int main ()
  6. {
  7. long long int s = 0;
  8. long long int h = 0;
  9.  
  10. while (s < 1000)
  11. {
  12. cout << "Please enter the starting value: ";
  13. cin >> s;
  14. }
  15. while (h <= 250)
  16. {
  17. cout << "Please enter how many numbers to sum: ";
  18. cin >> h;
  19. }
  20.  
  21. cout << "The summation of the numbers from " << s << " to " << h + s - 1
  22. << " is " << summation(s,h) << endl;
  23.  
  24.  
  25. return 0;
  26. }
  27.  
  28. /*****************
  29.  * Function Name: summation
  30.  * Paremeters: long long int start, long long int howMany
  31.  * Return Value: long long int-sum
  32.  * Description: this adds the numbers from the starting value up until
  33.  * the required number of values have been added together.
  34.  *****************/
  35.  
  36. long long int summation(long long int start, long long int howMany)
  37. {
  38. // SHOULD DO THE SUMMATION STUFF RIGHT HERE.
  39.  
  40. return sum;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: summation program

 
0
  #2
Oct 4th, 2005
something like
  1. long long int summation(long long int start, long long int howMany)
  2. {
  3. long long sum=0;
  4. for(long long i=0,j=start;i<howMany;++i,++j)
  5. sum += j;
  6. return sum;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: summation program

 
0
  #3
Oct 4th, 2005
Thank you. That worked perfectly. I did not think to do it like that. Thank you very much!
Nick
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC