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

#include <iostream>
using namespace std;
long long int summation(long long int start, long long int howMany);

int main ()
{
   long long int s = 0;
   long long int h = 0;

   while (s < 1000)
   {
       cout << "Please enter the starting value: ";
       cin >> s;
   }
   while (h <= 250)
   {
       cout << "Please enter how many numbers to sum: ";
       cin >> h;
   }

   cout << "The summation of the numbers from " << s << " to " << h + s - 1
       << " is " << summation(s,h) << endl;


   return 0;
}

/*****************
 * Function Name: summation
 * Paremeters: long long int start, long long int howMany
 * Return Value: long long int-sum
 * Description: this adds the numbers from the starting value up until
 *              the required number of values have been added together.
 *****************/

long long int summation(long long int start, long long int howMany)
{
   // SHOULD DO THE SUMMATION STUFF RIGHT HERE.

   return sum;
}

Recommended Answers

All 2 Replies

something like

long long int summation(long long int start, long long int howMany)
{
   long long sum=0;
   for(long long i=0,j=start;i<howMany;++i,++j)
      sum += j;
   return sum;
}

Thank you. That worked perfectly. I did not think to do it like that. Thank you very much!
Nick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.