944,135 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8911
  • C++ RSS
Oct 4th, 2005
0

summation program

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Oct 4th, 2005
0

Re: summation program

something like
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 4th, 2005
0

Re: summation program

Thank you. That worked perfectly. I did not think to do it like that. Thank you very much!
Nick
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Noobie, can someone help?
Next Thread in C++ Forum Timeline: number format exceptions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC