1
\$\begingroup\$

Problem : Read three integers and sort them in ascending order. After, print these values in ascending order, a blank line and then the values in the sequence as they were inputted.

input: 7 21 -14
output: -14
         7
         21

         7
        21
       -14

I have solved this problem in this way:

#include <stdio.h>

int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);

if (a < b && a < c)
{
    //first smallest number identified
    printf("%d\n",a);
  
    if (b < c)
    {
        //second and third smallest number identified
        printf("%d\n",b);
        printf("%d\n",c);
        printf("\n");
        printf("%d\n%d\n%d\n",a,b,c);
         
    }
    else if (c < b)
    {
         //second and third smallest number identified
         printf("%d\n",c);
         printf("%d\n",b);
         printf("\n");
         printf("%d\n%d\n%d\n",a,b,c);
    }
}
else if (b < a && b < c)
{
    //first smallest number identified
    printf("%d\n",b);
    if (a < c)
      {
         //second and third smallest number identified
         printf("%d\n",a);
         printf("%d\n",c);
         printf("\n");
         printf("%d\n%d\n%d\n",a,b,c);
      }
    else if (c < a)
    {
         //second and third smallest number identified
         printf("%d\n",c);
         printf("%d\n",a);
         printf("\n");
         printf("%d\n%d\n%d\n",a,b,c);
    }
}
else if (c < a && c < b)
{ 
    //first smallest number identified
    printf ("%d\n",c);
    if(a < b){
         //second and third smallest number identified
         printf("%d\n",a);
         printf("%d\n",b);
         printf("\n");
         printf("%d\n%d\n%d\n",a,b,c);
    }
    else if (b < a)
    {
         //second and third smallest number identified
         printf("%d\n",b);
         printf("%d\n",a);
         printf("\n");
         printf("%d\n%d\n%d\n",a,b,c);
    }
}

return 0;
}

What I am looking for: How can I simplify my solution? Will I face any problems with some conditions? If yes, then how can I handle this?

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

Bug

If all 3 values are the same, nothing is printed.

When 2 values are the same, some problems can occur too.

Code is using < when <= is called for, in which case the last else if (c <= a && c <= b) can be simply else.

The 3x use of code like if (b < c) ... else if (c < b) deserves to be if (b < c) ... else


Redundant code

Below code used 6 times. Once, toward the end, is enough.

 printf("\n");
 printf("%d\n%d\n%d\n",a,b,c);

qsort()

Consider qsort()

#define N 3
int a[N];
int b[N];
for (int i=0; i<N; i++) {
  if (scanf("%d", &a[i]) != 1) TBD_Code_Handle_Error();
  b[i] = a[i];
}
qsort(b, sizeof b[0], N, my_int_cmp);
for (int i=0; i<N; i++) {
  printf("%d\n", b[i]);
}
for (int i=0; i<N; i++) {
  printf("%d\n", a[i]);
}
\$\endgroup\$
1
\$\begingroup\$

In terms of simplifying the code it would be better to add all of the elements into an array and sort it rather than using if else statements to capture every case. I would look into the qsort library, which handles sorting for you.

\$\endgroup\$
1
\$\begingroup\$

This block can be put in a function:

    //second and third smallest number identified
     printf("%d\n",a);
     printf("%d\n",b);
     printf("\n");
     printf("%d\n%d\n%d\n",a,b,c);

The last line is even constant. It can be placed at the end.

other_two() works by the way which two arguments are passed.

#include <stdio.h>

void other_two(int x, int y) {
    if (x < y)
        printf("2) %d\n3) %d\n", x, y);
    else
        printf("2) %d\n3) %d\n", y, x);
}

void main() {
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);

    if (a <= b && a <= c) {
        printf("1) %d\n", a);
        other_two(b, c);
    } else if (b <= a && b <= c) {
        printf("1) %d\n", b);
        other_two(a, c);
    } else if (c <= b && c <= a) {
        printf("1) %d\n", c);
        other_two(a, b);
    }

    printf("%d\n%d\n%d", a, b, c);
    return;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.