COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Objectives

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

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

Create a new directory for this lab called lab04 by typing:
mkdir lab04
Change to this directory by typing:
cd lab04

Exercise: Fun facts about Circles (pair)

This is a pair exercise to complete with your lab partner.
Here is a program circle_facts.c which calculates some fun facts about circles.

Unfortunately it is incomplete. Your task is to complete it.

Its main function is complete. Do not main function change. The three functions to change are>

double area(double radius);
double circumference(double radius);
double diameter(double radius);

Hint use the constant M_PI defined in math.h

dcc -o circle_facts  circle_facts.c 
./circle_facts 
Enter circle radius: 1
Area          = 3.141593
Circumference = 6.283185
Diameter      = 2.000000
./circle_facts
Enter circle radius: 17
Area          = 907.920277
Circumference = 106.814150
Diameter      = 34.000000
./circle_facts
Enter circle radius: 0.0125
Area          = 0.000491
Circumference = 0.078540
Diameter      = 0.025000

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

1511 autotest circle_facts
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_circle_facts circle_facts.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Exercise: Countdown (pair)

This is a pair exercise to complete with your lab partner.

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:


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

In this exercise you will use a loop to print a countdown from 10 to 0. Start by creating a file called countdown.c in your week 4 directory, 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

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

1511 autotest countdown
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_countdown countdown.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Exercise: Three or Five (pair)

This is a pair exercise to complete with your lab partner.
Write a program that three_five.c that reads a positive integer n and print all the positive integers < n divisible by 3 or 5.

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

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 on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_three_five three_five.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Exercise: Are You Perfect (pair)

This is a pair exercise to complete with your lab partner.
Write a program perfect.c that reads a positive integer n from standard input and prints all the factors of n, their sum and if indicates if 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

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

1511 autotest perfect
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_perfect perfect.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Exercise: X Factor (pair)

This is a pair exercise to complete with your lab partner.
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.

You are not permitted to use an array in this exercise.

./x
Enter size: 5
*---*
-*-*-
--*--
-*-*-
*---*
./x
Enter size: 9
*-------*
-*-----*-
--*---*--
---*-*---
----*----
---*-*---
--*---*--
-*-----*-
*-------*
./x
Enter size: 15
*-------------*
-*-----------*-
--*---------*--
---*-------*---
----*-----*----
-----*---*-----
------*-*------
-------*-------
------*-*------
-----*---*-----
----*-----*----
---*-------*---
--*---------*--
-*-----------*-
*-------------*

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

1511 autotest x
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_x x.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Exercise: Danish Flag (pair)

This is a pair exercise to complete with your lab partner.

In this task, you will create a program called danish_flag.c which will read in a size and display a Danish Flag of that size using the following algorithm and the # character.

The Danish Flag is made up of 6 blocks. It is 3 blocks wide, and 2 blocks high. To display it using empty spaces and the character #, we will read in a value called size. Each block will be 3 times size wide, and 2 times size high.

  • In the top left block, the right column and bottom row will be empty spaces.
  • In the top middle block, the left column and bottom row will be empty spaces.
  • In the top right block, the bottom row will be empty spaces.
  • In the bottom left block, the right column and top row will be empty spaces.
  • In the bottom middle block, the left column and top row will be empty spaces.
  • In the bottom right block, the top row will be empty spaces.
  • Every other position will be the # character.

See the diagram below for details.

For example

dcc -o danish_flag  danish_flag.c 
./danish_flag
Enter the flag size: 1
##  #####


##  #####
./danish_flag
Enter the flag size: 2
#####  ###########
#####  ###########
#####  ###########


#####  ###########
#####  ###########
#####  ###########
./danish_flag
Enter the flag size: 3
########  #################
########  #################
########  #################
########  #################
########  #################


########  #################
########  #################
########  #################
########  #################
########  #################

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

1511 autotest danish_flag
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk04_danish_flag danish_flag.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise.

Challenge Exercise: Boxes (individual)

This is an individual exercise to complete by yourself.

For this challenge, make a program called boxes.c which reads in a number and then draws that many square boxes inside each other using the character #.

For example:

./boxes
How many boxes: 1
###
# #
###
./boxes
How many boxes: 2
#######
#     #
# ### #
# # # #
# ### #
#     #
#######
./boxes
How many boxes: 5
###################
#                 #
# ############### #
# #             # #
# # ########### # #
# # #         # # #
# # # ####### # # #
# # # #     # # # #
# # # # ### # # # #
# # # # # # # # # #
# # # # ### # # # #
# # # #     # # # #
# # # ####### # # #
# # #         # # #
# # ########### # #
# #             # #
# ############### #
#                 #
###################

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

1511 autotest boxes
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk04_boxes boxes.c
You must run give before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Challenge Exercise: Spiral (individual)

This is an individual exercise to complete by yourself.
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.

You are only permitted to use C language features covered in weeks 1-3 lectures. In particular, you are not permitted to use array(s).

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

./spiral
Enter size: 5
*****
----*
***-*
*---*
*****
./spiral
Enter size: 7
*******
------*
*****-*
*---*-*
*-***-*
*-----*
*******
./spiral
Enter size: 9
*********
--------*
*******-*
*-----*-*
*-***-*-*
*-*---*-*
*-*****-*
*-------*
*********
./spiral
Enter size: 17
*****************
----------------*
***************-*
*-------------*-*
*-***********-*-*
*-*---------*-*-*
*-*-*******-*-*-*
*-*-*-----*-*-*-*
*-*-*-***-*-*-*-*
*-*-*-*---*-*-*-*
*-*-*-*****-*-*-*
*-*-*-------*-*-*
*-*-*********-*-*
*-*-----------*-*
*-*************-*
*---------------*
*****************

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 wk04_spiral spiral.c
You must run give before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise. Note, 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)

This is an individual exercise to complete by yourself.
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.

You are only permitted to use C language features covered in weeks 1-3 lectures. In particular, you are not permitted to use array(s).

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

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

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 wk04_decimal_spiral decimal_spiral.c
You must run give before Sunday 25 March 23:59:59 to obtain the marks for this lab exercise. Note, 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 Sunday 25 March 23:59:59 to submit your work.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface

You can read more about lab assessment here