![]() |
|
#1
|
|||
|
|||
1st term c++ help sort?Assignment: Write a program to calculate the average of three test scores. All functions must be called from function main. The program must use (do not change prototypes) the following function prototypes:
int getScores (int &, int &, int &); Function prompts the user to enter 4 test scores. int findLowest (int, int, int, int); Function identifies the lowest score. void calcAvg (float &, int, int, int) Function calculates the average of the 3 highest scores. void displayAvg (float); Function displays the average with appropriate identifying information. I'm having trouble trying to calculate the average given the limitations. I think a, b, c and d should be sorted in some way (i'm not sure though) but we've not been over that in class. Keep in mind i'm a first term c++ student and we just started using multiple functions (don't make fun of my code too much CPP / C++ / C Code:
Last edited by LuciWiz : 03-Dec-2005 at 07:44.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
||||
|
||||
Program to calculate the average of three test scoresHi MartyMcFly,
Welcome to GIDForums. As you say: The program must use (do not change prototypes) the following function prototypes: Lets discuss how to do this one by one. First: A Function prompts the user to enter 4 test scores. Prototype: int getScores (int &, int &, int &); Note that you should not pass an extra argument as int&, as you did in your program. But return the fourth value using the return statement because the return_type of the function is int!!! Note this: int getScores (int &, int &, int &); Do you understand? So, just add a return d; at the end of the getScores function. CPP / C++ / C Code:
CPP / C++ / C Code:
Second: Function identifies the lowest score. Prototype: int findLowest (int, int, int, int); Aha! we can pass all the values a, b, c and d to the function. But how to find the lowest? First, we can declare a integer say int lowest; Initialize the lowest value to a. Then check whether lowest value is lesser than b. If it is, then no problem. But if it is not, change the lowest to b. Similarly, check for c and d. Here is a sample: Code:
Third: Function calculates the average of the 3 highest scores. Prototype: void calcAvg (float &, int, int, int) Here comes the problem. We have to find the average of 3 highest scores only!! So, we have to skip the lowest value, and then pass the remaining variables to the function. How? We can check whether lowest is equal to a. If it is, then skip a, and calculate the average by passing b, c and d: calcAvg(b, c, d); If lowest is not equal to a, then check whether lowest is equal to b. If it is, then skip b, and calculate the average by passing a, c and d. calcAvg(b, c, d); and so on.. Here is a sample: Code:
Prototype: void displayAvg (float); No problem. You have done this correctly! But three more things! Use getchar() instead of getch(). So, remove #include<conio.h> Declare the main function as int main(), rather than void main(). So, return 0 in the end of the main function. And finally, remove the #include<iomanip.h>. We are not using it. What compiler are you using? Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: 1st term c++ help sort?Hello, Paramesh and everyone!
I am new to here and just started studying C++. I have been working on this same problem for few hours but my program is not working correct. Could you tell me what is wrong with my code, please? #include <iostream.h> #include <iomanip.h> #include <conio.h> int getScores ( int &, int &, int &); int findLowest ( int, int, int, int ); void calcAvg ( float &, int, int, int ); void displayAvg ( float ); int main() { int t1, t2, t3, t4; float average; clrscr(); getScores ( t1, t2, t3); findLowest ( t1, t2, t3, t4 ); calcAvg ( average, t1, t2, t3); displayAvg (average); getch(); return 0; } int getScores(int &s1, int &s2, int &s3 ) { int s4; cout << " Please enter the score range 0 to 100.\n"; cout << " Test score1: "; cin >> s1; cout << " Test score2: "; cin >> s2; cout << " Test score3: "; cin >> s3; cout << " Test score4: "; cin >> s4; return s4; } int findLowest ( int s1, int s2, int s3, int s4 ) { int lowest = s1; if (s2 < lowest) lowest = s2; if (s3 < lowest) lowest = s3; if (s4< lowest) lowest = s4; return lowest; } void calcAvg( float &average, int s1, int s2, int s3) { int s4, lowest; cout << fixed << showpoint << setprecision(2); lowest = findLowest(s1, s2, s3, s4); average = (float)((s1 + s2 + s3 + s4) - lowest) / 3.0; } void displayAvg ( float average ) { cout << "\n\nThe average of the three test scores after "; cout << "dropping\nthe lowest score out of four test is "; cout << average << endl; getch(); } |
|
#4
|
|||
|
|||
Re: 1st term c++ help sort?Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#5
|
||||
|
||||
Re: 1st term c++ help sort?Quote:
But wait! You dont know the value of s4 there!!! You are passing only 3 numbers to the function calcAvg. You dont know the 4th number. So, just go with the method i mentioned earlier to find the average. and As Sokar mentioned, you should get the returned valud in the main function, for the findLowest function also. So, create a integer say lowest in the main function, and do like this: CPP / C++ / C Code:
Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#6
|
|||
|
|||
Re: 1st term c++ help sort?Hello, Sokar and Paramesh!
Thank you so much for helping me. Finally my program worked. |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ exponent help please!!!!! | green lantern | C++ Forum | 3 | 26-Nov-2005 08:12 |
| help with Bubble sort program that uses vectors instead of arrays | sasuke101 | C++ Forum | 9 | 25-Oct-2005 11:26 |
| Help with a bucket sort please. | glulu76 | C++ Forum | 11 | 18-Apr-2005 15:33 |
| [GIM] gim.h | dsmith | C Programming Language | 0 | 18-Jan-2005 08:48 |
| insert sort | saphir55 | C Programming Language | 4 | 06-Dec-2004 14:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The