You are on page 1of 2

Exercise :One dimensional array A Hundred students are asked to rate the quality of the food in the student

cafeteria of 1 to 10 with 1 being very bad and 10 being excellent. Suppose that the students answer are randomly generated in your program (i.e.; for each student answer, the program generate random number in the range 1 to 10) Write a C++ program that stores 100 random generated answers in array ANSWER and summarizes the frequency for each rating. Sample output of you program: Rating Frequency 1 12 2 5 3 17 4 4 5 15 6 11 7 5 8 7 9 11 10 13

Answer:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() {int ANSWER[100]={0}; int counter[10]={0},x; int i; srand(1); // computer generates numbers starting from 1 for(i=0;i<100;i++) ANSWER[i]= rand()%10; for(i=0;i<=100;i++) {switch(ANSWER[i]) {case 1:counter[0]++;break; case 2:counter[1]++;break; case 3:counter[2]++;break; case 4:counter[3]++;break; case 5:counter[4]++;break; case 6:counter[5]++;break; case 7:counter[6]++;break; case 8:counter[7]++;break; case 9:counter[8]++;break; case 10:counter[9]++;break; default:x=1;break; } } cout<<"Rating"<<"\t"<<"Frequency"<<endl; for(i=0;i<10;i++) cout<<i+1<<"\t"<<counter[i]<<endl; system("pause"); return 0; }

You might also like