Respuesta :
Answer:
In Java:
import java.util.*;
public class Main{
public static int countgrades(char[]letterGrades,char Grade, int lent){
int count = 0;
for(int i =0;i<lent;i++){
if(letterGrades[i]==Grade){
count++; } }
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int lent;
System.out.print("Length: ");
lent = input.nextInt();
while(lent >30 || lent<1){
System.out.print("Invalid input\nLength: ");
lent = input.nextInt(); }
char[] letterGrades = new char[lent];
for(int i =0; i<lent;i++){
letterGrades[i] = input.next().charAt(0);
}
char[] letters = {'A','B','C','D','F'};
for(char i:letters){
System.out.println(i+": "+countgrades(letterGrades,i, lent));
}
}
}
Explanation:
See attachment where I I used comment to explain each lin e
Following are the code to the given question:
Program Explanation:
- Include header file.
- Defining a method "count_Char" that takes three variable in the parameter that are "g as character array, x as integer variable, and a character variable l".
- Inside the method two integer variable "i,s" is declared.
- In the next step, a for loop is declared that use if block that checks input value is in array and return incremented s value.
- In the next line, a main method is declared inside this two integer variable "i,x" and two character array is declared that inputs value, and define a loop that call the above method, and print its value.
Program:
#include <iostream>//header file
using namespace std;
int count_Char(char g[], int x, char l)//defining a method count_Char that takes 3 variable in parameter
{
int i,s=0;//defining integer variable
for(i=0;i<x;i++)//defining for loop that checks input value find in array
{
if(g[i]==l)//defining if block that check value is in array
{
s++;//incrementing s value by 1
}
}
return s;//return s value
}
int main()//defining main method
{
int i,x;//defining integer variable
char g[50],l[5]={'A','B','C','D','F'};//defining 2 character array
cout<<"Please input the number of grades to be read in.(1-50): ";//print message
cin>>x;//input integer value
cout<<"all grades must be upper case A B C D or F\n";//print message
for(i=0;i<x;i++)//defining loop that Input array value
{
cout<<"Input a grade: ";//print message
cin>>g[i];//input array value
}
for(i=0;i<5;i++)//defining loop that prints array value and prints its match value
{
cout<<"Number of "<<l[i]<<"s ="<<count_Char(g,x,l[i])<<endl;//prints array value with matched value
}
return 0;
}
Output:
Please find the attached file.
Learn more:
brainly.com/question/19512947
