Week 03 Laboratory Exercises

Objectives

  • implementing simple numerical calculations
  • using complex if statements to control program execution
  • using while loops for repetition

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started

Exercise — in pairs:
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.
New! You can run an automated code style checker using the following command:
1511 style order3.c

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

1511 autotest order3

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_order3 order3.c

Note, even though this is a pair exercise, you both must run give from your own account before Friday 08 March 20:00 to obtain the marks for this lab exercise.

Exercise — in pairs:
Countdown

We use loops in C to do things multiple times. Here is an example of a loop that prints all the numbers from 1 to 17 on a new line in ascending order:

//initialise counter to 1
int counter = 1; 
// loop until not <= 17
while (counter <= 17) { // Have printed all numbers between 1 and counter
    // print counter
    printf("%d\n", counter);
    // increment counter
    counter = counter + 1; 
} // counter == 18

In this exercise you will use a loop to print a countdown from 10 to 0. Start by creating a file called countdown.c, and copying the above code. Modify this code so that the loop counts down from 10 until 0.

Example

./countdown
10
9
8
7
6
5
4
3
2
1
0
New! You can run an automated code style checker using the following command:
1511 style countdown.c

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

1511 autotest countdown

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_countdown countdown.c

Note, even though this is a pair exercise, you both must run give from your own account before Friday 08 March 20:00 to obtain the marks for this lab exercise.

Exercise — in pairs:
Three or Five

Write a program three_five.c that reads a positive integer n and print all the positive integers < n divisible by 3 or 5. All of the numbers printed should be in ascending order.

For example:

 ./three_five
Enter number: 10
3
5
6
9
./three_five
Enter number: 30
3
5
6
9
10
12
15
18
20
21
24
25
27
New! You can run an automated code style checker using the following command:
1511 style three_five.c

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

1511 autotest three_five

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_three_five three_five.c

Note, even though this is a pair exercise, you both must run give from your own account before Friday 08 March 20:00 to obtain the marks for this lab exercise.

Exercise — in pairs:
Are You Perfect

Write a program perfect.c that reads a positive integer n from standard input and prints all the factors of n, their sum and indicates whether n is a perfect number.
 ./perfect
Enter number: 6
The factors of 6 are:
1
2
3
6
Sum of factors = 12
6 is a perfect number
./perfect
Enter number: 1001
The factors of 1001 are:
1
7
11
13
77
91
143
1001
Sum of factors = 1344
1001 is not a perfect number
New! You can run an automated code style checker using the following command:
1511 style perfect.c

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

1511 autotest perfect

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_perfect perfect.c

Note, even though this is a pair exercise, you both must run give from your own account before Friday 08 March 20:00 to obtain the marks for this lab exercise.

Exercise — in pairs:
Draw a Hollow Triangle

Write a program called hollow_triangle.c that reads an integer n from standard input. and prints a pattern of asterisks forming a hollow triangle.

You can assume n is greater than 3.

Make your program match the examples below exactly.

Note: you are not permitted to use an array in this exercise.

dcc hollow_triangle.c -o hollow_triangle
./hollow_triangle 
Enter size: 4
*
**
* *
****
./hollow_triangle 
Enter size: 5
*
**
* *
*  *
*****
./hollow_triangle
Enter size: 8
*
**
* *
*  *
*   *
*    *
*     *
********
./hollow_triangle
Enter size: 11
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
***********
New! You can run an automated code style checker using the following command:
1511 style hollow_triangle.c

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

1511 autotest hollow_triangle

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_hollow_triangle hollow_triangle.c

Note, even though this is a pair exercise, you both must run give from your own account before Saturday 23 March 17:00 to obtain the marks for this lab exercise.

Exercise — in pairs:
Draw an X

Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X".

You can assume n is odd and >= 5.

Make your program match the examples below exactly.

This exercise is designed to give you practice with while loops, if statements and some mathematical operators. Do not use arrays for this exercise!

Note: you are not permitted to use an array in this exercise.

./x
Enter size: 5
*---*
-*-*-
--*--
-*-*-
*---*
./x
Enter size: 9
*-------*
-*-----*-
--*---*--
---*-*---
----*----
---*-*---
--*---*--
-*-----*-
*-------*
./x
Enter size: 15
*-------------*
-*-----------*-
--*---------*--
---*-------*---
----*-----*----
-----*---*-----
------*-*------
-------*-------
------*-*------
-----*---*-----
----*-----*----
---*-------*---
--*---------*--
-*-----------*-
*-------------*
New! You can run an automated code style checker using the following command:
1511 style x.c

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

1511 autotest x

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_x x.c

Note, even though this is a pair exercise, you both must run give from your own account before Friday 08 March 20:00 to obtain the marks for this lab exercise.

Challenge Exercise — individual:
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 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.

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 use arrays. You are not permitted to define functions.

You should invent your own solution - don't just google or ask others how do it!

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.

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

New! You can run an automated code style checker using the following command:
1511 style order3_challenge1.c

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

1511 autotest order3_challenge1

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab03_order3_challenge1 order3_challenge1.c

You must run give before Friday 08 March 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Challenge Exercise — individual:
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 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 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.

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 use arrays. You are not permitted to define functions.

You should invent your own solution - don't just google or ask others how do it!

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.

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

New! You can run an automated code style checker using the following command:
1511 style order3_challenge2.c

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

1511 autotest order3_challenge2

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab03_order3_challenge2 order3_challenge2.c

You must run give before Friday 08 March 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Challenge Exercise — individual:
Spiral

Write a program called spiral.c that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of a spiral.

You can assume n is odd and >= 5.

This exercise must be completed without arrays.

Make your program match the examples below exactly.

./spiral
Enter size: 5
*****
----*
***-*
*---*
*****
./spiral
Enter size: 7
*******
------*
*****-*
*---*-*
*-***-*
*-----*
*******
./spiral
Enter size: 9
*********
--------*
*******-*
*-----*-*
*-***-*-*
*-*---*-*
*-*****-*
*-------*
*********
./spiral
Enter size: 17
*****************
----------------*
***************-*
*-------------*-*
*-***********-*-*
*-*---------*-*-*
*-*-*******-*-*-*
*-*-*-----*-*-*-*
*-*-*-***-*-*-*-*
*-*-*-*---*-*-*-*
*-*-*-*****-*-*-*
*-*-*-------*-*-*
*-*-*********-*-*
*-*-----------*-*
*-*************-*
*---------------*
*****************
New! You can run an automated code style checker using the following command:
1511 style spiral.c

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

1511 autotest spiral

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab03_spiral spiral.c

You must run give before Friday 08 March 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Extra-hard challenge: Decimal Spiral (individual - attempt if you dare)

Write a program called decimal_spiral.c that reads an integer n from standard input. and prints an nxn pattern of decimal digits and dashes in the shape of a spiral.

You can assume n is odd and >= 5.

This exercise must be done without arrays.

Make your program match the examples below exactly.

./decimal_spiral
Enter size: 5
65432
----1
210-0
3---9
45678
./decimal_spiral
Enter size: 7
0987654
------3
87654-2
9---3-1
0-012-0
1-----9
2345678
./decimal_spiral
Enter size: 9
876543210
--------9
8765432-8
9-----1-7
0-210-0-6
1-3---9-5
2-45678-4
3-------3
456789012
./decimal_spiral
Enter size: 15
654321098765432
--------------1
2109876543210-0
3-----------9-9
4-210987654-8-8
5-3-------3-7-7
6-4-87654-2-6-6
7-5-9---3-1-5-5
8-6-0-012-0-4-4
9-7-1-----9-3-3
0-8-2345678-2-2
1-9---------1-1
2-01234567890-0
3-------------9
456789012345678
New! You can run an automated code style checker using the following command:
1511 style decimal_spiral.c

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

1511 autotest decimal_spiral

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab03_decimal_spiral decimal_spiral.c

You must run give before Friday 08 March 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

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

Remember you have until Week 3 Sunday 20:00 to submit your work.

You cannot obtain marks by e-mailing lab work to tutors or lecturers.

You check the files you have submitted here

Automarking will be run by the lecturer several days after the submission deadline for the test, using test cases that you haven't seen: different to the test cases autotest runs for you.

(Hint: do your own testing as well as running autotest)

After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec