i am new with c++ programming. i am posing a problem from dietel C++ book. can any one give me a little bit explanation of the following problem.
Thank you in advance.
"Write a program that uses a for statement to sum a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value per input statement. A typical input sequence might be
5 100 200 300 400 500
where the 5 indicates that the subsequent 5 values are to be summed."
Put forth some effort and provide an attempt at solving the problem. It not difficult. People are pretty helpful here. Just ask that you make an attempt rather than being spoon fed the answer.
@iharrold
i don't think he is asking he's asking the source code. i believe he is trying to solve a problem at the end of the book chapter, he just wants to know what the problem mean.. i mean not everyone speaks english or understand it well like me..
The question wants you to read a number. This number is to be used in a for loop which will read that many numbers into memory and sum them. Then output the answer.
In the example, the first number you read is a 5, so you will be reading in 5 numbers (the 100, 200, 300, 400, 500) and summing them. The answer is 1500, so you would output that.
Perhaps your right. I read it differently as a request to write out the program.
@eboy
Here is pseudo code of what should happen:
Read in the first Value in the list.
Loop the number of times the first Value says (i.e. 5 = 5 loops)
{
Read in the next value in the list
Add the newly read value to a sum total
}
Print the sum total
main()
{
int nums,sum;
cout<<"enter a row of numbers, 1 at a time, first being the amount of numbers.";
cin>>nums;
int numbers[nums];
for (nums;nums>0;nums--)
{
cin>>numbers[nums];
}
for (blablablablablablabla)
{
sum+=numbers[lolwut]
}
cout<<"summ of values = "<<sum;
}
thats how i would do it... then just remove the 53 syntax errors and youre done...
You can't do that. Unless you're going to dynamically create the array the compiler has to know at compile time the size of the array: int *numbers = newint[nums];
I won't go into how you haven't defined main as int(or any type) or that you haven't returned anything, or that your program will go tits up when it reaches the second cin>> statement.