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

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

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

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

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

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

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

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

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