Hi all,

Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:

#include <iostream>
using namespace std;

int main()
{

  int one, two, three, four, five, six, seven, eight, nine, ten;
  int count;

  cout << "This program accepts 10 integer numbers, the returns\n";
  cout << "the sum of all positive numbers, the sum of all negative\n";
  cout << "numbers and the sum of all the numbers." << endl;

  cout << "Enter 10 whole numbers, each followed by 'Enter': \n ";
  cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten;
  cout << endl;

  cout << "The sum of all 10 numbers = ";
  cout << one + two + three + four + five + six + seven + eight + nine + ten;
  cout << endl;




  return 0;

}

Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.

Any help would be great. I'm not looking for the code, just someone to point me in the right direction.

Many thanks.

Its better if you have an array of 10 ints and then put down a for loop to check whether its positive or negative and add them up.

If you have variables like this., IT WILL TAKE A LONG TIME TO WRITE IF LOOPS. AND THEN ADD.

to check if its negative or positive.

you can use.

if(var<0)
{
cout<<"negative";
}
else
{
cout<<"POSITIVE";
}

Hi all,

Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:

#include <iostream>
using namespace std;

int main()
{

  int one, two, three, four, five, six, seven, eight, nine, ten;
  int count;

  cout << "This program accepts 10 integer numbers, the returns\n";
  cout << "the sum of all positive numbers, the sum of all negative\n";
  cout << "numbers and the sum of all the numbers." << endl;

  cout << "Enter 10 whole numbers, each followed by 'Enter': \n ";
  cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten;
  cout << endl;

  cout << "The sum of all 10 numbers = ";
  cout << one + two + three + four + five + six + seven + eight + nine + ten;
  cout << endl;




  return 0;

}

Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.

Any help would be great. I'm not looking for the code, just someone to point me in the right direction.

Many thanks.

To make your program much more efficient I would recommend learning about iterative loops (using 'for' statements) and arrays.

http://www.hitmill.com/programming/cpp/forLoop.htm
http://www.cplusplus.com/doc/tutorial/arrays.html

It would be good if you learned about looping as tomhogans mentioned, also try and learn arrays, it would make the program easier to read as you wont have so many variables.

To get you started I suggest you try and write it out on paper its not too difficult, in order to calculate the totals of the positive or negative try and divide the number by 2 and if it returns a 0 then its positive else its negative.

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.

sir how bout of we count the number of inputted positive integers, negative and zero and not its sum..

can you revise it for me boss... thanks alot

sorry for flooding... i figured out how to count the positive numbers i just replaced the "numbers" to "1" ... :D

thanks for the codes anyway.. big help for the completion of my project..

This article has been dead for over six months. Start a new discussion instead.