no output & standard deviation computation

closed account (zT4NhbRD)
I've got this code that's supposed to take some randomly generated scores and calculate the mean and standard deviation. It was working up until I put in the bit of code to calculate standard deviation. The code compiles fine but gives no output.

Note: for the standard deviation I'm not to use an array to calculate it; I have to get the code to loop once to calculate it.

Last edited on
Line 43 ....

write (==) instead of (=) ...
(deviationCount == 1)

And since you use rand() add to the headers ..
 
#include<ctime>   


and in main ..
1
2
3
4
5
6
7
8
int main() {
srand((unsigned int)time(0)) ;
    double totalScores = 0, totalMean = 0 , totalDeviation = 0;// Don't forget to initialize the variables .. it's a good practice ..
    printGradeRange(totalScores, totalMean, totalDeviation);
    system ("pause");
    return 0;  
}


closed account (zT4NhbRD)
That didn't solve my problem.

Still getting no output...
Hi,

Just wondering what the point of the getScore function is: I mean the whole function is equivalent to this:

generatedScore = (rand()%30+40);

The for loops don't achieve anything because i is not used. And the argument i is reset straight away.

You should declare all your functions at the start, not sure why you are doing it on line 59.

Also, try to keep the names of your parameters / arguments consistent - it's a little confusing when they aren't. That might be the reason for your problems: you send the arguments (line 60), but the parameters which are references have different names. So it is those references which have the updated values, not the ones in the calling function. Try making the names of the arguments and parameters the same, see if that fixes it.

Initialising variables is always a good idea, though be careful not reset them.

The calculation of the standard deviation should be in it's own function - functions should only do one thing.

The do loop on line 41 only runs once - is that what you wanted?

The pow function is very expensive when the exponent is an integer, because it uses a binomial series to do it, so when squaring just do a * a.

Hope this helps :+)

Hi,

How to find these problems yourself :+)

Learn to use a debugger - if using an IDE it should have one as part of the system. If not there are command line versions. One can have a watchlist of the values of variables - use this to deduce where things go wrong. This will save you days of staring at code :+D

Poor man's debugging consists of putting std::cout statements everywhere to keep track of the value of your variables.

:+)
closed account (zT4NhbRD)
Hi,

thanks so much for all the help! Unfortunately I'm still getting no output even after applying all the changes you suggested.

To answers your questions:
Honestly, the only purpose of getScore is because it's a required function of my assignment.
I'd put standard deviation in another function but it's also required that getStats computes both the mean and the standard deviation.
The loop on line 41 is only supposed to run once yes.

Last edited on
Hi,

Can you see how lines 22-25 don't achieve anything?

If the do loop only runs once, how does it calc SD?

How did you get on with the debugging?

Got to run ............

Edit:

You are resetting everything to 0.0 on line 44.
Last edited on
closed account (zT4NhbRD)
Okay, finally got it to output.

Last edited on
I tried debugging but nothing came up (I'm using xCode, btw).



Are you sure you know what debugging means? You have a watch list of variables, and you check on their values as you step through the code 1 line at a time. If there are logic or run time errors you should be able to find them. Debugging is not compiling.


Like I said, I'm not supposed to use arrays and am supposed to get the formula for it to loop once to compute it without the array.


I think to loop once means loop through all the values (not one of them).

I will say again : The getScore function is equivalent to the value of (rand()%30+40).

The loop continues until i = 0, and generatedScore = (rand()%30+40)

So the looping achieves nothing.

Do you know that you can put things onto multiple lines, like this:

1
2
3
4
5
6
7
void printGradeRange(double meanScore, 
                                double standardDeviation, 
                                double highestAdditionRange, 
                                double lowestAdditionRange, 
                                double lowestSubtractionRange, 
                                double highestSubtractionRange)
{


This makes it easier to read, which aids understanding.

I would have functions that return 1 value, rather than altering multiple values via references. That way functions can do one thing, which is how it should be.

Hope this helps a bit :+)
Topic archived. No new replies allowed.