Problem:
Given a set of n student's examination marks in the range 0 to 100 make a count of the number of students that obtained each possible mark.
Solution:
package array.programs;
import java.util.Scanner;
public class ArrayCounting {
int[] marks = new int[101];
int n;
public static void main(String[] args) {
ArrayCounting arrayCounting = new ArrayCounting();
arrayCounting.getTheMarks();
arrayCounting.printTheArray();
}
public void getTheMarks(){
Scanner scanner = new Scanner(System.in);
System.out.println("how many marks");
n = scanner.nextInt();
int tmp = 0;
for(int i = 0; i < n; i++){
System.out.println("enter marks in the range 0 to 100");
tmp = scanner.nextInt();
marks[tmp] = marks[tmp] + 1;
}
scanner.close();
}
public void printTheArray(){
for(int i = 0; i<= 100; i++){
if(marks[i] != 0){
System.out.println(i + " marks obtained by " + marks[i] + " students");
}
}
}
}
Output:
how many marks
10
enter marks in the range 0 to 100
23
enter marks in the range 0 to 100
3
enter marks in the range 0 to 100
22
enter marks in the range 0 to 100
56
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
44
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
3 marks obtained by 1 students
22 marks obtained by 1 students
23 marks obtained by 1 students
44 marks obtained by 1 students
55 marks obtained by 5 students
56 marks obtained by 1 students
Given a set of n student's examination marks in the range 0 to 100 make a count of the number of students that obtained each possible mark.
Solution:
package array.programs;
import java.util.Scanner;
public class ArrayCounting {
int[] marks = new int[101];
int n;
public static void main(String[] args) {
ArrayCounting arrayCounting = new ArrayCounting();
arrayCounting.getTheMarks();
arrayCounting.printTheArray();
}
public void getTheMarks(){
Scanner scanner = new Scanner(System.in);
System.out.println("how many marks");
n = scanner.nextInt();
int tmp = 0;
for(int i = 0; i < n; i++){
System.out.println("enter marks in the range 0 to 100");
tmp = scanner.nextInt();
marks[tmp] = marks[tmp] + 1;
}
scanner.close();
}
public void printTheArray(){
for(int i = 0; i<= 100; i++){
if(marks[i] != 0){
System.out.println(i + " marks obtained by " + marks[i] + " students");
}
}
}
}
Output:
how many marks
10
enter marks in the range 0 to 100
23
enter marks in the range 0 to 100
3
enter marks in the range 0 to 100
22
enter marks in the range 0 to 100
56
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
44
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
enter marks in the range 0 to 100
55
3 marks obtained by 1 students
22 marks obtained by 1 students
23 marks obtained by 1 students
44 marks obtained by 1 students
55 marks obtained by 5 students
56 marks obtained by 1 students
No comments:
Post a Comment