COMP1511 17s1 Introduction to Programming

Objectives

In this Lab, you will practice:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

Getting Started

One member of your programming pair should login and run the following commands inside a Linux terminal

Create a new directory for this lab called lab03 by typing:

mkdir lab03
Change to this directory by typing:
cd lab03

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.

Your program should behave exactly like this example:

./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 you have order3.c working use autotest to test it further:

~cs1511/bin/autotest lab03 order3.c
Test order3_0 (1 2 3) - passed
Test order3_1 (99 50 1) - passed
Test order3_2 (45 99 87) - passed
Test order3_3 (100 4 45) - passed
Test order3_4 (50 60 20) - passed
Test order3_5 (50 30 90) - passed
Test order3_6 (20 20 20) - passed
Test order3_7 (20 20 30) - passed
Test order3_8 (30 30 20) - passed
Test order3_9 (30 20 30) - passed
Sample solution for order3.c
// Modified 3/3/2017 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;
}

Exercise: Is this a Leap Year?

Write a C program is_leap_year.c that reads a year and then prints whether that year is a leap year.

Match the examples below exactly

Hint: you only need use the int type, modulus (%) and if statement(s).

Hint: if you are clever with && and || it just needs one if statement.

For example:

dcc -o is_leap_year is_leap_year.c
./is_leap_year
Enter year: 2017
2017 is not a leap year.
./is_leap_year
Enter year: 2016
2016 is a leap year.
./is_leap_year
Enter year: 2000
2000 is a leap year.
./is_leap_year
Enter year: 3000
3000 is not a leap year.
When you think you have is_leap_year.c working use autotest to test it further:
~cs1511/bin/autotest lab03 is_leap_year.c
Test is_leap_year_0 (2020) - passed
Test is_leap_year_1 (2019) - passed
Test is_leap_year_2 (2018) - passed
Test is_leap_year_3 (2017) - passed
Test is_leap_year_4 (2016) - passed
Test is_leap_year_5 (2000) - passed
Test is_leap_year_6 (2100) - passed
Test is_leap_year_7 (1900) - passed
Test is_leap_year_8 (1800) - passed
Test is_leap_year_9 (1000) - passed
Sample solution for is_leap_year.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Test if a year is leap year
// https://en.wikipedia.org/wiki/Leap_year
#include <stdio.h>

int main(void) {
    int year;

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

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }

    return 0;
}

Exercise: Cubes

Write a program called cubes.c that reads a positive integer n and then prints the cubes of the integers from 1 to n.

Your program should print exactly this output:

./cubes 
Enter how many cubes: 4
1^3 = 1
2^3 = 8
3^3 = 27
4^3 = 64
./cubes 
Enter how many cubes: 16
1^3 = 1
2^3 = 8
3^3 = 27
4^3 = 64
5^3 = 125
6^3 = 216
7^3 = 343
8^3 = 512
9^3 = 729
10^3 = 1000
11^3 = 1331
12^3 = 1728
13^3 = 2197
14^3 = 2744
15^3 = 3375
16^3 = 4096
Make sure your program matches the above output perfectly.

As usual autotest is available to help you test your program.

 ~cs1511/bin/autotest lab03 cubes.c
Sample solution for cubes.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print first n cubes

#include <stdio.h>

int main(void) {
    int i, how_many_cubes;

    printf("Enter how many cubes: ");
    if (scanf("%d", &how_many_cubes) != 1) {
        return 1;
    }

    i = 1;
    while (i <= how_many_cubes) {
        printf("%d^3 = %d\n", i, i * i * i);
        i = i + 1;
    }

    return 0;
}

Exercise: Lots of Leap Years

Write a C program leap_years.c that reads a start year and finish year and then prints all the leap years that lie in that range.

Match the examples below exactly

Hint: you only need use the int type, modulus (%) and a while loop.

For example:

./leap_years 
Enter start year: 1995
Enter finish year: 2020
The leap years between 1995 and 2020 are: 1996 2000 2004 2008 2012 2016 2020
./leap_years 
Enter start year: 1780
Enter finish year: 1813
The leap years between 1780 and 1813 are: 1780 1784 1788 1792 1796 1804 1808 1812
./leap_years 
Enter start year: 997
Enter finish year: 1003
The leap years between 997 and 1003 are:
When you think you have leap_year.c working use autotest to test it further:
~cs1511/bin/autotest lab03 leap_years.c
Test 23 (1980 2020) - passed
Test 24 (1492 1508) - passed
Test 25 (1770 1810) - passed
Test 26 (4444 5555) - passed
Sample solution for leap_years.c
#include <stdio.h>

int main(void) {
    int year, start_year, finish_year;

    printf("Enter start year: ");
    if (scanf("%d", &start_year) != 1) {
        return 1;
    }

    printf("Enter finish year: ");
    if (scanf("%d", &finish_year) != 1) {
        return 1;
    }

    printf("The leap years between %d and %d are:", start_year, finish_year);
    year = start_year;
    while (year <= finish_year) {
        if (year % 400 == 0 || (year % 4 == 0 && year % 100)) {
            printf(" %d", year);
        }
        year = year + 1;
    }
    printf("\n");

    return 0;
}

Exercise: Sum Cubes

Write a program called sum_cubes.c that reads a positive integer n and then prints the sum of the cubes of the integers from 1 to n,

Your program should print exactly this output:

./sum_cubes
Sum how many cubes? 4
1^3 + 2^3 + 3^3 + 4^3 = 100
./sum_cubes
Sum how many cubes? 10
1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3 + 10^3 = 3025
./sum_cubes
Sum how many cubes? 16
1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3 + 10^3 + 11^3 + 12^3 + 13^3 + 14^3 + 15^3 + 16^3 = 18496
./sum_cubes
Sum how many cubes? 1
1^3 = 1
Make sure your program matches the above output perfectly.

As usual autotest is available to help you test your program.

 ~cs1511/bin/autotest lab03 sum_cubes.c
Sample solution for sum_cubes.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Sum the  first n cubes

#include <stdio.h>

int main(void) {
    int i, how_many_cubes, sum;

    printf("Sum how many cubes? ");
    if (scanf("%d", &how_many_cubes) != 1) {
        return 1;
    }

    if (how_many_cubes < 1) {
        return 0;
    }

    i = 1;
    sum = 0;
    while (i <= how_many_cubes) {
        if (i != 1) {
            printf(" + ");
        }
        printf("%d^3", i);
        sum = sum + i * i * i;
        i = i + 1;
    }

    printf(" = %d\n", sum);

    return 0;
}

Challenge Exercise: Print a Square with One Loop

Write a C program one_loop_square.c that reads an integer and prints a square of asterisks that size.

In lectures we cover how to solve this using nested while loops and three variables.

In this challenge exercises, you are only permited to use a single while loop and only two variables which must be of type int.

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 only permitted to use parts of C covered in the weeks 1 and 2 lectures.

Your program should behave exactly like this example:

./one_loop_square
Square size: 1
*
./one_loop_square
Square size: 2
**
**
./one_loop_square
Square size: 7
*******
*******
*******
*******
*******
*******
*******
This is as much a puzzle as a programming exercise but the techniques you need to use can be useful elsewhere.

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

 ~cs1511/bin/autotest lab03 one_loop_square.c
Sample solution for one_loop_square.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print a square of asterisks using a single while loop

#include <stdio.h>

int main(void) {
    int i, n;

    printf("Square size: ");
    if (scanf("%d", &n) != 1) {
        return 1;
    }
    i = 0;
    while (i < n * n) {
        printf("*");
        i = i + 1;
        // we printed n asterisks so print a newline character
        if (i % n == 0) {
            printf("\n");
        }
    }

    return 0;
}

Challenge 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 not permitted to use if statements.

You are not permitted to use loops (e.g. while).

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 use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures). You can use printf to print the value of an expression, in other words you can have an expression inside printf.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. For example, you are not permitted to use the ternary ?: operator. You are not permitted to define functions.

Your program should behave exactly like this example:

./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. Tutors will reward interesting, original or weird approaches even if they don't work.

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

 ~cs1511/bin/autotest lab03 order3_challenge1.c
Sample solution for order3_challenge1.c
// Modified 3/3/2017 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;
}

Challenge Exercise: Ordering Three Integers Without If Statements and With Only 3 Variables

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

You are only permitted to have 3 variables in your program and they must be of type int.

The restrictions of the previous challenge exercise also apply.

You are not permited to use if statements.

You are not permitted to use loops (e.g. while).

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 can use printf to print the value of an expression, in other words you can have an expression inside printf.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. For example, you are not permitted to use the ternary ?: operator. You are not permitted to define functions.

Your program should behave exactly like this example:

./order3_challenge2
Enter integer: 23
Enter integer: 5
Enter integer: 27
The integers in order are: 5 23 27
./order3_challenge2
Enter integer: 3
Enter integer: 6
Enter integer: 27
The integers in order are: 3 6 27
./order3_challenge2
Enter integer: 9
Enter integer: 8
Enter integer: 7
The integers in order are: 7 8 9
This is much more puzzle than a programming exercise.

Try to invent your own solution - don't google or ask others how do it. Tutors will reward interesting, original or weird approaches even if they don't work.

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

 ~cs1511/bin/autotest lab03 order3_challenge2.c
Sample solution for order3_challenge2.c
// Modified 3/3/2017 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;
}

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You also need to submit your work electronically by typing (run this command in the lab03 directory):
give cs1511 lab03 order3.c is_leap_year.c cubes.c leap_years.c sum_cubes.c one_loop_square.c order3_challenge1.c order3_challenge2.c
Submit the challenge exercises only if you attempt them.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by Monday 11:00am using give and ask your tutor to assess them at the start of the following lab.

Either or both members of a programming pair can submit the work (make sure each program lists both of you as authors in the header comment).