Tuesday, July 28, 2009

Difficult c++ chapter, multiple functions and stuff.?

I'm pretty bummed about this chapter is c++. It is pretty hard and I can't seem to put it together. I did most of the work but there are a just a few problems I can't seem to get. I hope someone out there will be able to help me.





Here is how this one goes:





Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions:


-- void getScore() should ask the user for a test score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores.


-- void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main, and should be passed the five scores.


-- int findLowest() should find and return the lowest of the fives scores passed to it. I should be called by calcAverage, who uses the function to determine which of the five scores to drop.

Difficult c++ chapter, multiple functions and stuff.?
#include %26lt;iostream%26gt;





using namespace std;





/* Assumes only 5 scores */


const int NUM_SCORES = 5;


double scores[NUM_SCORES];





void getScore(double %26amp; score)


{


char tmp[255];


char *s;


bool valid = false;





/* Keep looping until the user input a valid numer */


while(!valid)


{


cout %26lt;%26lt; "\nEnter test score: " %26lt;%26lt; endl;





cin %26gt;%26gt; tmp;





/* Validate */


s = tmp;


valid = true;


/* Loop through the input string making sure that only


numerals were entered */


while(*s != '\0')


{


if(!isdigit(*s))


{


/* Invalid input. will try again */


cout %26lt;%26lt; "Error. Invalid input " %26lt;%26lt; endl;


valid = false;


break;


}


s++;


}


}





/* Value is valid, convert string to a number */


score = atof(tmp);





/* Make sure value is realistic */


if(score %26lt; 0 || score %26gt; 100)


{


cout %26lt;%26lt; "\nError: Invalid score" %26lt;%26lt; endl;


getScore(score); /* Try again */


}


}





int findLowest()


{


double high_value = 101;


int lowest_offset = 0;





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


{


if(scores[i] %26lt; high_value)


{


high_value = scores[i];


lowest_offset = i;


}


}





return lowest_offset;


}





void calcAverage()


{


double avg = 0;





int lowest_offset = findLowest();





/* Loop through the array and add each score


except the one specified from findLowest */


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


{


if(i != lowest_offset)


avg += scores[i];


}





avg = avg / (NUM_SCORES-1);





cout %26lt;%26lt; "\n\nThe average is: " %26lt;%26lt; avg %26lt;%26lt; endl;


}





int main()


{


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


getScore(scores[i]);





calcAverage();





return 0;


}
Reply:Stash O, sometimes, it is better to try and do the program yourself before you ask for help from someone else. It's never a good learning process if someone gives you all of the answers. Report It


online florists

No comments:

Post a Comment