3

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)

  • E = O = 0, you declare arrays of size 0 ... – Déjà vu Mar 18 '23 at 09:08
  • How your code is not working as you expected? I run it on my Pc and it seems working for me! First I noticed you declared an array of the length of 0 and I thought is the problem you are facing, but again the program worked just fine, and I don't why 0_o, cause this normally should throw an error. So tell us what you are expecting from the program to output and what you find. – Salah a Mar 18 '23 at 09:22
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 18 '23 at 12:27

2 Answers2

5
int evens[E] , odds[O];

I guess this line doesn't work correctly. You'd like to allocate two arrays sized E and O, but they're now 0. Besides, it's not very recommended to declare an array using a variable as length.

As it's written by C++, you can use vector to allocate memory dynamically. You can rewrite it in a much better way (I strongly suggest using STL):

#include <iostream>
#include <vector>
using namespace std;

int n = 0;
void accept ();
void evenodd (vector<int>&);
int sum (vector<int>&);
void display(vector<int>&,vector<int>&,vector<int>&,int,int);
int main()
{
    cout<<"Enter How Many Number You Want to Add \n";
    cin>>n;
    accept();
    return 0;
}
void accept()
{
    vector<int> num(n, 0);
    for (int x=0;x<n;x++)
    {
        cout<<"Enter The Number \n";
        cin>>num[x];
    }
    evenodd(num);
}
void evenodd (vector<int>& num)
{
    vector<int> evens, odds;
    for( int x=0;x<n;x++)
    {
        if (num[x]%2==0)
        {
            evens.push_back(num[x]);
        }
        else 
        {
            odds.push_back(num[x]);
        }
    }
    int sumE=sum(evens);
    int sumO=sum(odds);
    display(num,evens,odds,sumE,sumO);
}
int sum (vector<int>& nums)
{
    int ans=0;
    for(int x=0;x<nums.size();x++)
        ans+=nums[x];
    return ans;
}
void display ( vector<int>& num, vector<int>& evens, vector<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<evens.size();x++)
        cout<<evens[x];
    cout<<"\n The list of Odds numbers to be added is \n";
    for (int x=0;x<odds.size();x++)
        cout<<odds[x];
    cout<<"\n The Sum of Even numbers is "<<sumE<<endl;
    cout<<"\n The Sum of Odd numbers is "<<sumO<<endl;
}

Result:

Enter How Many Number You Want to Add
4
Enter The Number
1 2 3 4
Enter The Number
Enter The Number
Enter The Number
The list of numbers to be added is
1234
 The list of Even numbers to be added is
24
 The list of Odds numbers to be added is
13
 The Sum of Even numbers is 6

 The Sum of Odd numbers is 4
cup11
  • 177
  • 7
  • 2
    Not sure why this answer gets no votes when the clearly incorrect answer above gets three. In C++ this task should be solved using vectors. – john Mar 18 '23 at 09:27
  • @john It's possible for a C++ student to be given assignments that require to use C-style array. You can't blame the students for being in such a situation. – screwnut Mar 18 '23 at 16:06
  • @screwnut I'm not blaming anyone for anything. – john Mar 18 '23 at 18:56
1

This should work. check it out.

#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[n] , odds[n]; // declare arrays with size n
    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;
}
kibromhft
  • 911
  • 1
  • 7
  • 18
  • 3
    `int evens[n]` (and many other examples) is not legal C++. In C++ a array bounds must be constant, but here `n` is a variable. – john Mar 18 '23 at 09:25