GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 21:13
MartyMcFly MartyMcFly is offline
New Member
 
Join Date: Dec 2005
Posts: 1
MartyMcFly is on a distinguished road

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:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

int getScores (int &, int &, int &, int &);
int findLowest (int, int, int);
void calcAvg (float &, int, int ,int);
void displayAvg (float);

void main ()
{
        int a, b, c, d;
        float avg;

        getScores(a, b, c, d);
        findLowest (a, b, c);
        calcAvg (avg, a, b, c);
        displayAvg (avg);
}

int getScores (int &a, int &b, int &c)
{
        int d;

        cout << "Enter score 1: "; cin >> a;
        cout << "Enter score 2: "; cin >> b;
        cout << "Enter score 3: "; cin >> c;
        cout << "Enter score 4: "; cin >> d;
        return 0;
}

int findLowest (int a, int b, int c)
{

}

void calcAvg (float &avg, int a, int b, int c)
{
        avg = (a + b + c) / 3;
}

void displayAvg (float avg)
{
        cout << "The average of the top 3 scores is: " << avg;
        getch ();
}
Last edited by LuciWiz : 03-Dec-2005 at 07:44. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 03-Dec-2005, 03:58
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Program to calculate the average of three test scores


Hi 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:
return d;
And in the main function, you can get the value of d like this:
CPP / C++ / C Code:
        d = getScores(a, b, c);



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:
a = 10. b = 20. c = 15. d = 5. Start: lowest = a. ==> lowest = 10. lowest < b? ==> lowest < 20? true. No change. lowest < c? ==> lowest < 15? true. No change. lowest < d? ==> lowest < 5? False. The lowest value is d!!! so, lowest = d. ==> lowest = 5. return lowest; End.

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:
a = 10. b = 20. c = 15. d = 5. After findLowest function, lowest = 5. Start: lowest == a? ==> 5 == 10? false. lowest == b? ==> 5 == 20? false. lowest == c? ==> 5 == 15? false. lowest == d? ==> 5 == 5? TRUE. Call the function calcAvg by skipping d, and passing only a, b and c. calcAvg(a, b, c); End
Fourth: Function displays the average with appropriate identifying information.
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  
Old 04-Dec-2005, 11:32
pacopaco pacopaco is offline
New Member
 
Join Date: Dec 2005
Posts: 1
pacopaco is on a distinguished road

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  
Old 04-Dec-2005, 13:29
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: 1st term c++ help sort?


Quote:
Originally Posted by pacopaco
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?
At a quick glance I see this.
CPP / C++ / C Code:
int getScores ( int &, int &, int &);
getScores returns a "int" and you return an "int".
CPP / C++ / C Code:
int getScores(int &s1, int &s2, int &s3 )
{
//code snipped
	return s4;
}
But you discard the return value.
CPP / C++ / C Code:
getScores ( t1, t2, t3);
maybe,
CPP / C++ / C Code:
t4 = getScores ( t1, t2, t3);
  #5  
Old 04-Dec-2005, 16:48
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: 1st term c++ help sort?


Quote:
Originally Posted by pacopaco
CPP / C++ / C Code:
lowest = findLowest(s1, s2, s3, s4);

        average = (float)((s1 + s2 + s3 + s4) - lowest) / 3.0;
Ah! Nicely done.

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:
        lowest = findLowest(t1, t2, t3, t4);
similar to what you did in the calcAvg function.


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  
Old 04-Dec-2005, 17:14
sakura sakura is offline
New Member
 
Join Date: Oct 2005
Posts: 1
sakura is on a distinguished road

Re: 1st term c++ help sort?


Hello, Sokar and Paramesh!
Thank you so much for helping me.
Finally my program worked.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 00:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.