Programming Fundamentals

Information

  • This page contains extra challenge exercises for week 02.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • These exercises are intended for students that want more challenge 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.

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;
}

Exercise
(●●●)
:

Ordering Three Integers Without If Statements

Write a C program order3_challenge1.c that reads 3 integers and prints them from smallest to largest.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,

  • You are not permitted to use if statements
  • You are not permitted to use loops (e.g. while)
  • You are not permitted to use the ternary ? operator
  • You are not permitted to use arrays. You are not permitted to define functions

You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.

You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).

You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).

For example, both of these are invalid:

(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid

You can use printf to print the value of an expression, in other words you can have an expression inside printf.

Examples

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

This is more puzzle than a programming exercise.

Try to invent your own solution - don't google or ask others how do it.

Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.

Sample solution for order3_challenge1.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
// without using functions: if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures

// This is a puzzle not a programming exercises

#include <stdio.h>

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

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

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

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

    tmp = b;
    b = a - (1 - (a > b)) * (a - b);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = a - (1 - (a > c)) * (a - c);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = b - (1 - (b > c)) * (b - c);
    b = b - (1 - (b < tmp)) * (b - tmp);

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

    return 0;
}
Alternative solution for order3_challenge1.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
// without using functions: if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures

// This is a puzzle not a programming exercises

#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;
    }

    tmp = b;
    b = a - (1 - (a > b)) * (a - b);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = a - (1 - (a > c)) * (a - c);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = b - (1 - (b > c)) * (b - c);
    b = b - (1 - (b < tmp)) * (b - tmp);

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

    return 0;
}

Exercise
(●●●)
:

Ordering Three Integers Without If Statements and With Only Three Variables

Write a C program order3_challenge1.c that reads 3 integers and prints them from smallest to largest.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,

  • You are not permitted to use if statements
  • You are not permitted to use loops (e.g. while)
  • You are not permitted to use the ternary ? operator
  • You are not permitted to use arrays. You are not permitted to define functions

For this exercise you also cannot use more than 3 variables

You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.

You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).

You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).

For example, both of these are invalid:

(a &lt; b) &amp;&amp; printf("a"); // invalid
(a &lt; b) &amp;&amp; (a = b); // invalid

You can use printf to print the value of an expression, in other words you can have an expression inside printf.

Examples

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

This is more puzzle than a programming exercise.

Try to invent your own solution - don't google or ask others how do it.

Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.

Sample solution for order3_challenge2.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
// without using: functions, if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures
// and only using 3 int variables

// This is a puzzle not a programming exercises

#include <stdio.h>

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

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

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

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

    printf("The integers in order are:");
    printf(" %d", a - (1 - (a < (b - (1 - (b < c)) * (b - c)))) * (a - (b - (1 - (b < c)) * (b - c))));
    printf(" %d", (a - (1 - (a < b)) * (a - b)) - (1 - ((a - (1 - (a < b)) * (a - b)) > (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b)))))) * ((a - (1 - (a < b)) * (a - b)) - (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b))))));
    printf(" %d", a - (1 - (a > (b - (1 - (b > c)) * (b - c)))) * (a - (b - (1 - (b > c)) * (b - c))));
    printf("\n");

    return 0;
}
Alternative solution for order3_challenge2.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
// without using: functions, if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures
// and only using 3 int variables

// This is a puzzle not a programming exercises

#include <stdio.h>

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

    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;
    }

    printf("The integers in order are:");
    printf(" %d", a - (1 - (a < (b - (1 - (b < c)) * (b - c)))) * (a - (b - (1 - (b < c)) * (b - c))));
    printf(" %d", (a - (1 - (a < b)) * (a - b)) - (1 - ((a - (1 - (a < b)) * (a - b)) > (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b)))))) * ((a - (1 - (a < b)) * (a - b)) - (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b))))));
    printf(" %d", a - (1 - (a > (b - (1 - (b > c)) * (b - c)))) * (a - (b - (1 - (b > c)) * (b - c))));
    printf("\n");

    return 0;
}

Exercise
(●●●)
:

Labour Day

Labour Day is a public holiday that is held in NSW on the first Monday of October. Related link https://en.wikipedia.org/wiki/Labour_Day

Write a program named labour_day.c that outputs the date of Labour Day in NSW when provided a given year.

Examples

dcc labour_day.c -o labour_day
./labour_day
Enter a year: 2023
In 2023, Labour Day is October 2nd
./labour_day
Enter a year: 2031
In 2031, Labour Day is October 6th
./labour_day
Enter a year: 2016
In 2016, Labour Day is October 3rd
Sample solution for labour_day.c
// Program that calculates the NSW Labour Day public holiday for a given year
// Author: George Muscat (z5363683) 2024

#include <stdio.h>

#define MONDAY 2
#define WEEK 7
#define OCTOBER 10

int main(void) {
    int year;

    printf("Enter Year: ");
    scanf("%d", &year);

    int day = 1;

    // Calculate which day of the week is the first of October
    int first_oct = (day + (13 * (OCTOBER + 1) / 5) + (year % 100) +
        ((year % 100) / 4) + ((year / 100) / 4) + 5 * (year / 100)) % 7;

    int first_monday_date;
    if (first_oct <= MONDAY) {
        first_monday_date = 1 + (MONDAY - first_oct);
    } else {
        first_monday_date = 1 + (MONDAY + WEEK - first_oct);
    }

    printf("In %d, Labour Day is October %d", year, first_monday_date);

    // Decide suffix
    if (first_monday_date == 1) {
        printf("st\n");
    } else if (first_monday_date == 2) {
        printf("nd\n");
    } else if (first_monday_date == 3) {
        printf("rd\n");
    } else {
        printf("th\n");
    }

    return 0;
}
Alternative solution for labour_day.c
// Alternative program that calculates the NSW Labour Day public holiday for a given year
// Author: George Muscat (z5363683) 2024

/*
    NOTE: This is another solution that introduces multiple concepts outside of
            1511 scope at this point. This has been provided to give curious students
            food for thought and material to research in order to understand the program.
*/

#include <stdio.h>

#define MONDAY 2
#define WEEK 7
#define OCTOBER 10


int day_of_the_week(int day, int month, int year);

char *get_suffix(int day);

int main(void) {
    int year;

    printf("Enter Year: ");
    scanf("%d", &year);

    int first_oct = day_of_the_week(1, OCTOBER, year);

    // This for loop could be considered bad style!
    int first_monday;
    for (first_monday = first_oct; first_monday % WEEK != MONDAY; first_monday++);

    int first_monday_date = first_monday - first_oct + 1;
    printf("In %d, Labour Day is October %d%s\n", year, first_monday_date, get_suffix(first_monday_date));

    return 0;
}

int day_of_the_week(int day, int month, int year) {
    return (day + (13 * (month + 1) / 5) + (year % 100) +
        ((year % 100) / 4) + ((year / 100) / 4) + 5 * (year / 100)) % 7;
}

char *get_suffix(int day) {
    switch (day) {
        case 1:
            return "st";
        case 2:
            return "nd";
        case 3:
            return "rd";
        default:
            return "th";
    }
}