Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert to C++dotNet import java.io.*; import java.text.NumberFormat; // for num

ID: 3739718 • Letter: C

Question

Convert to C++dotNet

import java.io.*;

import java.text.NumberFormat; // for number formatting

public class ClassAverageBR
{
public static void main(String[] args) throws IOException
{
//declare and initialize variables
int score;
int sumOfScoresByStudent = 0;
int sumOfAllScores = 0;
double classAverage;

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

String studentName;
String scoreStr;
String studentNameMessage = "Enter the name of the student:";
String numOfStudentStr = "Enter the number of students: ";
String numOfScoresStr = "Enter the number of scores to be entered per student: ";
String outputTable = "Names, scores, average scores, and letter grades: ";

NumberFormat numFormatter = NumberFormat.getNumberInstance();
numFormatter.setMinimumFractionDigits(1);
numFormatter.setMaximumFractionDigits(1);

//get input from user
System.out.println(numOfStudentStr);
int numOfStudents = Integer.parseInt(dataIn.readLine());
System.out.println(numOfScoresStr);
int numOfScores = Integer.parseInt(dataIn.readLine());

//declare arrays
int[][] scores = new int[numOfStudents][numOfScores];
String[] students = new String[numOfStudents];
double[] avgScores = new double[numOfStudents];
char[] letterGrades = new char[numOfStudents];

//get more user input, load arrays, and process data
for (int i = 0; i<numOfStudents; i++) // i loop is for students
{
System.out.println(studentNameMessage);
studentName = dataIn.readLine();

students[i] = studentName;

//add student to output string
outputTable += students[i] + " Scores: ";


for (int j = 0; j<numOfScores; j++) // j loop is for scores
{


System.out.println("Enter score " + (j + 1) + " for " + students[i] + ": ");

score = Integer.parseInt(dataIn.readLine());
scores[i][j] = score; // assign score to array
outputTable += scores[i][j] + " "; // add score to output

sumOfScoresByStudent += score; // accumulate for student
sumOfAllScores += score; // accumulate for all students
}// end j loop

avgScores[i] = (double) sumOfScoresByStudent / numOfScores;
letterGrades[i] = findGrade(avgScores[i]); // call findGrade method

// add average and grade to output
outputTable += "Avg: " + numFormatter.format(avgScores[i]) + " " + "Grade: " + letterGrades[i] + " ";

sumOfScoresByStudent = 0; // reset accumulator for a new student
}//end i loop

classAverage = (double) sumOfAllScores / (numOfStudents* numOfScores);

outputTable += " Overall class average =" + numFormatter.format(classAverage);

// display output
System.out.println(outputTable);
}// end main

// method for finding letter grade
public static char findGrade(double averageScore)
{
char theGrade;

if (averageScore >= 90)
theGrade = 'A';
else if (averageScore >= 80)
theGrade = 'B';
else if (averageScore >= 70)
theGrade = 'C';
else if (averageScore >= 60)
theGrade = 'D';
else
theGrade = 'F';
return theGrade;
}// end findGrade
}// end class

Explanation / Answer

Converted code in C++

#include<iostream>
#include<cstring>
#include <sstream>
using namespace std;

class ClassAverageBR
{
public:
// method for finding letter grade
char findGrade(double averageScore)
{
char theGrade;
if (averageScore >= 90)
theGrade = 'A';
else if (averageScore >= 80)
theGrade = 'B';
else if (averageScore >= 70)
theGrade = 'C';
else if (averageScore >= 60)
theGrade = 'D';
else
theGrade = 'F';
return theGrade;
}// end findGrade
};// end class
  
int main()
{
//declare and initialize variables
ClassAverageBR obj;
int score;
int sumOfScoresByStudent = 0;
int sumOfAllScores = 0;
int numOfStudents;
int numOfScores;
double classAverage;
string studentName;
string scoreStr;
string studentNameMessage = "Enter the name of the student:";
string numOfStudentStr = "Enter the number of students: ";
string numOfScoresStr = "Enter the number of scores to be entered per student: ";
string outputTable = "Names, scores, average scores, and letter grades: ";

//get input from user
cout<<numOfStudentStr<<endl;;
cin>>numOfStudents;
cout<<numOfScoresStr<<endl;
cin>>numOfScores;

//declare arrays
int scores[numOfStudents][numOfScores];
string students[numOfStudents];
double avgScores[numOfStudents];
char letterGrades[numOfStudents];
std::ostringstream oss;
//get more user input, load arrays, and process data
for (int i = 0; i<numOfStudents; i++) // i loop is for students
{
cout<<studentNameMessage<<endl;
cin>>studentName;
students[i] = studentName;
//add student to output string
outputTable += students[i] + " Scores: ";
for (int j = 0; j<numOfScores; j++) // j loop is for scores
{
cout<<"Enter score "<<(j + 1)<<" for "<<students[i]<<": "<<endl;
cin>>score;
scores[i][j] = score; // assign score to array
outputTable += scores[i][j] + " "; // add score to output

sumOfScoresByStudent += score; // accumulate for student
sumOfAllScores += score; // accumulate for all students
}// end j loop

avgScores[i] = (double) sumOfScoresByStudent / numOfScores;
letterGrades[i] = obj.findGrade(avgScores[i]); // call findGrade method

// add average and grade to output
outputTable +="Avg: ";
oss<<avgScores[i];
outputTable +=oss.str();
outputTable +="Grade: ";
oss<<letterGrades[i];
outputTable +=oss.str();

sumOfScoresByStudent = 0; // reset accumulator for a new student
}//end i loop

classAverage = (double) sumOfAllScores / (numOfStudents* numOfScores);

outputTable += "Overall class average =";
oss<<classAverage;
outputTable +=oss.str();

// display output
cout<<outputTable<<endl;
return 0;
}// end main