Reputation: 51
I am working on a beginning textbook c++ assignment. It is a basic calculation from an array that drops the highest and lowest scores. Every time I run it, I get 0 as the answer. I'm assuming the issue is in main or calculate score. I appreciate anyone who is willing to take a couple mins to look at this.
#include <iostream>
using namespace std;
void printHeader(int judges);
void enterData (float scores[], int judges, float difficulty);
float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least);
int indexofLeast(float scores[], const int judges);
int indexofMax(float scores[], const int judges);
int main () {
const int judges = 7;
float scores [judges];
float difficulty = 0;
int maxScore = indexofMax(scores, judges);
int least = indexofLeast(scores, judges);
float finalscore = calculateScore(scores, judges, difficulty, maxScore, least);
printHeader (judges);
enterData (scores, judges, difficulty); // get user input
indexofLeast(scores, judges); // find lowest score
indexofMax(scores, judges); // find highest score
calculateScore (scores, judges, difficulty, maxScore, least); // get final score
cout << "The final score is " << finalscore << '\n';
return 0;
}
void printHeader(const int judges) {
cout << "This program calculates a divers score over" << judges << "judges" << endl;
}
void enterData(float scores[], const int judges, float difficulty) {
for (int i = 0; i < judges; i++){
cout <<"Enter score for judge " << i+1 << endl;
cin >> scores[i];
}
cout << "Enter difficulty: "<< endl;
cin >> difficulty;
}
float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least) {
float sum = 0;
for (int i = 0; i < judges; i++) {
sum += scores[i];
}
return (sum - scores[least] - scores[maxScore]) * difficulty * 0.6;
}
int indexofLeast(float scores[], const int judges) {
int least = 0;
for (int i = 1; i< judges; i++) {
if (scores[i] < scores[least])
least = i;
}
return least;
}
int indexofMax(float scores[], const int judges) {
int maxScore = 0;
for (int i = 1; i< judges; i++) {
if (scores[i] > scores[maxScore])
maxScore = i;
}
return maxScore;
}
For expected input I entered 7 scores, (between 0 and 10): 1, 2, 2, 4, 5, 8, 10 So drop highest and lowest 1 and 10. So sum = 21 Degree of difficulty between 1.2 and and 4.0: 3.0 So 21 * 3.0 * 0.6 = 37.8 for expected input/ output I actually get -0
Upvotes: 0
Views: 169
Reputation: 24269
To expand on previous answers, I think you have fallen into a common error of understanding how '=' works in C/C++, often picked up from thinking of variables too much like algebra.
int maxScore = indexofMax(scores, judges);
This does not describe what "maxScore" is or will be, it assigns it a value. Consider #include
int f(int n); // some mathematical function.
int main(int argc, const char** argv)
{
int a = 1;
int b = f(a);
a = 15;
std::cout << "b = " << b << std::endl;
return 0;
}
int f(int n) { return n * 2; } // f(n) = 2f
This program will print "2", not not "30".
int b = f(a);
// is equivalent to
int b;
b = f(a);
in both cases, b
has been assigned a value, based on the evaluation of f(a) at the time. Changing 'a' beyond this point has no effect.
Upvotes: 0
Reputation: 63481
You need to pass difficulty
as a reference into enterData
.
void enterData(float scores[], const int judges, float &difficulty)
^
The problem is that you expect it to contain the value that the user entered. You initialise it to zero, then call enterData
. But when that function returns, the value is still zero because the parameter was a local variable. Later, you multiply your result by difficulty
, so the result is always zero.
Using a reference to return the user-inputted value should fix your problem. Syntactically, the only thing you need to change is to add the &
to the function definition as I've shown. You still call the function in the same way.
I urge you to develop the skills to critically analyse and test your code by yourself, rather than asking people. If you thought the issue was in the score calculation, you could have easily displayed all the values that go into the score calculation, and worked backwards from there... ie "why is difficulty zero? at what point does it become zero?"
To address your issue in the comments, your code is doing stuff in the wrong order. You need to assign the return values after the user enters data:
const int judges = 7;
float scores [judges];
float difficulty = 0;
printHeader (judges);
enterData (scores, judges, difficulty); // get user input
int maxScore = indexofMax(scores, judges);
int least = indexofLeast(scores, judges);
float finalscore = calculateScore(scores, judges, difficulty, maxScore, least);
Upvotes: 2
Reputation: 1350
because you set difficulty to zero:
float difficulty = 0;
and then in the function you declear
void enterData(float scores[], const int judges, float difficulty)
which mean you get it by value. so the value is not changing and you stay with zero.
you need to pass a refernce to difficulty:
void enterData(float scores[], const int judges, float &difficulty)
Upvotes: 2
Reputation: 247
you are multiplying by difficulty which is never changed from being 0. If you want to change difficulty, change the prototype and definition of enterData to the following
void enterData(float scores[], const int judges, float &difficulty)
See http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ for a more detailed explanation of passing by reference.
Upvotes: 2