# C++ Program Required with arrays

Asked By 20 points N/A Posted on -

Hi,

I want to write a program that will display the number of even, odd and zero values in an array. The issue is that ,whenever I enter values in array, C++ compiler doesn’t show the result and finishes the output box. Is there any solution for this?

Thanks friends.

SHARE
Answered By 0 points N/A #91607

## C++ Program Required with arrays

Hey Suny!

Have you fixed the size of the array? I'm thinking that maybe, since number of even, odd, and zero values will not be equal to each other, you have to set your array as variable size.

The problem is some compilers do not allow variable sized arrays. How about you fixing your array, with a big size and then set a letter as a default to all elements? Then have the letter replaced by the values you want in the array. If you set your counter, not to count the default letter, then you can still count the number of even, odd and zero values, even if you have a fixed array.

Answered By 10 points N/A #91608

## C++ Program Required with arrays

Use header file Conio.h and at the end of main function, before closing bracket, add statement

Getch();

This will hold the output window until you press any character.

Answered By 20 points N/A #91609

## C++ Program Required with arrays

Wow, it really solves my problem.  But there is another issue. The calculation of even numbers is wrong. I have used the statement like:

If(a[i]%2==0) for even

and

If(a[i]%2!=0) for odd

Guide me to solve this issue.

Answered By 10 points N/A #91610

## C++ Program Required with arrays

Mathematics considers even '0' as an even number, maybe that is why the counting of your even numbers is wrong.  Try again and check the result.

And also check the even number in if and in else case doesn’t use any statement for check, because by default if the number is not even, then that is odd. And check the zero value in other if statements.

Also there can be a reason that, you are not assigning '0' to your variables.  Because of this some times c++ compiler holds the last saved values in variable and adds the new value in that.  Assign '0' values to all your variables, at the time of defining.

Answered By 20 points N/A #91612

## C++ Program Required with arrays

I have performed all the solutions you have mentioned, but the result is the same. Number of odd values is right, but no of even and zero values are wrong.

Answered By 10 points N/A #91614

## C++ Program Required with arrays

I am sending you the code I tried make comparison of your code and understand the difference. For more ask me freely.

#include<iostream.h>

#include<conio.h>

int main()

{

int a[10],i,even,odd,zero;

i=0;

even=0;

odd=0;

zero=0;

for(i=0;i<=9;i++)

{

cout<<"Enter the value in Array: ";

cin>>a[i];

}

for(i=0;i<=9;i++)

{

if(a[i]%2==0)

{even=even+1;}

else

{odd=odd+1;}

if(a[i]==0)

{zero=zero+1;}

}

cout<<"n     Number of Even Values :     "<<even<<endl;

cout<<"n     Number of Odd Values  :     "<<odd<<endl;

cout<<"n     Number of Zero Values :     "<<zero<<endl;

getch();

}

Answered By 20 points N/A #91616

## C++ Program Required with arrays

Absolutely right solution for my problem. I have solved it before, according to your tips; but its awesome to see this coding approach, very concise and to the point. Thanks a lot.