Week 02 Extra Sample Solutions

Information

  • This page contains extra exercises for week 02.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●◌◌)
:

Ordering Three Integers

Write a C program order3.c using if statements (no loops) that reads 3 integers and prints them from smallest to largest.

Examples

dcc order3.c -o order3
./order3
Enter integer: 23
Enter integer: 5
Enter integer: 27
The integers in order are: 5 23 27
./order3
Enter integer: 3
Enter integer: 6
Enter integer: 27
The integers in order are: 3 6 27
./order3
Enter integer: 9
Enter integer: 8
Enter integer: 7
The integers in order are: 7 8 9

You can assume the user supplies 3 integers. You do not have to check the return value from scanf.

When you think your program is working, you can use autotest to run some simple automated tests:

1091 autotest order3
Sample solution for order3.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order

#include <stdio.h>

int main(void) {
    int a, b, c;
    int tmp;

    printf("Enter integer: ");
    if (scanf("%d", &a) != 1) {
        return 1; // EXIT_FAILURE would be more portable
    }

    printf("Enter integer: ");
    if (scanf("%d", &b) != 1) {
        return 1;
    }

    printf("Enter integer: ");
    if (scanf("%d", &c) != 1) {
        return 1;
    }

    // a, b, c can be in any order

    // swap a & b if they are not in order
    if (a > b) {
        tmp = b;
        b = a;
        a = tmp;
    }

    // swap a & c if they are not in order
    if (a > c) {
        tmp = c;
        c = a;
        a = tmp;
    }

    // a must be the smallest now

    // swap b & c if they are not in order
    if (b > c) {
        tmp = c;
        c = b;
        b = tmp;
    }

    // a, b, c now in  order

    printf("The integers in order are: %d %d %d\n", a, b, c);

    return 0;
}
Alternative solution for order3.c
// Program to take in three numbers then print them
// in decreasing order.
// Written by Rory Golledge (12/06/2021)
//
// Note: This solution provides an alternative to the much
// "smarter" solution given. This is in the case that a student
// does not understand the other as it may seem less
// intuitive.

#include <stdio.h>

int main(void) {
    int a;
    int b;
    int c;

    // Scan in all numbers
    printf("Enter integer: ");
    scanf("%d", &a);

    printf("Enter integer: ");
    scanf("%d", &b);

    printf("Enter integer: ");
    scanf("%d", &c);

    printf("The integers in order are: ");

    // Determine order from 6 possible combinations
    // and print them out.
    if (a <= b && b <= c) {
        printf("%d %d %d\n", a, b, c);
    } else if (a <= c && c <= b) {
        printf("%d %d %d\n", a, c, b);
    } else if (b <= a && a <= c) {
        printf("%d %d %d\n", b, a, c);
    } else if (b <= c && c <= a) {
        printf("%d %d %d\n", b, c, a);
    } else if (c <= a && a <= b) {
        printf("%d %d %d\n", c, a, b);
    } else {
        printf("%d %d %d\n", c, b, a);
    }

    return 0;
}