Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在数组C+中输入用户时,如何获得奇数和偶数之和+;_C++ - Fatal编程技术网

C++ 在数组C+中输入用户时,如何获得奇数和偶数之和+;

C++ 在数组C+中输入用户时,如何获得奇数和偶数之和+;,c++,C++,我们的编程活动是构造一个C程序,创建一个大小为12的数组,然后分别显示偶数索引单元格和奇数索引单元格的总和及其平均值。示例运行必须如下所示 样本运行: 输入12个元素:1 偶数索引单元格之和:6 奇数索引单元格之和:6 平均:6.0 然而,我在求奇数和偶数之和时遇到了困难。比如我如何让程序识别数组中的奇数和偶数?我是否需要添加“if else”这是我的代码 #include <iostream> using namespace std; int A[11],B[11],C[11];

我们的编程活动是构造一个C程序,创建一个大小为12的数组,然后分别显示偶数索引单元格和奇数索引单元格的总和及其平均值。示例运行必须如下所示

样本运行: 输入12个元素:1

偶数索引单元格之和:6

奇数索引单元格之和:6

平均:6.0

然而,我在求奇数和偶数之和时遇到了困难。比如我如何让程序识别数组中的奇数和偶数?我是否需要添加“if else”这是我的代码

#include <iostream>
using namespace std;

int A[11],B[11],C[11];
int sum,ave;
int x;
main()
{
cout<<"Enter 12 elements A: "<<endl;
    for(x=0; x<11; x++) 
        cin>>A[x];

cout<<endl<<"Sum of even-indexed number: "<<endl;
    for(x=0; x<=11; x++){
        cout<<B[x];
    }           
cout<<endl<<"Sum of odd-indexed number: "<<endl;
    for(x=0; x<=11; x++){
        cout<<C[x];
    }
                    
cout<<endl<<"Average: "<<endl;
    for(x=3; x<3; x++)
    ave += B[x]+C[x]/A[x];
        cout<<A[x]<<" ";
return 0;
}
#包括
使用名称空间std;
int A[11],B[11],C[11];
int sum,ave;
int x;
main()
{

cout你不需要3个数组,只需要一个元素数组和2个变量的偶数和赔率之和

您可以通过检查索引的剩余部分除以2是否等于0来检查数组中的位置

您还可以开始一个偶数位置,并将索引增加2,这样它只会检查偶数位置。对于奇数位置,您也可以执行相同的操作,增加2,但从奇数位置开始。

\35; include
#include <iostream>
using namespace std;

int main() {
    int array[12];
    int evenTotal = 0;
    int oddTotal = 0;
    for(int i=0;i<12;i++) {
        cin>>array[i];
        cout<<array[i]<<" ";
        if(i%2 == 0){
            //If we consider indexing with 0 to 11
            // If we consider indexing with 1 to 12
            // than this condition will be odd
            evenTotal += array[i];
        }
        else {
            oddTotal += array[i];
        }
    }
    cout<<"\n";
    // Because we have 6 evens and odds
    int average = (evenTotal + oddTotal)/12;
    cout<<"Even Total = "<<evenTotal<<"\n";
    cout<<"Ödd Total = "<<oddTotal<<"\n";
    cout<<"Average = "<<average;
    return 0;
}
使用名称空间std; int main(){ int数组[12]; int evenTotal=0; int-oddtoal=0; 对于(int i=0;i>array[i]; cout
for(x=0;x>A[x];
-您只读取11个数字。此外,当您要处理12个数字时,为什么要定义11个元素的数组?另外,您认为在哪里计算和?