C Programming ascending numbers with selections / if statements (only)
Ahh i see I understand that, i tried adding it in. Does this look right to you? It doesn't always work but it works better than it did before. Sometimes a combination of inputted values are not in assending order, but mostly they are in the output.
Yes, you're moving in the right direction. To address some more issues
if (a > b && a > c)else if (a > c && a > b)// These two conditions are basically the same. All it's doing is checking them in a different order, the results will be identical.// If the first is true, the second will never be reached. If the first is false, the second will also be false.
The reason your program is still running past the first if statement is because of the next issue, invalid brackets.
// if you don't supply brackets to an if, else if, or else block it'll only use the next statement.if (...) // no brackets, only one statement will be executedelse if (...) { // multiple statements can be executed}// so in your codeelse if (a > c && a > b) printf(" %i %i %i\n", a, c, b);{// the bracket there isn't actually part of that else if block and isn't doing anything.// So that makes your logic actually act like this// one if-else blockif (a > b && a > c) { printf(" %i %i %i", a, b, c);} else if (a > c && a > B) { printf(" %i %i %i\n", a, c, B);}// a separate if-else blockif (b > a && b > c) { printf(" %i %i %i\n", b, a, c);} else if (b > c && b > a) { printf(" %i %i %i\n", b, c, a);}// a third separate if-else blockif (c > b && c > a) { printf(" %i %i %i\n", c, b, a);} else if (c > a && c > B) { printf(" %i %i %i\n", c, a, B);}// adding in the first issue I mentioned, where your conditions are basically the same// your code is really reduced to thisif (a > b && a > c) { printf(" %i %i %i", a, b, c);}if (b > a && b > c) { printf(" %i %i %i\n", b, a, c);}if (c > b && c > a) { printf(" %i %i %i\n", c, b, a);}
Careful with your brackets and logic. Remember that else if () is used to check something different than your if () so make sure they are indeed different. And if you just want the opposite of your if (), then use a regular else.
It might help to write out all the possible cases on paper (or in code comments) to make sure you get everything. Don't forget than two or more values can be equal. It can also be helpful to write down a test for each case (ie: what you'd enter for a, b, and c) so you can go through them one by one like a checklist and know if your code is working correctly.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now