View Full Version : help
nothing
08-08-2003, 12:35 AM
How do I find the first biggest, the second biggest, and the third biggest between three numbers? I know how to find the biggest and the smallest but I can't think of a way to find the second biggest. Thank you.
EscapeCharacter
08-08-2003, 02:00 AM
compare them?
largest = 0;
smallest = 0;
if(num1 > largest)
largest = num1;
do the same for the other numbers
DNAunion2000
08-08-2003, 11:58 PM
nothing: How do I find the first biggest, the second biggest, and the third biggest between three numbers? I know how to find the biggest and the smallest but I can't think of a way to find the second biggest. Thank you.
/*DNAunion*/ Process of elimination?
nothing
08-09-2003, 12:59 PM
What do you mean by that?
jamessan
08-09-2003, 01:40 PM
The process by which you eliminate all possible wrong answers, thus leaving you with the correct answer.
sicarius
08-09-2003, 02:20 PM
If you can find the larget and the smallest, then the second largest is simply the one that is left (i.e. the middle number).
first largest == largest
second largest == middle value
third largets == smallest
you said you already know how to get t largest and smallest.
nothing
08-09-2003, 08:31 PM
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
int largest = 0;
int middleValue;
int smallest = 0;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 > largest)
largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
if (num1 > smallest)
smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
return 0;
}
Now how would I find the middle value?
sans-hubris
08-10-2003, 03:57 AM
Originally posted by nothing
Now how would I find the middle value? Figure out which number you haven't used yet.
nothing
08-10-2003, 02:33 PM
I think I got it.
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
int middleValue;
int largest = 0;
int smallest = 0;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 > largest)
largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
if (num1 > smallest)
smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
if (largest == num1 && smallest == num2)
middleValue = num3;
if (largest == num1 && smallest == num3)
middleValue = num2;
if (largest == num2 && smallest == num1)
middleValue = num3;
if (largest == num2 && smallest == num3)
middleValue = num1;
if (largest == num3 && smallest == num1)
middleValue = num2;
if (largest == num3 && smallest == num2)
middleValue = num1;
cout << smallest << " " << middleValue << " " << largest << endl;
return 0;
}
Is that how you guys would do it?
inkedmn
08-10-2003, 05:46 PM
you could also put them into an array and sort it (probably not as efficient)
nothing
08-10-2003, 10:23 PM
The program I'm writing is for an exercise on my book and it tells me to write a program that reads three nonzero double values and determines and prints whether they could represent the sides of a triangle, so I wrote this:
#include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
double num3;
double largest = 0;
double middleValue = 0;
double smallest = 0;
cout << "Enter three nonzero values: ";
cin >> num1 >> num2 >> num3;
while (num1 == 0)
{
cout << "Please, enter a nonzero value for side one: ";
cin >> num1;
}
while (num2 == 0)
{
cout << "Please, enter a nonzero value for side two: ";
cin >> num2;
}
while (num3 == 0)
{
cout << "Please, enter a nonzero value for side three: ";
cin >> num3;
}
if (num1 > largest)
largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
if (num1 > smallest)
smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
if (largest == num1 && smallest == num2)
middleValue = num3;
if (largest == num1 && smallest == num3)
middleValue = num2;
if (largest == num2 && smallest == num1)
middleValue = num3;
if (largest == num2 && smallest == num3)
middleValue = num1;
if (largest == num3 && smallest == num1)
middleValue = num2;
if (largest == num3 && smallest == num2)
middleValue = num1;
if ((middleValue + smallest) < largest)
{
cout << num1 << ", " << num2 << ", and " << num3
<< " could not possibly represent the sides of a triangle." << endl;
}
return 0;
}
When I run this program and I type 1 0 3, the program tells me to enter a nonzero value for side two, so I type in number 2 and instead of telling me if the numbers could represent the sides of a triangle, the program exits. Why is that happening? Another question: I know how to put this program in a loop, but I don't know how to exit the loop. I tried using while (num1 != -1) but it won't work :( Help please.
downpour
08-10-2003, 11:23 PM
Originally posted by nothing
if (num1 > smallest)
This is backwards. Look at your other two smallest cases.
When I run this program and I type 1 0 3, the program tells me to enter a nonzero value for side two, so I type in number 2 and instead of telling me if the numbers could represent the sides of a triangle, the program exits. Why is that happening?
Look at your program. You say:
if ((middleValue + smallest) < largest)
{
cout << num1 << ", " << num2 << ", and " << num3
<< " could not possibly represent the sides of a triangle." << endl;
}
your input was 1, 2, 3. 1+2=3, and 3 == 3. So it should not print anything out. Try using 1, 2, 4 for your input.
nothing
08-10-2003, 11:45 PM
OK. I modified that line and now it works.
if ((middleValue + smallest) < largest || (middleValue + smallest) == largest)
Now, I still don't know how to exit the loop. Any suggestions?
coldflame
08-21-2003, 02:33 PM
What is this code suposed to do anyways.
I mean what is the program?
sans-hubris
08-22-2003, 03:13 AM
Originally posted by nothing
OK. I modified that line and now it works.
if ((middleValue + smallest) < largest || (middleValue + smallest) == largest)
Now, I still don't know how to exit the loop. Any suggestions? Your latest code doesn't even wrap everything into a loop. There's no proper loop to exit! Just wrap everything into a while or a do-while loop and evaluate the variables for equality to zero.
Programm3r
08-22-2003, 08:44 AM
nothing, you are making the easy things difficult.
instaed of writting this:
>if ((middleValue + smallest) < largest || (middleValue + smallest) == largest)
you could write
if ((middleValue + smallest) <= largest )
if you want to put the in a loop do this :
include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
double num3;
double largest = 0;
double middleValue = 0;
double smallest = 0;
cout << "Enter three nonzero values: ";
cin >> num1 >> num2 >> num3;
while( num1 != -1 ){
while (num1 < 0)
{
cout << "Please, enter a nonzero value for side one: ";
cin >> num1;
}
while (num2 < 0)
{
cout << "Please, enter a nonzero value for side two: ";
cin >> num2;
}
while (num3 < 0)
{
cout << "Please, enter a nonzero value for side three: ";
cin >> num3;
}
if (num1 > largest)
largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
if (num1 > smallest)
smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
if (largest == num1 && smallest == num2)
middleValue = num3;
if (largest == num1 && smallest == num3)
middleValue = num2;
if (largest == num2 && smallest == num1)
middleValue = num3;
if (largest == num2 && smallest == num3)
middleValue = num1;
if (largest == num3 && smallest == num1)
middleValue = num2;
if (largest == num3 && smallest == num2)
middleValue = num1;
if ((middleValue + smallest) < largest) //forget this!
{
cout << num1 << ", " << num2 << ", and " << num3
<< " could not possibly represent the sides of a triangle." << endl;
}
cout << "Enter three nonzero values: ";
cin >> num1 >> num2 >> num3;
}
return 0;
}
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.