Very NEW Beginer, Going mad

This is a discussion on Very NEW Beginer, Going mad within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to write a program that inputs three integers from the keyboard and prints out the smallest and ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Very NEW Beginer, Going mad

    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.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Post your code and we can show you where your mistakes are.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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;
    	}
    }

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    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

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    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...
    nextus, the samurai warrior

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Hope this helps: No if's, no boolean operators.

    (condition ? truepart : falsepart)

    Code:
    highest=( (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) );

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Chris,

    You should be eternally grateful for the help you've received here.

    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.

    (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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Thumbs down

    Agree with Skipper. Let them do there own work first.
    Mr. C: Author and Instructor

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I agree with skipper.

  10. #10
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    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.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  11. #11
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    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
    nextus, the samurai warrior

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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!

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    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

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    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

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    well, that makes 3 people that just did someone else's homework in this thread. Read This

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makes me mad..
    By Warrax in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-01-2007, 07:49 AM
  2. I, Robot -- It's a trick, and I'm mad.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-15-2004, 01:57 PM
  3. mad libs
    By CheesyMoo in forum C++ Programming
    Replies: 17
    Last Post: 01-18-2003, 10:14 PM
  4. mad scrolling
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-20-2001, 01:38 PM
  5. Spam :mad:
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-22-2001, 10:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21