I could not find the lowest average - c++

I am trying to find the lowest student average in a golf game. but I could not find out how. all my attempts ended up finding the highest average which is the opposite of what I want to display.
*
*
*
*
*
*
*
*
*
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double total = 0; double totalr = 0;
const int round = 3; double less=0;
const int player = 5;
string name[player]={"Mike Sims ", "Paula Hill ", "Steve Jones", "Joll Burton", "Lee Smith "};
double average=0, avestudent, averound; string dummy;
int score[round][player] =
{ 78, 71, 72, 73, 74 , 76, 78, 75, 74, 79 , 74, 75, 73, 72, 78 };
cout << "Player Round1 Round2 Round3 Ave./Student"<<endl;
for (int i = 0; i <player ; i++)
{
cout << setw(10) << left << name[i]; total = 0;
for (int j = 0; j <round ; j++)
{
cout << setw(16) << right << score[j][i];
total = total + score[j][i]; avestudent = total / (int)round;
}
cout << fixed << setprecision(1);
cout <<setw(20)<< avestudent ;
cout << endl;
}
cout << endl;
cout << setw(10) << left << "Ave./Round ";
for (int i = 0; i <round; i++)
{
totalr = 0;
for (int j = 0; j <player; j++)
totalr = totalr + score[i][j];
averound = totalr / player;
cout << setw(13)<< " "<<averound;
}
cout<< endl;
double lowest = score[round][player];
for (int j = 0; j < round; j++)
{
lowest = 0;
for (int i = 0; i < player; i++)
{
avestudent = total / (int)round;
lowest = avestudent;
if (score[j][i] < lowest)
lowest = score[j][i];
}
}
cout << lowest << " was the best recorded student average from the rounds of golf." << endl;
cout << fixed << setprecision(1) << endl;
}

Honestly I'm not exactly sure what your logic/ workflow is in trying to find the lowest average. In this case, I would just create an array to save the different averages. (So change your avestudent variable into an array)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
double total = 0;
double totalr = 0;
const int round = 3;
double less=0;
const int player = 5;
string name[player]={"Mike Sims ", "Paula Hill ", "Steve Jones", "Joll Burton", "Lee Smith "};
double average=0, averound; string dummy;
double avestudent[player];
int score[round][player] = { 78, 71, 72, 73, 74 , 76, 78, 75, 74, 79 , 74, 75, 73, 72, 78 };
cout << "Player Round1 Round2 Round3 Ave./Student"<<endl;
for (int i = 0; i <player ; i++)
{
cout << setw(10) << left << name[i];
total = 0;
for (int j = 0; j <round ; j++)
{
cout << setw(16) << right << score[j][i];
total = total + score[j][i];
avestudent[i] = total / (int)round;
}
cout << fixed << setprecision(1);
cout <<setw(20)<< avestudent[i] ;
cout << endl;
}
cout << endl;
cout << setw(10) << left << "Ave./Round ";
for (int i = 0; i <round; i++)
{
totalr = 0;
for (int j = 0; j <player; j++)
{
totalr = totalr + score[i][j];
}
averound = totalr / player;
cout << setw(13)<< " "<<averound;
}
cout<< endl;
double lowest = score[round][player];
for (int j = 0; j < round; j++)
{
lowest = avestudent[0];
for (int i = 1; i < player; i++)
{
if (avestudent[i] < lowest)
{
lowest = avestudent[i];
}
}
}
cout << lowest << " was the best recorded student average from the rounds of golf." << endl;
cout << fixed << setprecision(1);
cout << endl;
}

The big issue with your code is that you're trying to initialize lowest to score[round][player]. You can't do this because these are the sizes of the array.
If there is an array int array[5]; you cannot access array[5]. The highest index that is reachable is array[4] because the array uses spaces 0,1,2,3,4 to hold the five integers.

Here's what I came up with:
int sums[player] = {};
int lowest_score = 0;
int lowest_player = 0;
double lowest_avg = 0.0;
for (int r = 0; r < round; r++)
{
for (int p = 0; p < player; p++)
{
sums[p] += score[r][p];
}
}
for (int p = 0; p < player; p++)
{
if ((p == 0) || (sums[p] < lowest_score))
{
lowest_player = p;
lowest_score = sums[p];
}
}
lowest_avg = ((double)lowest_score) / round;
std::cout << "The player with the lowest average score is " << name[lowest_player] << ". With an average score of " << lowest_avg << std::endl;

Related

Failing to properly print multidimensional array column?

I recently have been having trouble with this simple problem, at the end of the code there are two blocks of text dedicated to finding the highest/lowest grade from what the user inputted.
That was simple enough and it works well, but I also need to print the a row corresponding to the lowest/highest graded student.
Essentially I'm working with the marks[5][4] array, the first array being the students, the second array being the exams.
If the lowest graded student was the student on array [2][] then I would I want to print out [2][i] using a for loop.
For the second array it works fine, the problem is I'm having problem on how to link the lowest graded student to the first array link.
I tried to use the count1 and count loops but they return a mess. I have been at this for a few hours and need a nudge in the right direction.
Thank you very much.
#include<iostream>
using namespace std;
int main()
{
char grade[5];
double avg[5];
string name[5];
int marks[5][4];
for (int i = 0; i < 5; i++) {
cout << "\n enter student" << i + 1 << "name :";
cin >> name[i];
cout << "\n enter four subject marks:";
for (int j = 0; j < 4; j++) {
cin >> marks[i][j];
}
}
for (int i = 0; i < 5; i++)
{
int sum = 0;
for (int j = 0; j < 4; j++) {
sum = sum + marks[i][j];
}
avg[i] = sum / 4;
if (avg[i] > 90.0 && avg[i] <= 100.0) {
grade[i] = 'A';
}
else if (avg[i] > 80.0 && avg[i] <= 90.0) {
grade[i] = 'B';
}
else if (avg[i] > 70.0 && avg[i] <= 80.0) {
grade[i] = 'C';
}
else if (avg[i] > 60.0 && avg[i] <= 70.0) {
grade[i] = 'D';
}
else if (avg[i] <= 60.0) {
grade[i] = 'F';
}
}
for (int i = 0; i < 5; i++)
{
cout << "\nName :" << name[i];
cout << "\tAverage : " << avg[i];
cout << "\tGrade : " << grade[i];
}
for (int i = 0; i < 4; i++)
{
int sum2 = 0;
for (int j = 0; j < 5; j++) {
sum2 = sum2 + marks[j][i];
}
cout << "\n";
cout << "The average of each exam " << i+1 << " is " << "%" << sum2 / 5;
}
//LOWEST
int count1;
int lowest;
lowest = avg[0];
for (count1 = 1; count1 < 5; count1++)
{
if (avg[count1] < lowest)
lowest = avg[count1];
}
cout << "\n";
cout << "The lowest average grade is is: " << lowest << ".\n";
for (int i = 0; i < 4; i++) {
cout << marks[count1][i] << " ";
}
cout << endl;
//Ends lowest.
//HIGHEST
int count;
int highest;
highest = avg[0];
for (count = 1; count < 5; count++)
{
if (avg[count] > highest)
highest = avg[count];
}
cout << "\n";
cout << "The highest average grade is is: " << highest << ".\n";
for (int i = 0; i < 4; i++) {
cout << marks[count][i] << " ";
}
cout << endl;
//Ends highest.
return 0;
}
From what I understand, you need to link the name with the lowest avg student
I recommend that instead of initializing lowest and highest with the average, initialize it with the index of the lowest and highest average. You can then compare the averages in the for loop like this
if (avg[lowest] >= avg[count1])
lowest = count1;
or you can follow Sam's solution, create another variable to store the index of the current lowest and highest avg

How can I merge three functions into one? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have written next code but 3 functions must be replaced by 1 and I don't know how to.
The program creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Here's the code:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j++)
{
max = array[0][0];
for (int i = 0; i < k; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j++)
{
max = arr[0][0];
for (int i = 0; i < b; i++) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j++)
{
max = ar[0][0];
for (int i = 0; i < u; i++) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.
Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j++)
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}

2D Array. Calculating the average of the student

I can't figure out why I can't calculate the average. The output of the average is same as the other look the details in the picture below. My teacher told me to return the AVG to 0, but I don't know where to put it.
#include <iostream>
using namespace std;
const int ROW = 3;
const int COL = 3;
const int D = 3;
void display(int nums[ROW][COL]);
void show(int ID[D]);
int main()
{
int nums[ROW][COL];
int ID[D];
double AVG = 0;
for (int i = 1; i <= ROW; i++) {
cout << "Enter student ID[" << i << "]: ";
cin >> ID[i];
for (int j = 0; j < COL; j++) {
cout << "Enter student[" << i << "] Grades [" << j + 1 << "]:";
cin >> nums[i][j];
AVG = AVG + nums[i][j];
}
AVG = AVG / 3;
}
for (int i = 1; i <= ROW; i++) {
cout << "Student ID: " << ID[i] << endl;
for (int j = 0; j < COL; j++) {
cout << "Score: " << nums[i][j] << endl;
}
cout << "Average: " << AVG << endl;
}
return 0;
}
You Have some problem regarding this issues:
you take AVG as double but other variable as integer.so there is a datatype error.
2.you have problem doing array indexing.
Here Is the Solution. Here I declare AVG as an integer type array so that it remains same as your code.
#include
using namespace std;
const int ROW = 3;
const int COL = 3;
const int D = 3;
void display(int nums[ROW][COL]);
void show(int ID[D]);
int main ()
{
int nums[ROW][COL];
int ID[D];
int AVG[ROW];
for(int i=1; i<=ROW; i++)
{
cout << "Enter student ID[" << i <<"]: ";
cin>>ID[i];
AVG[i-1]=0;
for(int j=0; j<COL; j++)
{
cout<<"Enter student[" << i << "] Grades [" <<j+1<<"]:" ;
cin>>nums[i-1][j];
AVG[i-1] = AVG[i-1] + nums[i-1][j];
}
AVG[i-1] = AVG[i-1] / 3;
}
for(int i=1; i<=ROW; i++)
{
cout <<"Student ID: " << ID[i] << endl;
for(int j=0; j<COL; j++)
{
cout<<"Score: "<<nums[i-1][j]<<endl;
}
cout << "Average: " << AVG[i-1]<< endl;
}
return 0;
}
Currently you are calculating the average as follows;
First iteration:
(90 + 90 + 90) / 3 = 90
Second iteration:
(90 + 80 + 80 + 80)/3 = 110
Third iteration:
(110 + 70 + 70 + 70)/3 = 106
If you want to get the average of all grades, do this;
double AVG = 0;
double avg_tmp;
for (int i = 1; i <= ROW; i++) {
avg_tmp = 0;
cout << "Enter student ID[" << i << "]: ";
cin >> ID[i];
for (int j = 0; j < COL; j++) {
cout << "Enter student[" << i << "] Grades [" << j + 1 << "]:";
cin >> nums[i][j];
avg_tmp += nums[i][j];
}
AVG += avg_tmp / 3;
}
Your biggest issue is you invoke Undefined Behavior with your loop limits for for (int i = 1; i <= ROW; i++). In C/C++ all array indexes are Zero-Based with valid indexes of 0 <= i < n. So your i loop must be for (int i = 0; i < ROW; i++).
You do NOT need to compute the sum or average in your first set of loops. The first set of nested loops are you Input Loops where you are simply filling the elements of ID and nums. You only need AVG in your second set of nested loops (e.g. your Output Loops) Avoid mixing input and output logic.
Only declare and initialize your variables in the scope where they are needed. This will fix what your professor is talking above. If you only declare and initialize AVG within the first output-loop where the average will be computed, it will automatically be reset to zero for each student.
Putting it altogether, you could do:
#include <iostream>
const int ROW = 3; /* good job on declaring constants */
const int COL = 3;
const int D = 3;
int main()
{
int nums[ROW][COL] = {{0}}; /* initialize all plain-old arrays */
int ID[D] = {0};
for (int i = 0; i < ROW; i++) {
std::cout << "\nEnter student ID[" << i + 1 << "]: ";
/* you must validate EVERY user-input */
if (!(std::cin >> ID[i])) {
std::cerr << "error: invalid integer input.\n";
return 1;
}
for (int j = 0; j < COL; j++) {
if (!j)
std::cout.put('\n');
std::cout << "Enter student[" << i+1 << "] Grades [" << j+1 << "]: ";
if (!(std::cin >> nums[i][j])) { /* ditto */
std::cout << "error: invalid integer input.\n";
return 1;
}
}
}
for (int i = 0; i < ROW; i++) {
double AVG = 0.; /* declare / initialize in scope needed */
std::cout << "\nStudent ID: " << ID[i] << '\n';
for (int j = 0; j < COL; j++) {
std::cout << " Score: " << nums[i][j] << '\n';
AVG += nums[i][j]; /* sum each score */
}
std::cout << "Average: " << AVG / COL << '\n';
}
}
Example Use/Output
$ ./bin/studentavg
Enter student ID[1]: 1
Enter student[1] Grades [1]: 90
Enter student[1] Grades [2]: 89
Enter student[1] Grades [3]: 91
Enter student ID[2]: 2
Enter student[2] Grades [1]: 79
Enter student[2] Grades [2]: 81
Enter student[2] Grades [3]: 80
Enter student ID[3]: 3
Enter student[3] Grades [1]: 71
Enter student[3] Grades [2]: 70
Enter student[3] Grades [3]: 69
Student ID: 1
Score: 90
Score: 89
Score: 91
Average: 90
Student ID: 2
Score: 79
Score: 81
Score: 80
Average: 80
Student ID: 3
Score: 71
Score: 70
Score: 69
Average: 70
Look things over and let me know if you have further questions.
package test;
import java.util.*;
public class DoubleArray {
public static void main(String[] args) {
String[][] arr = {{"Ram", "100"}, {"Mohan", "80"}, {"Ram", "70"}, {"Mohan", "60"}, {"Chitra", "80"}, {"Ram", "88"}};
HashMap<String, Integer> pf = new HashMap<String, Integer>();
List<String> as = new ArrayList<>();
for (int row = 0; row < arr.length; row++) {
if (pf.containsKey(arr[row][0])) {
as.add(arr[row][0]);
pf.put(arr[row][0], ((pf.get(arr[row][0]) + Integer.parseInt(arr[row][1]))));
} else {
as.add(arr[row][0]);
pf.put(arr[row][0], Integer.parseInt(arr[row][1]));
}
}
int value = 0;
for (Map.Entry<String, Integer> entry : pf.entrySet()) {
pf.put(entry.getKey(), entry.getValue() / Collections.frequency(as, entry.getKey()));
value = Collections.max(pf.values());
if (entry.getValue() == value) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
}
}
}

Heap Corruption Detected: after normal block c++ dynamic 2D array

I keep getting this error Heap Corruption Detected: after normal block... It only comes up if I try to deallocate memory of my 2D array examScores using delete[]. I used a for loop to delete each array so I'm confused, is there still a memory leak somewhere? I'm not super familiar with memory management so thank you in advance for any help
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string getGradeOfExam(double grade, double examAverage) {
if (grade <= (examAverage - 15)) return "E";
if (grade >= (examAverage + 15)) {
return "A";
}
if (grade < (examAverage - 5)) return "D";
if (grade > (examAverage + 5)) return "B";
return "C";
}
string readNextName(ifstream& inputStream) {
string firstName;
string lastName;
inputStream >> firstName;
inputStream >> lastName;
string fullName = firstName + " " + lastName;
return fullName;
}
int readNextExamScore(ifstream& inputStream) {
int examGrade;
inputStream >> examGrade;
return examGrade;
}
int main(int argc, char* argv[]) {
if (argc < 3)
{
cerr << "Please provide name of input and output files";
return 1;
}
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 2;
}
cout << "Output file: " << argv[2] << endl;
ofstream out(argv[2]);
if (!out)
{
in.close();
cerr << "Unable to open " << argv[2] << " for output";
return 3;
}
int numStudents = 0;
int numExams= 0;
in >> numStudents >> numExams;
string* studentNames = new string[numStudents];
//first value corresponds to a specific student, second value corresponds to a specific exam
int** examScores = new int* [numStudents];
for (int i = 0; i < numExams; ++i) {
examScores[i] = new int[numExams];
}
for (int i = 0; i < numStudents; i++) {
studentNames[i] = readNextName(in);
for (int j = 0; j < numExams; j++) {
examScores[i][j] = readNextExamScore(in);
}
}
//Finds averages of all exams and stores them in an array
double* examAverages = new double[numExams];
for (int i = 0; i < numExams; i++) {
double examAverage = 0.0;
double totalExamScore = 0.0;
for (int j = 0; j < numStudents; j++) {
totalExamScore += examScores[j][i];
}
examAverage = totalExamScore / numStudents;
examAverages[i] = examAverage;
}
double totalExamScore = 0.0;
double totalExamAverage = 0.0;
for (int i = 0; i < numExams; i++) {
totalExamScore += examAverages[i];
}
totalExamAverage = totalExamScore / numExams;
double* studentAverages = new double[numStudents];
for (int i = 0; i < numStudents; i++) {
double studentTotalScore = 0.0;
for (int j = 0; j < numExams; j++) {
studentTotalScore += examScores[i][j];
}
studentAverages[i] = studentTotalScore / numExams;
}
//OUTPUT
out << "Student Scores:" << endl;
for (int i = 0; i < numStudents; i++) {
out << right << setw(6) << studentNames[i] << "\t";
for (int j = 0; j < numExams; j++) {
out << examScores[i][j] << "\t";
}
out << endl;
}
out << endl;
out << "Exam Averages:" << endl;
for (int i = 0; i < numExams; i++) {
out << setw(6) << "Exam " << i + 1 << " average = " << fixed << setprecision(1) << examAverages[i] << endl;
}
out << endl;
out << "Student Exam Grades:" << endl;
for (int i = 0; i < numStudents; i++) {
out << right << setw(6) << studentNames[i] << "\t";
for (int j = 0; j < numExams; j++) {
out << examScores[i][j] << " ";
out << "(" << getGradeOfExam(examScores[i][j], examAverages[j]) << ")\t";
}
out << endl;
}
out << endl;
out << "Exam Grades:" << endl;
for (int i = 0; i < numExams; i++) {
int gradeCount[5] = { 0,0,0,0,0 };
for (int j = 0; j < numStudents; j++) {
if (getGradeOfExam(examScores[j][i], examAverages[i]) == "A") {
gradeCount[0]++;
}
else if (getGradeOfExam(examScores[j][i], examAverages[i]) == "B") {
gradeCount[1]++;
}
else if (getGradeOfExam(examScores[j][i], examAverages[i]) == "C") {
gradeCount[2]++;
}
else if (getGradeOfExam(examScores[j][i], examAverages[i]) == "D") {
gradeCount[3]++;
}
else if (getGradeOfExam(examScores[j][i], examAverages[i]) == "E") {
gradeCount[4]++;
}
}
out << setw(6) << "Exam " << i + 1 << "\t" << fixed << setprecision(1) << examAverages[i] << "\t";
out << gradeCount[0] << "(A)" << "\t";
out << gradeCount[1] << "(B)" << "\t";
out << gradeCount[2] << "(C)" << "\t";
out << gradeCount[3] << "(D)" << "\t";
out << gradeCount[4] << "(E)" << endl;
}
out << endl;
out << "Student final grades:" << endl;
for (int i = 0; i < numStudents; i++) {
out << studentNames[i] << "\t" << studentAverages[i] << "(" << getGradeOfExam(studentAverages[i], totalExamAverage) << ")" << endl;
}
out << "Class Average Score: " << totalExamAverage << endl;
delete[] studentNames;
for (int i = 0; i < numStudents; i++)
{
delete[] examScores[i];
}
delete[] examScores;
delete[] examAverages;
delete[] studentAverages;
return 0;
}
My input file contains the following:
6 8
Cody Coder 84 100 100 70 100 80 100 65
Harry Houdini 77 68 65 100 96 100 86 100
Harry Potter 100 100 95 91 100 70 71 72
Mad Mulligun 88 96 100 90 93 100 100 100
George Washington 100 72 100 76 82 71 82 98
Abraham Lincoln 93 88 100 100 99 77 76 93
This part seems suspicious:
int** examScores = new int* [numStudents];
for (int i = 0; i < numExams; ++i) {
examScores[i] = new int[numExams];
}
The array examScores has numStudents elements, but the upper bound in the for-loop is numExams.
Errors of this kind can be often localized using the Valgrind tool. (It worked in this case.) Check it out.

I'm trying to make my 2-D Array program to get the largest element in each row but its not working

This is my code
#include <iostream>
using namespace std;
const int rows = 2;
const int cols = 6;
void CheckRow(int x[][6], int rows){
int highestr = 0;
int r = 0, c = 0;
for (r = 0; r < rows; ++r){
cout << "ROW " << r + 1 << ": ";
for (c = 0; c < cols; ++c)
cout << x[r][c] << " ";
cout << endl;
}
> End of PRINTING THE CONTENTS
> THE CODE BELOW MUST CHECK FOR THE GREATEST VALUE IN A ROW, BUT IT WONT GIVE ME THE EXACT RESULTS AS IT SHOULD DISPLAY
for (r = 0; r < rows; ++r){
for (c = 0; c < cols; ++c)
if (x[r][c] > highestr){
highestr += x[r][c];
cout << " Greatest Number: " << highestr;
cout << endl;
}
}
}
int main()
{
int temps[rows][cols] =
{
{ 12, 13, 14, 15, 16, 17 },
{ 18, 19, 20, 21, 22, 23 }
};
CheckRow(temps, 2);
cout << endl << endl;
system("pause");
return 0;
}
for (r = 0; r < rows; ++r)
{
for (c = 0; c < cols; ++c)
{
if (x[r][c] > highestr)
{
highestr = x[r][c];
}
}
cout << " Greatest Number: " << highestr;
cout << endl;
highestr=0; // considering every element in your 2D array is greater than 0
}