ROGRAM SPECIFICATION . Several he program is an interactive program that reads i
ID: 3705893 • Letter: R
Question
ROGRAM SPECIFICATION . Several he program is an interactive program that reads input from the keyboard structions (listed below) may be entered by the user. For each input, the program will give short response printed to cout. Each response to cout is followed by a single endl. mportant: The program must not try to store all the numbers in a single big vector. In fact, it Instead, you should create a frequency vector which has indexes from [O] to frequency vector should be initialized to all zeros, and each time a number is read, the outlawed by the CSI police that you have any vector that is larger than 100 elements such as frequency[i], tells you how many times the number i has occurred in the input. This [99]. A location, program will add one to the corresponding vector location. For example, frequency[42 begins at zero. When one 42 is read, frequency[42] is increased to one. If another 42 is read, frequency[42] is increased to two, and so on. All of the statistics that you need to calculate can be computed based on the frequency vector (so that you don't need to store all those numbers separately) Input: The letter N followed by an integer in the range 0 to 99, The biggest · possible input number is 99. The specification does not care what the program does for numbers that are outside of the legal range Output: The word "OK" Input: The letter S Output: The output is the SUM of all the input numbers read so far. This output is O if there has not yet been any numbers read by the program. Input: The letter A Output: The output is the AVERAGE of all the input numbers read so far, calculated as a double number. For example, if there have been four input numbers 2, 2, 6 and 5Explanation / Answer
SOLUTION:
THE BELOW CODE REPRESENTS WITH RESPECT TO THE GIVEN DATA;
#include<iostream>
//#include<stdlib>
void main()
{
char command;
int number, count;
int frequency[100]={0};
count=0;
while(true)
{
cin >> command;
//
switch(command)
{
case 'N':
{
cin >> number;
if(number > 99 || number <0) cout << "Invalid Number";
else
{
frequency[number]++;
count++;
cout << "OK"<< endl;
}
break;
}
case 'S':
{
int sum=0;
for(int i=0; i<100;i++)
{
sum=sum+(frequency[i]*i);
}
cout << sum<< endl;
break;
}
case 'A':
{
int sum=0;
for(int i=0; i<100;i++)
{
sum=sum+(frequency[i]*i);
}
cout << (float)sum/count << endl;
break;
}
case 'B':
{
int sum=0;
for(int i=99; i>=0;i--)
{
if(frequency[i]>0)
{
cout << i << endl;
break;
}
}
break;
}
case 'F':
{
int h=0;
for(int i=0; i<99;i++)
{
if(frequency[i]>h)
{
h=i;
}
}
cout << h << endl;
break;
}
case 'H':
{
cout << count << endl;
break;
}
case 'M':
{
int temp;
if(count % 2 != 0)
temp = ((count+1)/2)-1;
else temp=(count/2)-1;
int j=0;
for(int i=0; i<100;i++)
{
if(frequency[i]>0)
{
j++;
if(j==temp)
{
cout << i;
break;
}
}
}
break;
}
case 'Q':
{
cout <<"END";
return 0;
}
default: cout <<"Illegal code ";
}
cin.clear();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.