Please support our C++ advertiser: Programming Forums
Views: 6227 | Replies: 31 | Solved
![]() |
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,625
Reputation:
Rep Power: 40
Solved Threads: 983
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.
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;
} Well, I revised my code, and this is what I came up with.
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.
#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.
If you teach a man to fish, you feed him for a lifetime.
another thing... why are you using as a parameter in calcAverage?
int small
-->sometimes i wanna take my toaster in a bath<-- oooohh... i know where your problem is... when you call 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:
or, the other way around, you could do this before calling your calcAverage function:
which will work exactly the same...
got it?
calcAverage
calcAverage(score1,score2,score3,score4,score5, findLowest(score1,score2,score3,score4,score5));
small=findLowest(score1,score2,score3,score4,score5);
got it?
Last edited by Nichito : Apr 27th, 2007 at 3:44 am.
-->sometimes i wanna take my toaster in a bath<-- 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.
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":) :) :)
"I M POSSIBLE":) :) :)
•
•
•
•
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.
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<-- Here's my code again with the corrections that you suggested:
But, here are the error messages that i got:
#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"
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.
If you teach a man to fish, you feed him for a lifetime.
cplusplus Syntax (Toggle Plain Text)
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5) { average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode