sum

Ok I know one way of using the formula n*(n+1)/2) and have used it to calculate the sum already but I just wanted to know how I can access many numbers in a loop and add them together. Here is what I did so far.

Code:

#include <iostream>
using namespace std;
int main (){
int num, num1, sum;
cout<<"Please enter a number\n";
cin>>num;
cout <<"Please enter the number to which you want to count\n";
cin>>num1;
for (; num<=num1; num++)
cout<<num<< ","; //I do get the numbers but from here
//I want to know what I can do so that I can add each
// of these numbers together?
do (sum=num+num);//I want to add them like 0+1+2+...
//tried using loops but couldn't get it
while (num<=sum);
cout<<sum;//but this does not work so any help is appreciated
return 0;
}

I'm a little unsure on what you are trying to do. Do you want the user to enter an initial number, then have the user enter that many numbers and add them together. So if the user enters "3", the program will prompt the user to enter 3 numbers which get added together?

The user enters a number first. Then s/he enters a second number (that should be greater than the first one) and so the program counts from the number entered first to the second number. So the user enters for example enters 3 first, and then 10 and the program will count to 10 i.e. it will display 3,4,5,6,7,8,9,10. Now what I want is to get the sum, average and standard deviation of these numbers such that the program sums up 3+4+5+6+7+8+9+10 to get the sum and then divides by8 to get the average and calculate for the deviation and so on.

The other things I don't understand is why is it that it looks like there is an extra number when calculating average? For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?

For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?

Sounds like integer division, so if you are required to use integers only, 4 would be correct.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).

Here is my code.

Code:

#include <iostream>
using namespace std;
int main (){
int num, num1, average;
cout<<"Please enter a number\n";
cin>>num;
cout <<"Please enter the number to whcih you want to count\n";
cin>>num1;
int sum = 0;
for (; num<=num1; num++)
{
cout << num << endl;
sum = sum +num; // this is fine and all is OK
}
average=(sum/(num+1));//but here is the problem. Try with 1 and 12 and you get
// the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
cout<<sum<<average;
return 0;
}

#include <iostream>
using namespace std;
int main (){
int num, num1;
float average;
cout<<"Please enter a number\n";
cin>>num;
cout <<"Please enter the number to whcih you want to count\n";
cin>>num1;
int sum = 0;
for (; num<=num1; num++)
{
cout << num << endl;
sum = sum +num; // this is fine and all is OK
}
average=(static_cast<float>(sum)/(num+1));//but here is the problem. Try with 1 and 12 and you get
// the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
cout<<sum<<average;
return 0;
}