# Very NEW Beginer, Going mad

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-26-2003
chrismax2
Hi, I need to write a program that inputs three integers from the keyboard and prints out the smallest and largest, using only the IF statement with no logical AND or OR.
Now this is surpose to be possable, but when I do it I get the two lowest numbers and the two highest numbers.
Driving me nuts please help. I spent 3 hours going over and over the same stuff and I cant see how to do it without useing more then just IF.

Thanks

Chris.
• 02-26-2003
hk_mp5kpdw
Post your code and we can show you where your mistakes are.
• 02-26-2003
Speedy5
Very easy. Not a single boolean operator. You can even make it smaller using the ? : tierinary operator, but its advanced stuff:

( (condition) ? truepart : falsepart )

A function to find the smallest number would use the same principle as this one:

Code:

```int Largest(int a, int b, int c) {         if (a>b)         {                 if (a>c)                         return a;                 else                         return c;         }         else         {                 if (b>c)                         return b;                 else                         return c;         } }```
• 02-26-2003
chrismax2
Thanks, but..
Its more difficult then that as the challenge I was set said you cant use "else" as well.
If its not possable to do this without "else" then please let me know if it is, I will try yet again to work it out.
Thanks
• 02-26-2003
nextus
i wrote your program..just a plain if and a for loop

Code:

```#include <iostream> using namespace std; int main() {         int num[3];         cout << "enter first num: ";         cin >> num[0];         cout << "enter second num: ";         cin >> num[1];         cout << "enter third num: ";         cin >> num[2];         int low = num[0]; //low is set to first         for(int i = 1; i < 3; i++) //loop for low         {                 if(low >= num[i])                 {                         low = num[i];                 }         }         int high = num[0]; //high is set to first         for(i = 1; i < 3; i++)         {                 if(high <= num[i])                 {                         high = num[i];                 }         }         cout << "low is " << low << endl                 << "high is " << high << endl;         return 0; }```
:P was bored..code kinda long could be shorter...
• 02-26-2003
Speedy5
Hope this helps: No if's, no boolean operators.

(condition ? truepart : falsepart)

Code:

`highest=( (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) );`
• 02-26-2003
skipper
Chris,

Between nextus and Speedy5 (nextus' code being more straightforward), your homework is done.

A simple 'copy & paste' and you've learned, probably, little, or nothing. At least, nothing that you'll retain.

Good show, people. :rolleyes:

(Do you think that there may just be a reason why the moderators and very senior members suggest that students do their own work?)

-Skipper
• 02-26-2003
Mister C
Agree with Skipper. Let them do there own work first.
• 02-26-2003
alpha
I agree with skipper.
• 02-27-2003
rmullen3
~
Nah, we should write all their programs for them. That way we'll get practice, and they won't learn anything... Less competition for real programmers in the job market later on.
• 02-27-2003
nextus
haha..very funny...i agree that it is good practice...but i get so caught up in the question..that like a few moments later the whole program is written....i am sorry..i try not to do it again...:P
• 02-27-2003
Speedy5
You see, I had the same question at school. People asked me how to do it at school so its only fair if I help this person. Plus, I didn't give the WHOLE answer, he still needs to understand it: I gave him an explanation of the ?: operator.

I'll give more broader help next time! ;)
• 02-27-2003
Jamsan
That really didnt do anything for em...if he is just doing basic programming, the teacher will think WTF when he sees that kind of syntax, especially

Code:

`highest=( (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) );`
also, i doubt he is allowed to use loops as well, seeing as he has just started conditional statements
• 02-27-2003
Jamsan
as basic as it can get without using else's and AND or ORS

Code:

```#include <iostream.h> int main (void) {         int num1;         int num2;         int num3;         int max;         int min;         cout << "Enter Number 1: ";         cin >> num1;         cout << "Enter Number 2: ";         cin >> num2;         cout << "Enter Number 3: ";         cin >> num3;         max = num1;         min = num1;         if (num2 > max)                 max = num2;         if (num3 > max)                 max = num3;         if (num2 < min)                 min = num2;         if (num3 < min)                 min = num3;         cout << "\nLargest number is: " << max << endl;         cout << "Smallest number is: " << min << endl```
• 02-27-2003
alpha
well, that makes 3 people that just did someone else's homework in this thread. Read This
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last