列出的C++函数之一无法正常工作

6ju8rftf  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(137)

作为一个初学者,我试图用函数来求出奇数和偶数的和。我不知道是哪个,但我的一个函数没有像我预期的那样工作。所以请帮助我。

#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;
    }

我怀疑,除了这个功能以外,其他都正常。

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);
    }

而且整个程序不适用于“N〉3”(如果用户输入超过三个)

whlutmcx

whlutmcx1#

int evens[E] , odds[O];

我猜这一行是不能正常工作的,你想分配两个大小分别为EO的数组,但是现在它们是0,另外,不推荐使用变量作为长度来声明数组。
因为它是用C++编写的,所以你可以使用vector来动态分配内存,你可以用一种更好的方式重写它(我强烈建议使用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;
}

结果:

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
8mmmxcuj

8mmmxcuj2#

这个应该可以,看看.

#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;
}

相关问题