Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

If statements C++

Options
  • 03-07-2009 10:42am
    #1
    Registered Users Posts: 2,315 ✭✭✭


    I'm just trying to figure out how to get c++ to determine the smallest and largest of three numbers using if statements. Anyone any ideas how would be done.


Comments

  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    Can you show an attempt?


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    deceit wrote: »
    I'm just trying to figure out how to get c++ to determine the smallest and largest of three numbers using if statements. Anyone any ideas how would be done.

    Well, if you think about how you'd do this in your head, it'll help with the solution. For instance, I give you a list of numbers (a couple dozen, say), and ask you what the biggest one is. You'd look down through the list; "1, 10, 8, 15...". So far, it's 15 - you'll have that in your head as the biggest one, and keep going down the list. If you find a bigger one than 15, you'll swap that in your mind as the biggest one, and so on until the end of the list. Whatever you've got in your head as the biggest one when you reach the end, is the answer.

    You just need to think about the above description in terms of loops, a variable or two and conditional (if) statements. Finding the smallest number is the same thing, and can be added to the above - if I asked you the smallest number in the list, you could do it at the same time, as long as you keep two numbers in your head.


  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    If it's just 3 numbers, and they're not in a list/array, i.e. the function is 'int get_max (int x, int y, int z)', then you don't need a loop, just 2 if statements.

    That said, it's better to know how to do it generally for a long list of ints in a list/array.


  • Registered Users Posts: 2,315 ✭✭✭deceit


    Sorry I forgot to put up my attempt. Here it is, just keeps showing an error.
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num1, num2, num3;
    
    	cout << "Enter Three numbers: " << endl;
    	cin >> num1 >> num2 >> num3;
    
    	if(num1 < num2)
    		if(num1 < num3)
    			cout << num1 << " is the smallest number." << endl;
    
    	if(num2 < num1)
    		if(num2 < num3)
    			cout << num2 << " is the smallest number." << endl;
    
    	if(num3 < num1)
    		if(num1 < num2)
    			cout << num2 << " is the smallest number." << endl
    
    	system("pause");
    	return 0;
    }
    


  • Registered Users Posts: 2,315 ✭✭✭deceit


    Just read the error that was showing and realised was me just being stupid. It just missed a semi colon, must have read the error wrong last time :o:o


  • Advertisement
  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    deceit wrote: »
    Sorry I forgot to put up my attempt. Here it is, just keeps showing an error.
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num1, num2, num3;
    
    	cout << "Enter Three numbers: " << endl;
    	cin >> num1 >> num2 >> num3;
    
    	if(num1 < num2)
    		if(num1 < num3)
    			cout << num1 << " is the smallest number." << endl;
    
    	if(num2 < num1)
    		if(num2 < num3)
    			cout << num2 << " is the smallest number." << endl;
    
    	if(num3 < num1)
    		if(num1 < num2)
    			cout << num2 << " is the smallest number." << endl
    
    	system("pause");
    	return 0;
    }
    

    Well the semi-colon error should have been immediately obvious. What tools are you using (visual studio?)?

    And the code above won't give the third number as being the smallest ever.

    I assume you're new to programming so I'll say always put brackets after your 'if' statements. Makes it easier to read and to add stuff to. Secondly why are you not using any 'else if/else' conditions in your code? It's better practice than a load of if statements on their own.

    Six 'if's is a bit overkill for this. I'm sure someone has a shorter way but just off the top of my head you should be looking towards a solution like the following
    if( (num1<num2) && (num1<num3))
    	{
    		cout << num1 << " is the smallest number\n";
    	}
    
    	else if ( (num2<num1) && (num2<num3))
    	{
    		cout << num2 << " is the smallest number\n";
    	}
    
    	else
    		cout << num3 << " is the smallest number\n";
    


  • Registered Users Posts: 2,315 ✭✭✭deceit


    toiletduck wrote: »
    Well the semi-colon error should have been immediately obvious. What tools are you using (visual studio?)?

    And the code above won't give the third number as being the smallest ever.

    I assume you're new to programming so I'll say always put brackets after your 'if' statements. Makes it easier to read and to add stuff to. Secondly why are you not using any 'else if/else' conditions in your code? It's better practice than a load of if statements on their own.

    Six 'if's is a bit overkill for this. I'm sure someone has a shorter way but just off the top of my head you should be looking towards a solution like the following
    if( (num1<num2) && (num1<num3))
    	{
    		cout << num1 << " is the smallest number\n";
    	}
    
    	else if ( (num2<num1) && (num2<num3))
    	{
    		cout << num2 << " is the smallest number\n";
    	}
    
    	else
    		cout << num3 << " is the smallest number\n";
    
    I am new to programming yes. I know it should have been obivious, just never noticed as back from work and havent slept yet. I'm using visual studio professional 2008 (get for free in work).Do you mean brackets like these after the if statements {}, I taught they where for when adding more than 1 bit of code. I havent learned about else and using && yet.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Well, some people prefer to always have them. I think it looks clearer so I always do it anyways. Up to you though.


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    deceit wrote: »
    I am new to programming yes. I know it should have been obivious, just never noticed as back from work and havent slept yet. I'm using visual studio professional 2008 (get for free in work).

    Good tool to have. Spotting errors and knowing what error ,messages actually mean is pretty important in learning programming!

    Do you mean brackets like these after the if statements {}, I taught they where for when adding more than 1 bit of code.

    Well they are, but some people (like myself) always use them even for single line expressions.
    I havent learned about else and using && yet.

    Ok, you learning by yourself or doing a course...? 'else' is the counterpoint to 'if' basically. And "&&" translates to 'AND'.

    So
    if( (num1<num2) && (num1<num3))
    

    means if num1 is less than num2 and num1 less than three do something. It's called a logical operator, there are others (OR, XOR etc.).

    The 'else' at the end of the code I slapped togther above means that if the first two 'ifs' aren't satisfied then whatever is contained within the 'else' is executed.


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    If you're just using 'if's' then your attempt will do fine, with minor changes (in bold) in the last two if statements.
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num1, num2, num3;
    
    	cout << "Enter Three numbers: " << endl;
    	cin >> num1 >> num2 >> num3;
    
    	if(num1 < num2)
    		if(num1 < num3)
    			cout << num1 << " is the smallest number." << endl;
    
    	if(num2 < num1)
    		if(num2 < num3)
    			cout << num2 << " is the smallest number." << endl;
    
    	if(num3 < num1)
    		if([B]num3[/B] < num2)
    			cout << [B]num3[/B] << " is the smallest number." << endl
    
    	system("pause");
    	return 0;
    }
    


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    highest and lowest at the same time...
    // given num1, num2, num3
    int largest = num3;
    int smallest = num3;
    if(num1 > largest) largest = num1;
    else smallest = num1;
    if(num2 > largest) largest = num2; 
    else if(num2 < smallest) smallest = num2;
    
    ... works in Java anyhoo!


  • Registered Users Posts: 50 ✭✭Chi chi


    Are you restricted to using ONLY "if" for the flow control? It doesn't have to be that complicated....


  • Registered Users Posts: 2,315 ✭✭✭deceit


    Yes I was restricted as it was a question put forward to me and stated could only use a limited set of code.


Advertisement