Monday, July 27, 2009

C++, lowest test score drop question?

Writing a program where user inputs 5 scores %26amp; lowest score drops. Has to be in 2 seperate functions. I have getValue function done ok; user inputs 5 scores as array of type float. When I move on to the second function (findLowest), I don't know how to transfer the scores found in getValues. No global variables, %26amp; I think I'm supposed to use call by reference, but I'm confused at how you would take the array from getValues and do a call by reference to get the scores into findLowest.





# include %26lt;iostream%26gt;


using namespace std;





void getValues();


void findLowest();








int main()


{


getValues();


findLowest();


return 0;


}





void getValues()


{


const int size = 5;





float score[size];





cout%26lt;%26lt; "Enter " %26lt;%26lt; size%26lt;%26lt; " scores, seperated with a space: \n";


cin%26gt;%26gt; score[0];





findLowest();


}





void findLowest()


{


int i;


float lowscore;





lowscore = score[0];


for (i=1; i%26lt;5; i++)


{


cin%26gt;%26gt;score[i];


if (score[i] %26lt; lowscore)


lowscore = score[i];


}

C++, lowest test score drop question?
As soon as getValues() returns, the score array gets deleted. What you need to do is create an array in the main() method (this isn't a global variable) and pass it to getValues() and findLowest() (c++ will automatically pass arrays by value).





int main()


{


float highscores[4];


getValues(score);


return 0;


}





void getValues(float[ ] highscores)


{


float scores[5];





// Populate the the scores array


// i.e. scores[ i ] = input





float lowscore = findLowest(scores);





//move high scores into highscores array


int count=0;


for (int i=0; i%26lt;5; i++)


{


if (scores[i] != lowscore) // as long as it's not the low score


highscores[count++] = scores[i];


}





}








float findLowest(float[ ] scores)


{


// Run your algorithm to find the lowest score in scores[ ]


// return that score


}





That's how it's done. Just fill in the blanks. Good luck!


No comments:

Post a Comment