c++编程,数组方面的题
发布网友
发布时间:2023-09-30 04:07
我来回答
共4个回答
热心网友
时间:2024-11-26 19:22
#include<iostream>
using namespace std;
void main()
{
int sum_odd=0,sum_even=0;
int a[10]={3,6,45,12,16,2,8,9,7,5};
for(int i=0;i<10;i++)
{
if(a[i]%2==0)
sum_odd+=a[i];
else
sum_even+=a[i];
}
cout<<"偶数之和:"<<sum_odd<<endl;
cout<<"奇数之和:"<<sum_even<<endl;
}
/*运行结果如下:
偶数之和:44
奇数之和:69
*/
热心网友
时间:2024-11-26 19:22
#include<stdio.h>
main()
{
int odd = 0;
int even = 0;
int a[10]={3,6,45,12,16,2,8,9,7,5};
int i;
for(i = 0; i < 10; i++)
{
if(a[i]%2 == 0)
odd += a[i];
else
even += a[i];
}
printf("\n even=: %d",even);
printf("\n odd=: %d",odd);
}
热心网友
时间:2024-11-26 19:23
给你写一个函数来实现
#include<iostream>
using namespace std;
void Sum(int *a ,int n,int &oshe,int &jshe);
//n为数组大小,
void main()
{
int *shzu;
int number;
int oshuhe=0,jshuhe=0;
cout<<"输入数组大小";
cin>>number;
shzu= new int[number];
cout<<"输入数字";
for(int i=0;i<number;i++)
{
cin>>shzu[i];
}
Sum(shzu,number,oshuhe,jshuhe);
cout<<"偶数和为"<<oshuhe<<endl;
cout<<"奇数和为"<<jshuhe<<endl;
}
void Sum(int *a,int n,int &oshe,int &jshe)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
oshe+=a[i];
}
else
{
jshe+=a[i];
}
}
}
热心网友
时间:2024-11-26 19:23
#include<stdio.h>
main()
{
int odd,even;
int a[10]={3,6,45,12,16,2,8,9,7,5};
int j;
for ( j=0,even=0,odd=0;j<10;j++)
{
if (a[j]%2==0)
{
even+=a[j];
}
else
{
odd+=a[j];
}
}
printf("\n even=: %d",even);
printf("\n odd=: %d",odd);
}