Help to get out of this problem in C++.

Pages: 12
Problem is:-
Write a program that reads in ten whole number and then outputs the sum of only all positive numbers, sun of only all negative or zero numbers and also sun of all ten numbers including positive and negative. The user enters the ten number once in any order. Your program should not ask the user to enter the positive numbers and negative numbers separately.
Please tell me how to solve it?

I already saw the following post but I can't understand it.
http://www.cplusplus.com/forum/beginner/92085/
What's the first line in that you don't understand?
#Moschops I am totally hacked on this problem. Oky lets start first I will created ten variables like:-

int a, b, c, d, e, f, g, h, i, j;

// Now I will ask the user to enter ten whole numbers.

cout << "Enter ten whole numbers and press return key after entering each number\n";
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
cin >> f;
cin >> g;
cin >> h;
cin >> i;
cin >> j;

Now what to do next???

do
{
if (a > 0)
??????
Last edited on
That would be the long way of doing it, but it works. Create a variable for the sum of positive numbers, negative numbers, and whatever sum you need. Make if statements, to check what it is, depending if it's a positive or negative or whatever, put it in one of the variables.

I would recommend learning how to use arrays/vectors (use vectors), and how to use loops (specifically a for-loop in this case) before you continue.
# TarikNeaj Can you show me how to do this???
Yes, done it after thinking a lot. But still have a problem. How can I sum all postie and negative numbers. Mean I am still stopped at sum_all variable.



#include <iostream>
using namespace std;
int main()
{
	int num, count = 1, sum_positive = 0, sum_negative = 0, sum_all;

	do
	{
		cout << "Enter ten whole numbers\n";
		cin >> num;

		if (num > 0)
		{
			sum_positive = sum_positive + num;
		}
		else
		{
			sum_negative = sum_negative + num;
		}

		count++;
	} while (count <= 10 );

	cout << "Sum of your positive numbers are" << sum_positive << endl;
	cout << "Sum of your negative numbers are" << sum_negative << endl;
	

	return 0;
}

Please tell me how to sum all numbers.
Now this is:--

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>
using namespace std;
int main()
{
	int num, count = 1, sum_positive = 0, sum_negative = 0, sum_all;

	do
	{
		cout << "Enter ten whole numbers\n";
		cin >> num;

		if (num > 0)
		{
			sum_positive = sum_positive + num;
		}
		else
		{
			sum_negative = sum_negative + num;
		}

		count++;
	} while (count <= 10 );

	cout << "Sum of your positive numbers are" << sum_positive << endl;
	cout << "Sum of your negative numbers are" << sum_negative << endl;
	sum_all = sum_postive + sum_negative;
	cout << "Sum of your all whoel numbers are " << sum_all;
	

	return 0;
}

Last edited on
Ok, now I am on the second part of problem, in which program also have to find average of all positive numbers,average of all negative numbers, all and average of all postie + negative numbers. I have done this coding but computer is giving error. Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
using namespace std;
int main()
{
	int num, count = 1, sum_positive = 0, post_n = 0, avr_post;
        int sum_negative = 0, neg_n = 0, avr_neg, sum_all, avr_all;

	cout << "Please enter ten whole numbers.\n";
	cout << "Press return key after writing a number.\n";
	cout << "  \n";
	do
	{
		cout << "Type your number\n";
		cin >> num;

		if (num > 0)
		{
			sum_positive = sum_positive + num;
			post_n++;
		}
		else
		{
			sum_negative = sum_negative + num;
			neg_n++;
		}

		count++;
	} while (count <= 10 );

	cout << "    \n";
	cout << "Sum of your positive numbers are " << sum_postive << endl;
	avr_post = sum_positive / post_n;
	cout << "Average of all positive numbers are " << avr_post << endl;
	cout << "   \n";
	cout << "Sum of your negative numbers are " << sum_negative << endl;
	avr_neg = sum_negative / neg_n;
	cout << "Average of all positive numbers are " << avr_neg << endl;
	cout << "   \n";
	sum_all = sum_positive + sum_negative;
	cout << "Sum of your all whole numbers are " << sum_all;
	avr_all = sum_all / count;
	cout << "The average of all numbers are " << avr_all;
}

Last edited on
We cant read minds. You need to tell us the error. It works perfectly fine for me. Please copy paste the entire error.
Error is "Unhandled exception at 0x00b51675 in jk.exe: 0xC0000094: Integer division by zero."
Compiler gives this error when it reaches at avr_all variable mean line# 42 other programs work fine.

#TarikNaej
Last edited on
Well. Isnt the error clear? You're dividing by zero, you can't do that for obvious reasons, if you dont know why please use google to find out.

Make an if statement that takes care of the division by 0. Like for example, if post_n or neg_n is equal to 0, don't divide.

Edit:

A tip for the future by the way, you can just type that error on google and in almost all cases you'll get plenty of good solutions.
Last edited on
OKy, now another error, compiler saying avr_neg is not initialized .Why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <iostream>
using namespace std;
int main()
{
	int num, count = 1, sum_postive = 0, post_n = 0, avr_post, sum_negative = 0, neg_n = 0, avr_neg, sum_all, avr_all, end_p;

	cout << "Please enter ten whole numbers.\n";
	cout << "Press return key after writing a number.\n";
	cout << "  \n";
	do
	{
		cout << "Type your number\n";
		cin >> num;

		if (num > 0)
		{
			sum_postive = sum_postive + num;
			post_n++;
		}
		else
		{
			sum_negative = sum_negative + num;
			neg_n++;
		}

		count++;
	} while (count <= 10 );

	cout << "    \n";
	cout << "Sum of your postive numbers are " << sum_postive << endl;
	if (sum_postive > 0)
	{
		avr_post = sum_postive/post_n;
	}
	cout << "Average of all postive numbers are " << avr_post << endl;
	cout << "   \n";
	cout << "Sum of your negative numbers are " << sum_negative << endl;
	if (sum_negative > 0)
	{
		avr_neg = sum_negative / neg_n;
	}
	cout << "Average of all negative numbers are " << avr_neg << endl;
	cout << "   \n";

	sum_all = sum_postive + sum_negative;
	cout << "Sum of your all whole numbers are " << sum_all;
	if (sum_all> 0)
	{
		avr_all =  sum_all/count;

	}
	cout << "The avaerage of all numbers are " << avr_all;

	cin >> end_p;
}
Let's try this again... You do not know more than your compiler, please provide the full and exact error message... The code works perfectly fine for me.
Last edited on
Error is
"Run-Time Check Failure #3 - The variable 'avr_neg' is being used without being initialized."
Well. Im not sure if we have the same code because avr_neg is not being used without being initialized. In either case, just initialize it to 0.

neg_n = 0, avr_neg = 0, sum_all,

(You might as well initialize everything to 0)
Last edited on
Ok, now it is working. But why it was not working without avr_neg= 0 ?
If it is not working in this code you provided then Im afraid I dont know, to my knowledge it should work perfectly fine, which it does on visual studio 2013/2015 and on cpp.sh.
Ok, Oky I found the problem.

1
2
3
4
5
6

	if (sum_negative > 0)
	{
		avr_neg = sum_negative / neg_n;
	}

Here how can the sum of negative number greater than 0. Ha ha ha (Laughing).
First that is why compiler skip it and says avr_neg is not initialized and when we have set it to zero then program was working but all the time it was giving same answer which was 0.
Last edited on
Ah, that makes perfect sense :)
Pages: 12