急问 关于C++的一道数组题

来源:百度知道 编辑:UC知道 时间:2024/06/17 00:40:33
Write a function
void separator(int a[ ], int na, int odd[ ], int& no,
int even[ ], int& ne)
This function separates odd and even integers. For example, if a[ ] = {1,2,3,4,5}, na=5, then odd[ ] = {1,3,5}, no=3, even[ ]={2,4}, ne=2. Write a main program that illustrates the use of this function (it must display all arrays).

大致意思就是,写一个方程,分开数组中的奇数偶数。
如a[]={1,2,3,4,5},则 奇数[]={1,3,5} 奇数个数为3;偶数[]={2,4},偶数个数为2 。

#include<iostream>
using namespace std;

void separator(int a[],int na,int odd[],int& no,int even[],int& ne);
int main()
{
int *a,*odd,*even,
na,ne=0,no=0,
i;
cout<<"输入数组长度:";
cin>>na;
a=new int[na];
odd=new int[na];
even=new int[na];

cout<<"为数组a赋值:"<<endl;
for(i=0;i<na;i++)
cin>>a[i];

separator(a,na,odd,no,even,ne);
cout<<"输入的数组为:";
for(i=0;i<na;i++)
cout<<a[i];
cout<<endl<<"其中奇数"<<no<<"个:";
for(i=0;i<no;i++)
cout<<odd[i];
cout<<endl<<"偶数"<<ne<<"个:";
for(i=0;i<ne;i++)
cout<<even[i];
cout<<endl;
delete []a;
delete []odd;
delete []even;

return 0;
}

void separator(int a[],int