Help, positive and negative number sum...

need help writing code that will add the number of positives and negat

Page 1 of 1

7 Replies - 8215 Views - Last Post: 20 June 2009 - 11:45 PM Rate Topic: -----

#1 xspockx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-March 09

Help, positive and negative number sum...

Posted 05 March 2009 - 04:47 PM

i need help with a part of my program that would allow me to sum up the amount of positives and negatives inputted into the program.
oh, btw, I know I'm missing the variables totalPos and totalNeg, there what im trying to find..

Here's what I have:

#include <iostream>
using namespace std;

int main()
{
	int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum_num;
	 
		
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num1;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num2;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num3;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num4;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num5;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num6;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num7;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num8;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num9;
	cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num10;
	
	sum_num = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
   
	cout << "You entered "<< totalPos <<" positive numbers"; 
	cout << "and " << totalNeg <<" negative numbers";
	cout <<"The sum of all numbers is " << sum_num;
	cout <<"\n";
	system("pause"); 
	return 0;
}
	


This post has been edited by xspockx: 05 March 2009 - 04:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help, positive and negative number sum...

#2 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:06 PM

totalPos and totalNeg will be counters. They should both be initialized to zero at the beginning of the program. Here's an example of how to use them:
cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num1;
if (num1 >= 0)
   totalPos++;
else
   totalNeg++;
...



Here's another way to do this program. You know you want to enter in 10 numbers, so make it a for loop.
#include <iostream>
using namespace std;
int main()
{
int num, sum_num = 0, totalPos = 0, totalNeg = 0;
for (int x = 0; x < 10; x++){
   cout << "Please Enter a Whole Number, positive or negative: ";
   cin >> num;
   sum_num += num;
   if (num >= 0)
	  totalPos++;
   else
	  totalNeg++;
   cout << "\n";
}
	cout << "You entered "<< totalPos <<" positive numbers";
	cout << "and " << totalNeg <<" negative numbers";
	cout <<"The sum of all numbers is " << sum_num;
	cout <<"\n";
	system("pause");
	return 0;
}


As you can see, it's much shorter :)

This post has been edited by OrganizedChaos: 05 March 2009 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:07 PM

Never mind.

This post has been edited by ericr2427: 05 March 2009 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 xspockx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-March 09

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:12 PM

Does that mean I have to add the if-else to each num?(ex num1, num2...to num10?)

View PostOrganizedChaos, on 5 Mar, 2009 - 04:06 PM, said:

totalPos and totalNeg will be counters. They should both be initialized to zero at the beginning of the program. Here's an example of how to use them:
cout << "Please Enter a Whole Number, positive or negative: ";
	cin >>num1;
if (num1 >= 0)
   totalPos++;
else
   totalNeg++;
...



Here's another way to do this program. You know you want to enter in 10 numbers, so make it a for loop.
#include <iostream>
using namespace std;
int main()
{
int num, sum_num = 0, totalPos = 0, totalNeg = 0;
for (int x = 0; x < 10; x++){
   cout << "Please Enter a Whole Number, positive or negative: ";
   cin >> num;
   sum_num += num;
   if (num >= 0)
	  totalPos++;
   else
	  totalNeg++;
   cout << "\n";
}
	cout << "You entered "<< totalPos <<" positive numbers";
	cout << "and " << totalNeg <<" negative numbers";
	cout <<"The sum of all numbers is " << sum_num;
	cout <<"\n";
	system("pause");
	return 0;
}


As you can see, it's much shorter :)

Was This Post Helpful? 0
  • +
  • -

#5 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:20 PM

No, because in the for loop, instead of using a separate variable for each number inputted, it uses the same one, and adds the number to sum_num and increments totalPos or totalNeg.
Was This Post Helpful? 0
  • +
  • -

#6 xspockx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-March 09

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:33 PM

I think i understand most of what was going on..but I've been trying to figure out what the operator "++" does or what it means? I see that its in the variables as well as the if-else's, am I right in thinking that it "adds"?
Was This Post Helpful? 0
  • +
  • -

#7 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:37 PM

Yes, ++ adds one to the variable. It can be used as ++variable, which increments the variable and then does the operation, or variable++, which does the operation and then increments the variable.
Was This Post Helpful? 1
  • +
  • -

#8 sayha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-June 09

Re: Help, positive and negative number sum...

Posted 20 June 2009 - 11:45 PM

Dose this work on Dev C++?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1