Please Help-Output problem

Can someone please help me with the following program.

I got a question for homework and i tried to make a program but this gives the wrong output.

The question is: Write a C++ program to print sum of positive even integers, positive odd integers and neagitive integers. The program should terminate when the number entered is 0.


The program i tried 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
35
36
37
38
39
40
41
42
43
44
45
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num[100],i,sum1,sum2,sum,n,m;
	m=1;
	while(m!=0)
	{
	sum=0;
	sum1=0;
	sum2=0;
	cout<<"Enter the no. of numbers you want to input: "<<endl;
	cin>>n;
		for(i=0;i<n;i++)
		{
		cout<<"Number "<<i+1<<" : ";
		cin>>num[i];
		}
		for(i=0;i<=n;i++)
		{
			if(num[i]<1)
			{
			sum1=sum1+num[i];
			}
			else if(num[i]>0)
			{
				if(num[i]%2==0)
				{
				sum=sum+num[i];
				}
				else if(num[i]%2==1)
				{
				sum2=sum2+num[i];
				}
			}
		}
	cout<<"Sum of positive even integers is: "<<sum<<endl;
	cout<<"Sum of positive odd integers is: "<<sum2<<endl;
	cout<<"Sum of neagitive integers is: "<<sum1<<endl;
	cout<<"If you want to terminate the program press 0: ";
	cin>>m;
	}
getch();
}


I run the program and input the numbers but at the end the answer comes out to be wrong. For e.g.:

If i take the no. of numbers to be 3,and take
number 1:4
number 2:7
number 3:-6

Then the output comes as:-
Sum of postive even integers=4
Sum of postive odd integers=7
Sum of neagitive integers=-6

Then the screen shows if you want to exit press 0
But if i press any other number the program continues and again asks to input the no. of numbers

If i again take the no. of numbers to be 3,and take
number 1:6
number 2:9
number 3:-4

Then the output comes as:-
Sum of postive even integers=6
Sum of postive odd integers=9
Sum of neagitive integers=-4

The second time i run the program the sum of positive even integers comes out to be the same as the previous sum.

Please help me!!
Hi

I dont understand what you really mean. If you want to get the sum of all entererd numbers, of evens, odds, and negatives, you should take
1
2
3
   sum=0;
	sum1=0;
	sum2=0;
out of the loop, set them to zero, before you enter the loop.

and one thing more on line 20 why for(i=0;i<=n;i++) why i <= n
should not it be i < n ?

hope it helps
Use while(true) and break;
You have to use the modulo function (%). For example is 13 mod 5 (13%5) = 3, because 3 is the offcut of the division from 13 by 5. In your case, you have to use this function in an if-clause to check whether the number is even or odd. So you have to use input_no mod 2 (input_no%2). Then you get this if-clause:
1
2
3
4
5
6
if((input_no%2)!=0)
{
    //Do whatever you want to do with the ODD NUMBERS
}else{
    //Do whatever you want to do with the EVEN NUMBERS
}

This is the solution for the first aspect (the even and odd numbers). For the second (negative integers) you only have to use another if-clause. This one checks whether the number is bigger or equals zero or if it's lower. The code is really easy:
1
2
3
4
5
6
if(input_no>=0)
{
    //Do something with this POSITIVE NUMBER
}else{
    //Do something with this NEGATIVE NUMBER
}


But how I see, you have already managed to do this. But I have found a critical error: What - do you think - does the program if you enter more than 100 numbers? (I know it's unrealistic, but possible.) The program will crash. So I'd make sure that you don't allocate more numbers than you have space in the array.
(at lines 15 and 20)
1
2
3
4
for(int i=0;i<n&&i<100;i++)
{
    ...
}

This little code above should manage this. See the difference? I added a &&i<100 in the conditional part of the for-loop.
A second error first appeared to me when I entered this to my DEV-C++ compiler. It says:
Line 4: 'main' must return 'int'

As you see, the return value of your main() is void, not int. So change this, please.
What I also missed was that you use cin/cout/etc instead of std::cin/std::cout/etc. You either have to use using namespace std; at the beginning of your code or - everytime when you call cin or cout or... - use std::.
And, to your other problem...
priyanshm wrote:
Then the screen shows if you want to exit press 0
But if i press any other number the program continues and again asks to input the no. of numbers

...in my version (already corrected), the program doesn't continue if I press 0 and enter. Might be that it was in connection with these errors.
By the way, I see what you mean with that the program outputs the wrong results. As I entered
Enter the no. of numbers you want to input:
5
Number 1: -1
Number 2: 6
Number 3: 5
Number 4: 4
Number 5: 2

the results were these:
positive even: 6225932
positive odd: 5
negative: -1

By the way, the last one is correct, but only this one. I guess it might be that you did not initialize your array. You know, something like this:
1
2
int array[5];
for(int i=0;i<5;i++) array[i]=0;

If you don't initialize your variables, you don't know what's in there.

After all these explanations, I'll give you the corrected version of your program.
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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int num[100],i,sum1,sum2,sum,n,m;
	m=1;
	while(m!=0)
	{
		sum=0;
		sum1=0;
		sum2=0;
		cout<<"Enter the no. of numbers you want to input: "<<endl;
		cin>>n;
		for(i=0;i<n&&i<100;i++)
		{
			cout<<"Number "<<i+1<<" : ";
			cin>>num[i];
		}
		for(i=0;i<n&&i<100;i++)
		{
			if(num[i]<0)
			{
				sum1=sum1+num[i];
			}
			else if(num[i]>=0)
			{
				if(num[i]%2==0)
				{
					sum=sum+num[i];
				}
				else if(num[i]%2==1)
				{
					sum2=sum2+num[i];
				}
			}
		}
		cout<<"Sum of positive even integers is: "<<sum<<endl;
		cout<<"Sum of positive odd integers is: "<<sum2<<endl;
		cout<<"Sum of negative integers is: "<<sum1<<endl;
		cout<<"If you want to terminate the program press 0: ";
		cin>>m;
	}
	
	system("PAUSE");
	
	return 0;
}


With this code you get the right results. =)

And by the way, this forum is not for homework. So it would be far easier to get an answer if you don't write that it's for a homework. ;)
Last edited on
And by the way, this forum is not for homework. So it would be far easier to get an answer if you don't write that it's for a homework. ;)


Don't encourage dishonesty.
This is my homework but in the forum rules it is written that
"Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions."
I havent asked for the entire solution but just for a hint. I made the program myself. I am just asking what is my mistake in the program.
Last edited on
Thank you so much FlashDrive i did what u said and i get the correct answer.
and i also have figured why is ur version of program not working when u enter zero the program does not terminte is because in line 28 of ur program it should have been
1
2
3
4
else if(num[i]>0)
{
\\whatevr u want to do
}

There shouldnt be the = sign.
Last edited on
Topic archived. No new replies allowed.