. 随机产生100个位于【-100,100】之间的整数并保存在数组里,统计数组中的正数、负数的个数,计算平均值和标准差。
. 随机产生100个位于【-100,100】之间的整数并保存在数组里,统计数组中的正数、负数的个数,计算平均值和标准差。
收起
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
int a[100];
int main() {
srand(time(0));
for(int i = 0; i < 100; i++) {
a[i] = rand() % 201 - 100;
}
double s = 0, s2 = 0, ave, sn;
int n = 0, m = 0;
for(int i = 0; i < 100; i++) {
if(a[i] > 0) n++;
else if(a[i] < 0) m++;
s += a[i];
}
ave = s / 100;
for(int i = 0; i < 100; i++) {
s2 = s2 + pow(a[i] - ave, 2);
}
sn = sqrt(s2 / 100);
cout << "正数个数:" << n << endl;
cout << "负数个数:" << m << endl;
cout << "平均值:" << ave << endl;
cout << "标准差:" << sn << endl;
return 0;
}
报告相同问题?