Rewrite the following function so that it returns the same result, but does not increment the variable ptr. Your new program must not use any square brackets, but must use an integer variable to visit each double in the array. You may eliminate any unneeded variable.
double computeMean(const double* scores, int numScores)
{
const double* ptr = scores;
double tot = 0;
while (ptr != scores + numScores)
{
tot += *ptr;
ptr++;
}
return tot/numScores;
}
When I looked at this I thought easy, I will just declare a new variable i and set it to ptr and increment i but that does not compile. The only way I know to traverse arrays are through the [] operators and through pointer, both of which I cannot use here. Can anyone help?

New Topic/Question
Reply



MultiQuote




|