Here is what I have #include <iostream> #include <iomanip> using namespace std;
ID: 3543868 • Letter: H
Question
Here is what I have
#include <iostream>
#include <iomanip>
using namespace std;
void createHeading(int n) {
cout << " ";
for (int i = 0; i < n; i++)
cout << " Exam " << (i + 1);
cout << " Average" << endl;
}
void printLine(int n) {
cout << " ";
for (int i = 0; i < (n + 1); i++)
cout << " ---------";
cout << endl;
}
void displayLowestScore(int** p_int, int n1, int n2) {
cout << setw(11) << left << "Lowest";
int lowest;
for (int j = 0; j < n2; j++) {
lowest = p_int[0][j];
for (int i = 0; i < n1; i++) {
if (lowest > p_int[i][j])
lowest = p_int[i][j];
}
cout << setw(11) << right << lowest;
}
}
void displayHighestScore(int** p_int, int n1, int n2) {
cout << setw(11) << left << "Highest";
int highest;
for (int j = 0; j < n2; j++) {
highest = p_int[0][j];
for (int i = 0; i < n1; i++) {
if (p_int[i][j] > highest)
highest = p_int[i][j];
}
cout << setw(11) << right << highest;
}
}
void displayAverage(int** p_int, int n1, int n2) {
int sum;
cout << setw(11) << left << "Average";
for (int j = 0; j < n2; j++) {
sum = 0;
for (int i = 0; i < n1; i++) {
sum = sum + p_int[i][j];
}
cout << setprecision(2) << fixed << setw(11) << right <<
static_cast<double>(sum) / n1;
} cout << endl;
}
void displayStudentRecords(int** p_int, int n1, int n2) {
int sum;
for (int i = 0; i < n1; i++) {
cout << "Student " << (i + 1) << " ";
sum = 0;
for (int j = 0; j < n2; j++) {
cout << setw(11) << p_int[i][j];
sum = sum + p_int[i][j];
}
cout << setprecision(2) << fixed << setw(11) << static_cast<double>(sum) / n2;
cout << endl;
}
}
int** createTable(int** p_int, int* row, int* column) {
cout << " Enter the number of students: ";
cin >> *row;
p_int = new int*[*row];
cout << "Enter the number of tests: ";
cin >> *column;
for (int i = 0; i < *row; i++) p_int[i] = new int[*column];
return p_int;
}
void enterRecords(int** p_int, int row, int column) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cout << "Enter Student " << (i + 1) << "'s " << "Exam " << (j + 1) << "
score: ";
cin >> p_int[i][j];
}
}
}
int main()
{
int numberOfStudents;
int numberOfTests;
int** p_Students = 0;
char check = '';
do {
p_Students = createTable(p_Students, &numberOfStudents, &numberOfTests);
enterRecords(p_Students, numberOfStudents, numberOfTests);
createHeading(numberOfTests);
printLine(numberOfTests);
displayStudentRecords(p_Students, numberOfStudents, numberOfTests);
printLine(numberOfTests);
displayAverage(p_Students, numberOfStudents, numberOfTests);
displayHighestScore(p_Students, numberOfStudents, numberOfTests);
cout << endl;
displayLowestScore(p_Students, numberOfStudents, numberOfTests);
while (!(check == 'q' || check == 'c')) {
cout << " Enter c to continue or q to quit: ";
cin >> check;
}
for (int i = 0; i < numberOfStudents; i++) delete [] p_Students[i];
delete [] p_Students;
} while (check == 'c' || check != 'q');
return 0;
}
Explanation / Answer
Here is your corrected code: Please choose as Best Answer and comment if you need more help.
The error was on line 77: 'score' was on a new line when it should be part of the string.
#include <iostream>
#include <iomanip>
using namespace std;
void createHeading(int n)
{
cout << " ";
for (int i = 0; i < n; i++)
cout << " Exam " << (i + 1);
cout << " Average" << endl;
}
void printLine(int n)
{
cout << " ";
for (int i = 0; i < (n + 1); i++)
cout << " ---------";
cout << endl;
}
void displayLowestScore(int** p_int, int n1, int n2)
{
cout << setw(11) << left << "Lowest";
int lowest;
for (int j = 0; j < n2; j++)
{
lowest = p_int[0][j];
for (int i = 0; i < n1; i++)
{
if(lowest > p_int[i][j])
lowest = p_int[i][j];
}
cout << setw(11) << right << lowest;
}
}
void displayHighestScore(int** p_int, int n1, int n2)
{
cout << setw(11) << left << "Highest";
int highest;
for (int j = 0; j < n2; j++)
{
highest = p_int[0][j];
for (int i = 0; i < n1; i++)
{
if(p_int[i][j] > highest)
highest = p_int[i][j];
}
cout << setw(11) << right << highest;
}
}
void displayAverage(int** p_int, int n1, int n2)
{
int sum;
cout << setw(11) << left << "Average";
for (int j = 0; j < n2; j++)
{
sum = 0;
for (int i = 0; i < n1; i++)
{
sum = sum + p_int[i][j];
}
cout << setprecision(2) << fixed << setw(11) << right
<< static_cast<double> (sum) / n1;
}
cout << endl;
}
void displayStudentRecords(int** p_int, int n1, int n2)
{
int sum;
for (int i = 0; i < n1; i++)
{
cout << "Student " << (i + 1) << " ";
sum = 0;
for (int j = 0; j < n2; j++)
{
cout << setw(11) << p_int[i][j];
sum = sum + p_int[i][j];
}
cout << setprecision(2) << fixed << setw(11) << static_cast<double> (sum)
/ n2;
cout << endl;
}
}
int** createTable(int** p_int, int* row, int* column)
{
cout << " Enter the number of students: ";
cin >> *row;
p_int = new int*[*row];
cout << "Enter the number of tests: ";
cin >> *column;
for (int i = 0; i < *row; i++)
p_int[i] = new int[*column];
return p_int;
}
void enterRecords(int** p_int, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << "Enter Student " << (i + 1) << "'s " << "Exam " << (j + 1)
<< " score: ";
cin >> p_int[i][j];
}
}
}
int main()
{
int numberOfStudents;
int numberOfTests;
int** p_Students = 0;
char check = '';
do
{
p_Students = createTable(p_Students, &numberOfStudents, &numberOfTests);
enterRecords(p_Students, numberOfStudents, numberOfTests);
createHeading(numberOfTests);
printLine(numberOfTests);
displayStudentRecords(p_Students, numberOfStudents, numberOfTests);
printLine(numberOfTests);
displayAverage(p_Students, numberOfStudents, numberOfTests);
displayHighestScore(p_Students, numberOfStudents, numberOfTests);
cout << endl;
displayLowestScore(p_Students, numberOfStudents, numberOfTests);
while (!(check == 'q' || check == 'c'))
{
cout << " Enter c to continue or q to quit: ";
cin >> check;
}
for (int i = 0; i < numberOfStudents; i++)
delete[] p_Students[i];
delete[] p_Students;
}
while (check == 'c' || check != 'q');
return 0;
}