Design a function StatisticS() Generate 100 random numbers in the range of [-100
ID: 3806998 • Letter: D
Question
Design a function StatisticS()
Generate 100 random numbers in the range of [-100, 100 ] ;
Sum all the negative numbers and store the result in sumN
Count all the negative numbers and store the result in CountN
Average all the negative numbers and store the result in StatN
Sum all the positive numbers and store the result in sumP
Count all the positive numbers and store the result in CountP
Average all the positive numbers and store the result in StatP
return sumN , CountN , StatN , sumP , CountP , and StatP
Write a C++ program to test StatisticS()
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void StatisticS();
void test_StatisticS();
signed long int out[6];
int main()
{
StatisticS();
test_StatisticS();
return 0;
}
void StatisticS() {
unsigned int i;
signed int number = 0;
signed long int sumN = 0;
signed long int countN = 0;
signed long int statN = 0;
signed long int sumP = 0;
signed long int countP = 0;
signed long int statP = 0;
srand(time(NULL));//to initialize seed
for(i=0;i<100;i++) {
//generate random number
number = rand() % 201 - 100;
//cout<<" random no is "<<number;
if (number < 0) {
countN++;
sumN += number;
}
if (number > 0) {
countP++;
sumP += number;
}
}
if (countN > 0)
statN = sumN / countN;
if (countP > 0)
statP = sumP / countP;
out[0] = sumN;
out[1] = countN;
out[2] = statN;
out[3] = sumP;
out[4] = countP;
out[5] = statP;
/*print statisticS*/ /*
cout<<" sumN is "<<sumN;
cout<<" countN is "<<countN;
cout<<" statN is "<<statN;
cout<<" sumP is "<<sumP;
cout<<" countP is "<<countP;
cout<<" statP is "<<statP;
*/
}
void test_StatisticS() {
//out[0] = sumN
//out[1] = countN
//out[2] = statN
//out[3] = sumP
//out[4] = countP
//out[5] = statP
(out[0] >= -10000) ? cout<<" sumN is OK" : cout<<" sumN is wrong";
(out[1] <= 100) ? cout<<" countN is OK" : cout<<" countN is wrong";
(out[2] >= -100) ? cout<<" statN is OK" : cout<<" statN is wrong";
(out[3] <= 10000) ? cout<<" sumP is OK" : cout<<" sumP is wrong";
(out[4] <= 100) ? cout<<" countP is OK" : cout<<" countP is wrong";
(out[5] <= 100) ? cout<<" statp is OK" : cout<<" statP is wrong";
}
Please let me know in case of any doubts.
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.