RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 6227 | Replies: 31 | Solved
Reply
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,625
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 983
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Finding the lowest of five test scores

  #11  
Apr 22nd, 2007
1. remove the else in that function. Each score must be tested and the else is preventing that from happening.

2. At the top of the function set LO to be initially the value of score1.

3. Why is LO a parameter to the function? Make it a local variable and return it at the end of the function.
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
    int lo = score1;
    // other code here

   return lo;
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Help Re: Finding the lowest of five test scores

  #12  
Apr 27th, 2007
Well, I revised my code, and this is what I came up with.

 
#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage (int, int, int, int, int, int, int);
int main()
{
        int average=0, score1=0, score2=0, score3=0, score4=0, score5=0, small=0;
        getscore (score1, score2, score3, score4, score5);
        calcAverage (average, score1, score2, score3, score4, score5, small);
        getch();
        return 0;
}
 
void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
{
        cout << "Enter first score: ";
        cin  >> score1;
        cout << "Enter second score: ";
        cin  >> score2;
        cout << "Enter third score: ";
        cin  >> score3;
        cout << "Enter fourth score: ";
        cin  >> score4;
        cout << "Enter fifth score: ";
        cin  >> score5;
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
        int small =score1;
        if(small>score2)
        {
        small=score2;
        }
        if(small>score3)
        {
        small=score3;
        }
        if(small>score4)
        {
        small=score4;
        }
        if(small>score5)
        {
        small=score5;
        }
        return small; 
}
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5, int small)
{
         
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}

As soon as I tried to find the average of 100 + 100 + 100 + 100, my average came out to be 125! When you run my program, you will see.
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

  #13  
Apr 27th, 2007
this is because you have a small mistake right in the end...

erm... you aren't using four scores... you are actually using five scores...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

  #14  
Apr 27th, 2007
another thing... why are you using
int small
as a parameter in calcAverage?
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

  #15  
Apr 27th, 2007
oooohh... i know where your problem is... when you call
calcAverage
you are using your variable "small" instead of the function you used to find the smallest... your code should go this way in line 12:
calcAverage(score1,score2,score3,score4,score5,
findLowest(score1,score2,score3,score4,score5));
or, the other way around, you could do this before calling your calcAverage function:
small=findLowest(score1,score2,score3,score4,score5);
which will work exactly the same...

got it?
Last edited by Nichito : Apr 27th, 2007 at 3:44 am.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Feb 2006
Location: INDIA
Posts: 446
Reputation: anupam_smart is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
anupam_smart's Avatar
anupam_smart anupam_smart is offline Offline
Posting Pro in Training

Re: Finding the lowest of five test scores

  #16  
Apr 27th, 2007
The problem is that you are not getting the value of small as evaluated from the function findLowest().

So, better try this thing...

void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
int small=findLowest(score1,score2,score3,score4,score5);
average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
cout << average;
}

As suggested by Nichito.

Another point:
Why are you declaring average in main() and then passing as an argument? Just declare it in calcAverage() only.
Never say impossible 'cause impossible itself says:

"I M POSSIBLE":) :) :)
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

  #17  
Apr 27th, 2007
Originally Posted by anupam_smart
Another point:
Why are you declaring average in main() and then passing as an argument? Just declare it in calcAverage() only.
i agree with anupam_smart, but you can save even more memory by not declaring average at all... just do
 cout<<(((score1+score2+score3+score4+score5)-small)/4);
Last edited by Nichito : Apr 27th, 2007 at 5:15 am.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Apr 2007
Location: Mississippi
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: Finding the lowest of five test scores

  #18  
Apr 27th, 2007
Here's my code again with the corrections that you suggested:

 
#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage (int, int, int, int, int,);
int main()
{
        int average, score1, score2, score3, score4, score5, small;
        getscore (score1, score2, score3, score4, score5);
        calcAverage (score1, score2, score3, score4, score5);
        getch();
        return 0;
}
 
void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
{
        cout << "Enter first score: ";
        cin  >> score1;
        cout << "Enter second score: ";
        cin  >> score2;
        cout << "Enter third score: ";
        cin  >> score3;
        cout << "Enter fourth score: ";
        cin  >> score4;
        cout << "Enter fifth score: ";
        cin  >> score5;
}
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
         
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
        int small =score1;
        if(small>score2)
        {
        small=score2;
        }
        if(small>score3)
        {
        small=score3;
        }
        if(small>score4)
        {
        small=score4;
        }
        if(small>score5)
        {
        small=score5;
        }
        return small; 
}

But, here are the error messages that i got:
  • Undefined symbol "small"
Small is representing the smallest number from the five numbers that I key in.
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,554
Reputation: John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all 
Rep Power: 17
Solved Threads: 284
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Finding the lowest of five test scores

  #19  
Apr 27th, 2007
  1. void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
  2. {
  3.  
  4. average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
You never had small as a function parameter. And more to the point, you never calculated small in your program (even though you coded the function).
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Finding the lowest of five test scores

  #20  
Apr 27th, 2007
as suggested before, you shoul (in the main() function) assign your int small the value of the function findLowest, so, later, you can send it as a parameter to the function calcAverage... that should do it...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:00 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC