- using complex if statements to control program execution
- using a while loop for repetition
- input & output of numerical values
- implementing simple numerical calculations

Create a new directory for this lab called `lab03`

by typing:

Change to this directory by typing:mkdir lab03

cd lab03

`order3.c`

using Your program should behave exactly like this example:

You can assume the user supplies 3 integers. You do not have to check the return value from scanf../order3Enter integer:23Enter integer:5Enter integer:27The integers in order are: 5 23 27./order3Enter integer:3Enter integer:6Enter integer:27The integers in order are: 3 6 27./order3Enter integer:9Enter integer:8Enter integer:7The integers in order are: 7 8 9

When you think you have `order3.c`

working use autotest to test it further:

~cs1511/bin/autotest lab03 order3.cTest 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; }

`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:

When you think you havedcc -o is_leap_year is_leap_year.c./is_leap_yearEnter year:20172017 is not a leap year../is_leap_yearEnter year:20162016 is a leap year../is_leap_yearEnter year:20002000 is a leap year../is_leap_yearEnter year:30003000 is not a leap year.

`is_leap_year.c`

working use autotest to test it further:
~cs1511/bin/autotest lab03 is_leap_year.cTest 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; }

`cubes.c`

that reads a positive integer
Your program should print **exactly** this output:

Make sure your program matches the above output./cubesEnter how many cubes: 4 1^3 = 1 2^3 = 8 3^3 = 27 4^3 = 64./cubesEnter 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

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

`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:

When you think you have./leap_yearsEnter start year:1995Enter finish year:2020The leap years between 1995 and 2020 are: 1996 2000 2004 2008 2012 2016 2020./leap_yearsEnter start year:1780Enter finish year:1813The leap years between 1780 and 1813 are: 1780 1784 1788 1792 1796 1804 1808 1812./leap_yearsEnter start year:997Enter finish year:1003The leap years between 997 and 1003 are:

`leap_year.c`

working use autotest to test it further:
~cs1511/bin/autotest lab03 leap_years.cTest 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; }

`sum_cubes.c`

that reads a positive integer
Your program should print **exactly** this output:

Make sure your program matches the above output./sum_cubesSum how many cubes?41^3 + 2^3 + 3^3 + 4^3 = 100./sum_cubesSum how many cubes?101^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3 + 10^3 = 3025./sum_cubesSum how many cubes?161^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_cubesSum how many cubes?11^3 = 1

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

`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:

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

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

`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:

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

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

`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:

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

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

`lab03`

directory):
Submit the challenge exercises only if you attempt them.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

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).