| Natalyias77 (11) | |
|
I am trying to write a program that compares 3 numbers with the output labeling high, med,and low. It works for the highest and lowest, but I am having challenges trying to figure out how to calculate for the medium. Here is my program. In the third function it grabs what 'a' is. It ignores my if... #include "stdafx.h" #include <iostream> using namespace std; double larger(double a, double b, double c); double lowest(double a, double b, double c); double medium(double a, double b, double c); int main() { double one, two, three; cout << "Enter three numbers: \n"; cin >> one >> two >> three; cout << endl; cout << "High: " << larger(one, two, three)<< endl; cout << "Low: " << lowest(one, two, three) << endl; cout << "Med: " << medium(one, two, three) << endl; system ("pause"); return 0; } double larger(double a, double b, double c) { double max = a; if (b > max) max = b; if (c > max) max = c; return max; } double lowest(double a, double b, double c) { double min = a; if (b < min) min = b; if (c < min) min = c; return min; } double medium(double a, double b, double c) { double med = a; if (a < larger(a, b, c) && a > lowest(a, b, c)); return med; } Thanks, Shirley | |
|
|
|
| Moschops (5961) | ||
It does not ignore your if; the problem is that there is no code to be executed when the if is found to be true. After an if statement, the semi-colon marks the END of the code to be executed when the if is found to be true. if (a < larger(a, b, c) && a > lowest(a, b, c));I'll add a bit to your line of code to show you the code that is executed when the if is found to be true: if (a < larger(a, b, c) && a > lowest(a, b, c)) */ HERE IS THE CODE THAT IS EXECUTED - THERE IS NOTHING HERE */ ;
| ||
|
|
||
| Natalyias77 (11) | |
|
So what would you suggest? The trick is I can only do 3 comparisons. I can see where my third function is returning a empty value. Any thoughts would be great. | |
|
|
|
| Natalyias77 (11) | |
|
So I updated my code but am still unable to get the 'middle (med)' value. Thoughts? #include "stdafx.h" #include <iostream> using namespace std; double larger(double a, double b, double c); double lowest(double a, double b, double c); double medium(double a, double b, double c); int main() { double one, two, three; cout << "Enter three numbers: \n"; cin >> one >> two >> three; cout << endl; cout << "High: " << larger(one, two, three)<< endl; cout << "Low: " << lowest(one, two, three) << endl; cout << "Med: " << medium(one, two, three) << endl; system ("pause"); return 0; } double larger(double a, double b, double c) { double max = a; if (b > max) max = b; if (c > max) max = c; return max; } double lowest(double a, double b, double c) { double min = a; if (b < min) min = b; if (c < min) min = c; return min; } double medium(double a, double b, double c) { double med =a; if (b < larger(a, b, c) && b > lowest(a, b, c)) { b = med; } return med; } | |
|
|
|