C++/array

Advertisement


Question

-------------------------

Followup To

Question -
hi prince m.premnath im from malaysia i do not how do this question can u help me  thank you       


write a program that students test score in  the range 0-100.IT should then determine the number of students having scores in each of the following ranges.0-39,40-44,50-54,55-59,60-64,65-69,70-74,75-79,80-89,90-100.Output the score range and the number of students (Run your program with the following input data:12,45,100,78,41,53,85,66,73,78,58,52,39,80,75,55,40,45,50,90,89,100,0,29,51,63,67,73,76,55,89,74,65,55,89,74,65,64,99,87,70.

Answer -
Its really simple!

1. declare an array enough to store your data's
2. Traverse the array from the begining
3. Check the number with the multiple conditions if it matches with any one of the given case then incriment the value in an empty array that has been initialized with 0,
4. Then finally using the second array you can retrive the number of students belonging to the given set of ranges!

eg:

I consider with two case ( say 0-50 , 51 - 100 )

int arr[20]; // to store 20 marks
int range[2];
range[0] = 0;// Initialize the array with 0's insted you can also use for loop to initialize bigger array.
range[1] = 0;
int i;
int j;
int mark;
// get the 20 marks
    .
    .

for(i = 0 ; i < 20 ;i++)
{
   mark = arr[i];
   
   if( mark >= 0 && mark <= 50)
     arr[0] ++;
   else
     arr[1] ++;// you can use else if statements for multiple ranges !
}

  printf("Students in the range 0-50 %d" , arr[0]);
  printf("Students in the range 51 - 100 %d" ,arr[1]);
}

Thank you !



but i can not get the ans in c++.the program give error .why


Answer
Ok Nothing to worry Here is the program !


#include <stdio.h>
#include<conio.h>
void main()
{
int arr[50];
static int range[10];

int n;
int i , mark;

printf("Enter number of mark's:");
scanf("%d" , &n);
for( i = 0 ; i < n ; i++)
  scanf("%d" , &arr[i]);
for(i = 0 ; i < n ; i++)
{
   mark = arr[i];
   if( mark >= 0 && mark <=39)
  range[0]++;
   else if( mark >= 40 && mark <=44)
  range[1]++;

   else if( mark >= 50 && mark <=54)
  range[2]++;
   else if( mark >= 55 && mark <=59)
  range[3]++;

   else if( mark >= 60 && mark <=64)
  range[4]++;

   else if( mark >= 70 && mark <=74)
  range[5]++;

   else if( mark >= 75 && mark <=79)
  range[6]++;

   else if( mark >= 80 && mark <=89)
  range[7]++;

   else if( mark >= 90 && mark <=100)
  range[8]++;

}
printf("Number of students in the range 0-39: %d\n" , range[0]);
printf("Number of students in the range 40-44: %d\n" , range[1]);
printf("Number of students in the range 50-54: %d\n" , range[2]);
printf("Number of students in the range 60-64: %d\n" , range[3]);
printf("Number of students in the range 70-74: %d\n" , range[4]);
printf("Number of students in the range 75-79: %d\n" , range[5]);
printf("Number of students in the range 80-89: %d\n" , range[6]);
printf("Number of students in the range 90-100: %d\n" , range[7]);
getch();
}


   THANK YOU !

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.