Arrays Frequencies storing Gate 2005


#1

A program P reads in 500 integers in the range [0, 100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies?

(i)An array of 50 numbers

(ii)An array of 100 numbers

(iii)An array of 500 numbers

(iv)A dynamically allocated array of 550 numbers


#2

Best way to store is array with 50 elements. Those are 51 to 100. Others are no need to store


#3

Yes we just need to store “An array of 50 numbers” .

The code will look something like this.

#include <stdio.h>

int main() {

int ar[50];
//initialize all array elements to 0
for(int i=0;i<50;i++){
ar[i]=0;
}

//to read 500 integer the below for-loop is used
for(int i=1;i<=500;i++) {                   
int x=0;
scanf("%d",&x);
if(x>50){
ar[(x%50)-1]++;
}
}

//prints the numbers and their frequency
for(int i=0;i<50;i++){
printf("%d %d \n",(i+51),ar[i]);
}

return 0;
}

So option (i) is the correct answer.


#4

1 st option is correct if we want to print the values.but inorder to store the values OPTION 2 (an array of 100 numbers) is the correct answer.

Let us assume the array indexes takes place in the range of 1 to 100.In every index we have to store marks frequency.
for example : if we consider about points scored in class ,the data of how many number of students got 10 points will be stored in 10th index.in the question we have been give with the ranger of [0,100]. so option (2).

In case of printing we have more than 50 points so array index 50 to 100 will be enough.

But in the question we are asked about storing frequency.so option 2 an array of 100 numbers is the correct answer.