c++之二:利用一维数组求最大,最小,平均值
#include <iostream> using namespace std; int main(){ int sum,max=0,min=100,a[3]; float avg; sum=0; for (int i=1;i<=9;++i) { //cin >> a[i]; a[i]=rand()% 100; cout<<a[i]<<endl; sum += a[i]; if (a[i]<=min) min=a[i]; if (a[i]>=max) max=a[i]; } avg=sum/9; cout <<endl<< sum<<endl; cout <<avg<<endl; cout <<max<<endl; cout <<min<<endl; return 0; }
随机产生9个数,利用数组求和,平均数,最大值,最小值
shaochi