Here's the code XD
#include <iostream>
using namespace std;
int main()
{
int numbers[10];
int poscount=0, negcount=0, totalcount=0;
cout << "This program accepts 10 integer numbers, the returns the sum of all positive\n";
cout << "numbers, the sum of all negative numbers and the sum of all the numbers." << endl << endl;
for( int i=0; i<10; i++)
{
cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i;
cin >> numbers[i];
if( numbers[i] > 0 )
poscount += numbers[i];
else
negcount += numbers[i];
}
cout << endl;
cout << "Sum of positive numbers: " << poscount << endl;
cout << "Sum of negative numbers: " << negcount << endl;
cout << "Sum of all numbers: " << poscount + negcount << endl;
system("pause>nul");
return 0;
}
I wouldn't know where to start if I didn't know arrays and the logic of array indexes.
When you state
int number; you're asking for the computer to allocate enought memory for 1 integer number to be stored. But when you say
int numbers[10]; you're asking for enough space to be allocated for 10 integers, all ten with the tag
numbers but you can tell them apart by the index. You really have 10 variables but you can tell them apart by the index number. So you will have 1 integer in numbers[0] through numbers[9]. The max the index number goes for that array is 9, not 10. Then to print out
cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i; we're using the index number to show a different number, in this case how many integers have been input so far.
Any questions feel free to pm me. These forums are very active and messages can get lost very easily inside threads.