COMP1511 19T2
COMP1511 19T2

Objectives

  • using complex if statements to control program execution
  • using a while loop for repetition
  • using arrays to remember sequences of numbers
  • 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.

Getting Started

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

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

Autotest Results

98% of 507 students who have autotested perfect.c so far, passed all autotest tests.
  • 98% passed test perfect_0
  • 98% passed test perfect_1
  • 98% passed test perfect_2
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab04_perfect perfect.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 to obtain the marks for this lab exercise.

Exercise: Reverse Array (pair)

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

Write a C program, reverse_array.c, which reads integers line by line, and when it reaches the end of input, prints those integers in reverse order, line by line.

You will never be given more than 100 integers to print out.

The output from your program should look exactly like this:

dcc reverse_array.c -o reverse_array
./reverse_array
Enter numbers forwards:
10
50
20
40 
Reversed:
40
20
50
10
./reverse_array
Enter numbers forwards:
-5
-4
-3
-2
-1 
Reversed:
-1
-2
-3
-4
-5

Need a Hint?

The result from calling scanf can be assigned to an integer, like so:


int scanned_in_value;
int result = scanf("%d", &scanned_in_value);

result will be equal to the number of variables that scanf successfully scanned in.

Explanation
In the code sample above, if the user does enter a number, scanf will be able to read this in to the variable scanned_in_value, and thus it will have successfully scanned in one value, so the variable result will contain the value 1.

If the user had not entered a valid number (for example if they typed in a word instead), there would be no integer there for scanf to read into scanned_in_value, so scanf would not successfully scan in any values, and so the variable result would contain the value 0.

Detecting the end of input
The variable result can be used to determine whether your program has reached the end of the input (hint: if there aren't any values left for scanf to scan in, the result variable won't contain the number 1).

If you have a while loop condition that checks the result of scanf to determine whether scanf was able to successfully scan the expected number of variables, you can use this information to keep scanning numbers until the user tells you to stop.

Note: on Mac/Linux, you can signal "end of input" on the command line by pressing Control + D

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

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

1511 autotest reverse_array

Autotest Results

96% of 485 students who have autotested reverse_array.c so far, passed all autotest tests.
  • 98% passed test 0
  • 98% passed test 1
  • 98% passed test 2
  • 98% passed test 3
  • 97% passed test 4
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab04_reverse_array reverse_array.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 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.

This exercise must be completed without Arrays.

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
########  #################
########  #################
########  #################
########  #################
########  #################


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

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

1511 autotest danish_flag

Autotest Results

96% of 472 students who have autotested danish_flag.c so far, passed all autotest tests.
  • 97% passed test danishFlag_1
  • 96% passed test danishFlag_2
  • 96% passed test danishFlag_3
  • 96% passed test danishFlag_4
  • 96% passed test danishFlag_5
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab04_danish_flag danish_flag.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 01 July 17:00 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 integer 1.

This exercise can be completed with or without arrays. Either way you can assume that you will not be tested on any values of n above 50.

For example:

./boxes
How many boxes: 1
111
101
111
./boxes
How many boxes: 2
1111111
1000001
1011101
1010101
1011101
1000001
1111111
./boxes
How many boxes: 5
1111111111111111111
1000000000000000001
1011111111111111101
1010000000000000101
1010111111111110101
1010100000000010101
1010101111111010101
1010101000001010101
1010101011101010101
1010101010101010101
1010101011101010101
1010101000001010101
1010101111111010101
1010100000000010101
1010111111111110101
1010000000000000101
1011111111111111101
1000000000000000001
1111111111111111111
New! You can run an automated code style checker using the following command:
1511 style boxes.c

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

1511 autotest boxes

Autotest Results

90% of 112 students who have autotested boxes.c so far, passed all autotest tests.
  • 96% passed test boxes_0
  • 97% passed test boxes_1
  • 94% passed test boxes_10
  • 94% passed test boxes_11
  • 94% passed test boxes_12
  • 94% passed test boxes_13
  • 94% passed test boxes_14
  • 94% passed test boxes_15
  • 94% passed test boxes_16
  • 94% passed test boxes_17
  • 94% passed test boxes_18
  • 94% passed test boxes_19
  • 95% passed test boxes_2
  • 94% passed test boxes_20
  • 95% passed test boxes_3
  • 95% passed test boxes_4
  • 95% passed test boxes_5
  • 94% passed test boxes_6
  • 94% passed test boxes_7
  • 94% passed test boxes_8
  • 94% passed test boxes_9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab04_boxes boxes.c
You must run give before Monday 01 July 17:00 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.

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

Autotest Results

94% of 50 students who have autotested spiral.c so far, passed all autotest tests.
  • 96% passed test spiral_0
  • 96% passed test spiral_1
  • 96% passed test spiral_2
  • 94% passed test spiral_3
  • 94% passed test spiral_4
  • 94% passed test spiral_5
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab04_spiral spiral.c
You must run give before Monday 01 July 17:00 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.

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

Autotest Results

88% of 17 students who have autotested decimal_spiral.c so far, passed all autotest tests.
  • 88% passed test decimal_spiral_0
  • 88% passed test decimal_spiral_1
  • 88% passed test decimal_spiral_2
  • 88% passed test decimal_spiral_3
  • 88% passed test decimal_spiral_4
  • 88% passed test decimal_spiral_5
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab04_decimal_spiral decimal_spiral.c
You must run give before Monday 01 July 17:00 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 Monday 01 July 17: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
The lab exercises for each week are worth in total 2 marks.

The best 8 of your 9 lab marks for weeks 2-10 will be summed to give you a mark out of 13. If their sum exceeds 13 - your mark will be capped at 13.

  • You can obtain full marks for the labs without completing challenge exercises
  • You can miss 1 lab without affecting your mark.