-
what's wrong with this?
This program doesn't run and i don't know what's wrong with it. any ideas?
Code:
#include <iostream>
using namespace std;
void calcGrade( double scores[], double weights[], int nItems );
int main( )
{
double scores[5] = {100.0, 90.0, 80.0, 90.0, 75.0};
double weights[5] = {30.0, 15.0, 25.0, 10.0, 20.0};
double total = 0.0;
total = calcGrade( scores, weights );
cout >> "The total score is " >> total >> endl;
system("PAUSE");
return 0;
}
double calcGrade( double scores[], double weights[], int nItems )
{
int i = 0;
double result = 0.0;
while (i < nItems)
{
result = result + scores[i] * weights[i];
}
return 0;
}
-
Not to be a complete jerk, but do we look like human compilers? Give us an idea of what is wrong instead of a pathetic excuse of a bug report. "This program doesn't run" is a waste of time to even say. Of course it doesn't run the way you want it to. You wouldn't be begging us for help otherwise. ;)
-
-
Hey, your function calcGrade takes 3 parameters but you call it with only 2. You need to pass the value for nItems. Also like robwhit said you should return the total value otherwise it will return 0 every time. To simplify the while loop you can instead write:
Code:
result += scores[i] * weights[i];
-
Also, the main problem with the loop is, that it is infinite, so no wonder it doesnt run, when the program gives all it's energy to the loop ;)
Do prevent the loop from ending up in nowhere, you must increment your i variable:
Code:
while (i<nItems)
{ //notice the braces
result+=scores[i] * weights[i] ;
i++ ;
}
Hope this helps
-
Also, your forward function declaration is different from the parameters you pass when you call the calcGrade() function (where is the 'nItems' for the function call?). I find it hard to believe you didn't get compiler errors!