问题描述
编写一个读取大小为4(4个元素)的数组的程序。并且
1-使用数组计算这四个元素的总和。
2-使用数组计算这四个元素的平均值。
3-找出最小元素。
我试过,但它说错误。
我尝试过:
int item [4];
浮点数= 0,aver,minElement = 0;
cout<<输入四个元素<< endl;
for( int i = 0,i< 4; i ++){
cin>> item [i];
sum = sum + item [i];
}
aver = sum / 4;
minElement = item [0]
for(int i = 1; i< 4; i ++){
if(item [i]
Write a program that read an array of size 4 (4 elements). and
1- Compute the sum of these four elements using the array.
2- Compute the average of these four elements using the array.
3- Find out the minimum element.
I Tried but it says Error .
What I have tried:
int item[4];
float sum=0, aver,minElement=0;
cout <<"enter four elements"<<endl;
for(int i =0,i<4;i++){
cin>>item[i];
sum =sum+item[i];
}
aver=sum/4;
minElement=item[0]
for(int i =1;i<4;i++){
if (item[i]
推荐答案
//for(int i =0,i<4;i++)
// Should be:
for(int i =0;i<4;i++)
#include <array>
#include <climits>
#include <iostream>
using namespace std;
int main()
{
const size_t ITEMS = 4;
std::array<int, ITEMS> a;
double avg = 0.0;
int min = INT_MAX;
cout << "enter " << ITEMS << " integers" << endl;
for ( auto & x : a)
{
cin >> x;
avg += x;
min = min < x ? min : x;
}
avg /= ITEMS;
cout << "average " << avg << ", minimum " << min << endl;
}
这篇关于C ++程序错误(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!