Friday, July 31, 2009

C++: How would I print the 5 middle numbers?

#include %26lt;iostream.h%26gt;


#include %26lt;iomanip.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;stdlib.h%26gt;





using namespace std;





float findMiddleOne(float,float,float,float,fl...


float findFirst(float,float,float,float,float,...


float findLast(float,float,float,float,float,f...





int main ()


{


string name;


float score1,score2,score3,score4,score5,score...


float firstlowest=0;


float lastlargest=0;


float difficulty;


cout %26lt;%26lt; "Enter the divers name: ";


getline (cin,name);


cout %26lt;%26lt; "Enter the degree of difficulty for "%26lt;%26lt;name%26lt;%26lt;"'s dive: ";


cin %26gt;%26gt; difficulty;


while (difficulty%26lt;1.2 ||difficulty%26gt;3.8)


{


cout %26lt;%26lt; "Such difficulty does not exist, please try again: ";


cin %26gt;%26gt; difficulty;


}


cout%26lt;%26lt;"Enter the seven judges' scores"%26lt;%26lt;endl;


cout%26lt;%26lt;"First score: ";


cin %26gt;%26gt; score1;


while (score1%26lt;0||score1%26gt;10)


{


cout %26lt;%26lt; "Wrong number, please try again: ";


cin %26gt;%26gt; score1;


}


cout %26lt;%26lt;"Second score: ";


cin %26gt;%26gt; score2;

C++: How would I print the 5 middle numbers?
Your code is way too complex for such a simple problem.





You can use a 7-cell array to store the scores. Much more efficient code:





string names;


float scores[7];


int i;


float score;


float difficulty = 0;





while(difficulty %26lt; 1.2 || difficulty%26gt;3.8) {


cout %26lt;%26lt; "Enter the degree of difficulty for " %26lt;%26lt; name %26lt;%26lt; "'s dive:" %26lt;%26lt; endl;


cin %26gt;%26gt; difficulty;


}





for(i = 0 ; i %26lt; 7; i++) {


score = -1;


while(score %26lt; 0 || score %26gt; 10) {


cout %26lt;%26lt; "Enter the score from judge " %26lt;%26lt; i + 1 %26lt;%26lt; endl;


cin %26gt;%26gt; score;


}


scores[i] = score;


}





Just pass the 7-array score to functions to find max and min:





float getMaxScore(float scores[], int size) {


float result = 0;


float score;


int i;


for(i = 0; i %26lt; size; i++) {


if(scores[i] %26gt; result) {


result = scores[i];


}


}


return result;


}





float getMinScore(float scores[], int size) {


float result = 100;


float score;


int i;


for(i = 0; i %26lt; size; i++) {


if(scores[i] %26lt; result) {


result = scores[i];


}


}


return result;


}





Back in your main:





float maxScore, minScore;


maxScore = getMaxScore(scores, 7);


minScore = getMinScore(scores, 7);





cout %26lt;%26lt; "The highest score was " %26lt;%26lt; maxScore %26lt;%26lt; endl;


cout %26lt;%26lt; "The lowest score was " %26lt;%26lt; minScore %26lt;%26lt; endl;


cout %26lt;%26lt; "The other five scores are: ";


for(i = 0; i %26lt; 7; i++) {


if(scores[i] != maxScore %26amp;%26amp; scores[i] != minScore) {


cout %26lt;%26lt; scores[i] %26lt;%26lt; " ";


}


}


cout %26lt;%26lt; endl;


No comments:

Post a Comment