Jump to content

C Programming ascending numbers with selections / if statements (only)

Go to solution Solved by madknight3,

 

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.

I have this task i need to do which is:

 

Write a C program sort3.c that

  1. prompts the user to enter three integer numbers;
  2. outputs the numbers in ascending numerical order. (NB You should solve the problem using if statements).

And so far the code I thought of is this:

#include <stdio.h>int main(){	int a, b, c;	printf(" Enter value 1: "); scanf_s(" %i", &a);	printf(" Enter value 2: "); scanf_s(" %i", &b);	printf(" Enter value 3: "); scanf_s(" %i", &c);	if (a > b && c)			printf(" %i %i %i", a, b, c);	else if (a > c || b)			printf(" %i %i %i\n", a, c, b);	{		if (b > a && c)			printf(" %i %i %i\n", b, a, c);		else if (b > c && a)			printf(" %i %i %i\n", b, c, a);		{			if (c > b && a)				printf(" %i %i %i\n", c, b, a);			else if (c > a && b)				printf(" %i %i %i\n", c, a, b);		}	}		return 0;}

It's not broken but it's not quite doing what I want it to do.

 

I've attached a picture of the current output. I am sort of new to programming and have only just learned about selections, so if anyone could help me with this code I'd be thankful.

post-225550-0-52754300-1448120857.png

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

The following code, and all your conditionals, probably aren't doing what you think they are doing

a > b && c

This checks if a > b is true or not. If it's false, the whole thing is false (short circuiting). If it is true, it checks if c is true (which I believe for the C language checks if c isn't zero).

 

So this

// So thisif (a > b && c) {    // Do something}// Would be like doing thisif (a > b) {    if (c) {        // Do something    }}

If you want to compare the same variable against two variables it would look like this

// Example 1: Check if a is larger than both b and cif (a > b && a > c) {    // Do something}// Example 2: Check if a is larger than b and b is larger than cif (a > b && b > c) {    // Do something}
Link to comment
Share on other sites

Link to post
Share on other sites

 

The following code, and all your conditionals, probably aren't doing what you think they are doing

a > b && c

This checks if a > b is true or not. If it's false, the whole thing is false (short circuiting). If it is true, it checks if c is true (which I believe for the C language checks if c isn't zero).

 

So this

// So thisif (a > b && c) {    // Do something}// Would be like doing thisif (a > b) {    if (c) {        // Do something    }}

If you want to compare the same variable against two variables it would look like this

// Example 1: Check if a is larger than both b and cif (a > b && a > c) {    // Do something}// Example 2: Check if a is larger than b and b is larger than cif (a > b && b > c) {    // Do something}

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.

#include <stdio.h>int main(){	int a, b, c;	printf(" Enter value 1: "); scanf_s(" %i", &a);	printf(" Enter value 2: "); scanf_s(" %i", &b);	printf(" Enter value 3: "); scanf_s(" %i", &c);	if (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);	{		if (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);		{			if (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);		}	}		return 0;}
   
   
Link to comment
Share on other sites

Link to post
Share on other sites

 

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.

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Oh god I really did do it the hard way lol. Well at least I learned something, thanks for your help.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Oh god I really did do it the hard way lol. Well at least I learned something, thanks for your help.

 

No problem, if you run into any more issues I'll be here.

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×