Reputation: 677
I'm pretty new to C++, only had experience in C#, Python, and JS so bear with me!
I am taking the user's input on 5 scores and storing it in an array. I pass the array off and evaluate the scores in the array to find the lowest value and highest value. I need to drop those two, then find the average of the other 3 values. My problem is, I'm not finding the highest/lowest value. I have the logic in findLowest() and findHighest() but to test it I put the same logic in main() to see if its working before its passed off, and its not working. Can someone guide me to finding the highest/lowest values in the array? Thanks!
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
void getJudgeData(double score)
{
cin >> score;
if (score > 10 || score < 0)
{
cout << "Invalid score, please enter again." << endl;
cin >> score;
}
else
{
cout << "Thank you." << endl;
}
}
double findLowest(double scores[])
{
double lowScore = *min_element(scores, scores+5);
return lowScore;
}
double findHighest(double scores[])
{
double highScore = *max_element(scores, scores+5);
return highScore;
}
double calcScore(double scores[])
{
double finalScore;
double sum = 0;
double newScore[3] = {};
double lowScore = findLowest(scores);
double highScore = findHighest(scores);
int j = 0;
for (int i=0; i < 5; i++)
{
if (scores[i] != lowScore && scores[i] != highScore)
{
scores[i] = newScore[j];
j++;
}
}
for (int k=0; k < 3; k++)
{
sum = sum + newScore[k];
}
finalScore = sum/3;
return finalScore;
}
int main()
{
double finalScore;
double judgeScores[5] = {};
for (int i = 0; i < 5; ++i)
{
cout << "Enter judge " << i + 1 << "'s score: ";
getJudgeData(judgeScores[i]);
}
finalScore = calcScore(judgeScores);
cout << "Highest score is: " << *max_element(judgeScores, judgeScores+5) << endl;
cout << "The final score is: " << finalScore << endl;
// This prevents the Console Window from closing during debug mode
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();
return 0;
}
Upvotes: 1
Views: 1066
Reputation: 17043
Change
void getJudgeData(double score)
to
void getJudgeData(double &score)
Upvotes: 4
Reputation: 40335
It looks like most of your logic is OK, except you have your assignment swapped in calcScore()
. You have:
scores[i] = newScore[j];
You probably meant:
newScore[j] = scores[i];
Also, be wary: If your input array contains multiple scores equal to the minimum or maximum, you will have less than 3 remaining after you remove them.
Edit: Oh yeah, and also what others have said about passing the value by reference to getJudgeData()
.
Upvotes: 2
Reputation: 726509
This is because getJudgeData
takes double score
by value. As the result, the entries by the end user remain confined to the getJudgeData
function; the judgeScores[i]
variable that you pass to the function remains unchanged.
Add an ampersand to fix this problem:
void getJudgeData(double &score) {
...
}
Now the parameter is passed by reference, letting getJudgeData
make modifications to it.
Upvotes: 6