The program should be structured as follows, i. Ask for the number of grades to
ID: 3767176 • Letter: T
Question
The program should be structured as follows,
i. Ask for the number of grades to be inputted,
ii. Store all the input grades one by one in an array,
iii. Call a function, the prototype looks like below:
int countChar(char [], int, char);
where the return of the function is the count of a give letter grade, which is the third
parameter with the type char in the above function. The first parameter is the array
holding the grades provided in ii. The second parameter is the number of grades obtained
from i.
The function will go through the array and count how many occurrences for the
designated letter grade
Explanation / Answer
import java.util.Scanner;
public class Count {
static int countChar(char array[], int p, char z)
{int count=0;
for(int i=0;i<p;i++)
{
if(array[i]==z)
count++;
}
return count;
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter no of grades you want to input(1-50)");
int n=s.nextInt();
char array[]=new char[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter Grade");
array[i]=s.next().charAt(0);
}
System.out.println("No of As is: "+countChar(array, n, 'A'));
System.out.println("No of Bs is: "+countChar(array, n, 'B'));
System.out.println("No of Cs is: "+countChar(array, n, 'C'));
System.out.println("No of Ds is: "+countChar(array, n, 'D'));
System.out.println("No of Es is: "+countChar(array, n, 'E'));
System.out.println("No of Fs is: "+countChar(array, n, 'F'));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.