As a Beginner am trying to find the sum of even and odd numbers with their list using functions. I don't know which but one of my function is not working as I expected. So help me out. With Respect.
#include <iostream>
using namespace std;
int n=0, E=0, O=0; // E and O index counter
void accept ();
void evenodd (int[]);
int sumEven (int[]);
int sumOdd (int[]);
void display(int[],int[],int[],int,int);
int main()
{
cout<<"Enter How Many Number You Want to Add \n";
cin>>n;
accept();
return 0;
}
void accept()
{
int num[n];
for (int x=0;x<n;x++)
{
cout<<"Enter The Number \n";
cin>>num[x];
}
evenodd(num);
}
void evenodd (int num[])
{
int evens[E] , odds[O];
for( int x=0;x<n;x++)
{
if (num[x]%2==0)
{
evens[E]=num[x];
E++;
}
else
{
odds[O]=num[x];
O++;
}
}
int sumE=sumEven(evens);
int sumO=sumOdd(odds);
display(num,evens,odds,sumE,sumO);
}
int sumEven (int evens[])
{
int sumE=0;
for(int x=0;x<E;x++)
sumE+=evens[x];
return sumE;
}
int sumOdd (int odds [])
{
int sumO=0;
for (int x=0;x<O;x++)
sumO+=odds[x];
return sumO;
}
void display ( int num[], int evens[], int odds[], int sumE, int sumO)
{
cout<<"The list of numbers to be added is \n";
for (int x=0;x<n;x++)
cout<<num[x];
cout<<"\n The list of Even numbers to be added is \n";
for (int x=0;x<E;x++)
cout<<evens[x];
cout<<"\n The list of Odds numbers to be added is \n";
for (int x=0;x<O;x++)
cout<<odds[x];
cout<<"\n The Sum of Even numbers is "<<sumE<<endl;
cout<<"\n The Sum of Odd numbers is "<<sumO<<endl;
}
I Suspect, Except the this Function all are ok.
void evenodd (int num[])
{
int evens[E] , odds[O];
for( int x=0;x<n;x++)
{
if (num[x]%2==0)
{
evens[E]=num[x];
E++;
}
else
{
odds[O]=num[x];
O++;
}
}
int sumE=sumEven(evens);
int sumO=sumOdd(odds);
display(num,evens,odds,sumE,sumO);
}
But also whole program not working for "N>3" (if the user input more than Three)