Question & Answer: Help on simple C++ program……

Help on simple C++ program.

I am getting incorrect output compared to the sample provided and I’m not sure why.

#include <cstdio>
#include <iostream>
#include <iomanip>

using namespace std;

// length is the number of elements in the array
double findAverageScore(int scores[], int length ){
// create variable to store highest and lowest scores
int highest = 0;
int lowest;
double total;

for (int i = 0; i < length; i++) {
total += scores[i];

// Check to see if score is greater than the previous highest score
if (scores[i] >= highest) {
highest = scores[i];
}
// Check to see if score is less than the previous lowest score
if (scores[i] <= lowest) {
lowest = scores[i];
}
}

total = total – highest – lowest;
return total / (length – 2);
}

int main(){
int N;
int maxScore = 0;
int contestant;
int highestContestant;

printf(“Enter the number of judges: “);
cin >> N;
// Array of scores to be stored
int* scores = new int[N];

printf(“Number of judges entered: %dn”, N);

puts(“Enter contestant number:”);
cin >> contestant;

puts(“Enter score: “);
for (int i = 0; i < N; i++) {
cin >> scores[i];
}

double averageScore = findAverageScore(scores, N);

printf(“Average score: %dn”, averageScore);

return 0;
}

********************* . Program Details . *********************

1. Write a complete C++ program that takes the judges’ scores for a gymnastic event and finds each contestant’s score. It should satisfy all the following specifications: it first reads in an integer N that gives the number of judges. It then reads in a contestant number followed by N integers which are the scores given the contestant by the N judges. The scores should be read into an array, and then the program calls a function called findAverageScore that takes N and the array of judges’ scores as arguments. The function should find and drop the highest score and the lowest score, and then find and return the average of the remaining N-2 scores. The main should then print the contestant number and the average score s/he receives for the event to the screen. It will continue to do this (read in the contestant number, then the N integers, then print the contestant number and the calculated score on a new line) until a negative value is entered for the contestant number. The program will then print the contestant number who scored the highest. (You may assume no ties will occur.) All input will be from the keyboard and you may also assume that N will never be greater than 10 and never less than 3. You may format the output in any way as long as it is readable and correct, and you may assume all inputs are correct. (JUST WRITE CODE; NO DOCUMENTATION REQUIRED except for your NAME and ID at the start of the file!) Name your program prob1.cpp. Submit it to the EPP – Problem 1 section on the class Titanium site. If you wrote more than one file containing code, make sure to send and copy them as well. Remember to put in your name and ID at the start of each file. Sample output screen for a session (Bold numbers are printed by the program; normal numbers are typed in by the user.)

SAMPLE OUTPUT:

Number of Judges: 5

34 7 5 3 7 6

Contestant 34 6

28 . 8 5 8 10 7

Contestant 28 7.666666667

3 5 5 3 4 4

Contestant 3 4.333333333

−1

Contestant 28 had the highest score.

Expert Answer

 

Program:

Sample output:

Code to copy:

#include <iostream>

using namespace std;

// FUNCTION PROTOTYPES

double findAverageScore(int,int[]);

double findgimLowest(int,int[]);

double findgimHighest(int,int[]);

//MAIN FUNCTION

int main()

{

int N, CN=0, jScores[5];

double avgScore;

int topCN=0;

double topScore=0;

//Read the number of judges

cout << “Number of Judges: “;

cin >> N;

while (N < 3 || N>10)

{

cout << “N never be greater than 10 and never less than 3” << endl;

cin >> N;

}

while (true)

{

// ASKING THE USER TO ENTER CONTESTANT NUMBER & EACH JUDGE’s SCORE

cin >> CN;

if (CN < 0)

break;

//Read the scores into jScores array

for (int i = 0; i < 5; i++)

cin >> jScores[i];

//Call findAverageScore

avgScore = findAverageScore(N, jScores);

//PRINTING THE CONTESTANT MEMBERS

cout << “Contestant “;

cout << CN<< ” ” << avgScore<<endl;

if (avgScore > topScore)

{

topCN = CN;

topScore = avgScore;

}

}

cout << “Contestant ” << topCN << ” had the highest score.” << endl;

return 0;

}

double findAverageScore(int N,int scores[])

{

double rtValue=0;

double lwScre = findgimLowest(N,scores);

double hgScore = findgimHighest(N,scores);

for (int i = 0; i < N; i++)

rtValue = rtValue + scores[i];

rtValue = (rtValue – (lwScre + hgScore)) / 3;

return rtValue;

}

//FINDS THE LOWEST SCORE

double findgimLowest(int N,int scores[])

{

double lowestScore=scores[0];

for (int i = 0; i < N; i++)

{

if (scores[i] < lowestScore)

lowestScore = scores[i];

}

return lowestScore;

}

//FINDS THE HIGHEST SCORE

double findgimHighest(int N, int scores[])

{

double highScore = scores[0];

for (int i = 0; i < N; i++)

{

if (scores[i] > highScore)

highScore = scores[i];

}

return highScore;

}

Still stressed from student homework?
Get quality assistance from academic writers!