Programming Fundamentals

Objectives

  • using complex if statements to control program execution
  • using while loops for repetition
  • building up combinations of loops
  • defining and using enums and structs

Feedback Week!

In this week's lab, your tutors will go around the class and give you one on one feedback on some code you have written in a previous week.

Take this as an opportunity to ask any questions you might have about the course content so far!

So, if you would like, have a think about if there is any particular exercise you would like to receive feedback for, or any particular content you would like to ask about.

1511 Cheatsheet

When you are finished with the exercises for this week, take a look at the 1511 Cheatsheet. This page has useful linux commands and VSCode keyboard shortcuts that you may find helpful when programming!

Reminder: Help sessions

Help sessions are running this week!

These are one of the best ways for you to get one on one help with a tutor for any course content (including Lab Exercises and Assignments).

For the dates and times of the help sessions, see the Help Session Timetable.

To join a help session, or for more information, see the COMP(1511|1911) Help Session Microsoft Teams.

For face-to-face help sessions, the lab map can be found here. If help sessions are running in other buildings you can use Lost on Campus to help you find them.

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth 1 mark(s) in total:

  • count_up_down
  • vector_sum
  • debug_factorial
  • print_grid

Worth 1 mark(s) in total:

  • custom_cake
  • letters_between
  • street_fighter

Worth 0.5 mark(s) in total:

  • xtreme
  • abc_grid

For your interest, but not for marks:

  • spiral
  • decimal_spiral

Problem sets are capped at 15 marks (there are 4 possible bonus marks from the three-dot exercises that can bring you up to a total of 15 if you missed out on any other marks in the one- or two-dot exercises).

Completing just the one and two-dot exercises every week can give you the full 15 marks needed in this component.

For more details, see the course outline.

Preparation

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

When attempting the following exercises, make sure to read the whole exercise, including any hints and assumptions that may make the exercise easier.

Exercise
(●◌◌)
:

Count Up/Down

Write a C program count_up_down.c that reads one integer n and prints all integers from 0 to n inclusive one per line.

Note that if n is positive, we are counting up and if n is negative, we are counting down.

Examples

dcc count_up_down.c -o count_up_down
./count_up_down
Enter number: 5
0
1
2
3
4
5
./count_up_down
Enter number: -3
0
-1
-2
-3

Assumptions/Restrictions/Clarifications

  • You may assume 0 will never be given as input.
  • You are not permitted to use arrays.
  • You do not have to do any error checking.
  • You do not have to check the return value from scanf.
You can run an automated code style checker using the following command:
1511 style count_up_down.c

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

1511 autotest count_up_down

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

give cs1511 lab03_count_up_down count_up_down.c

You must run give before Monday 30 September 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.

Exercise
(●◌◌)
:

Vector Sum

Download vector_sum.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity vector_sum

Complete the C program vector_sum.c which reads in two vectors with 3 integer components each and stores the sum of these components in a new vector to be printed.

Your job for this exercise is to complete the middle section (bolded above). There is a given struct vector called sum_vector, which needs to store the sum of the two scanned-in vectors, everything else has already been completed.

Complete the C program vector_sum.c which takes in two 3D vectors, each with an x, y and z integer component as input, and then calculates the sum to be printed.

Your job for this exercise is to use the values stored in first_vector and second_vector which have already been scanned in for you to calculate the sum of the vectors and store the result in the given struct vector called sum_vector.

Examples

dcc vector_sum.c -o vector_sum
./vector_sum
Please enter the values of the first vector (x, y, z): 1 2 3
Please enter the values of the second vector (x, y, z): 4 5 6
The resulting sum vector is:
x: 5
y: 7
z: 9
./vector_sum
Please enter the values of the first vector (x, y, z): 1 2 3
Please enter the values of the second vector (x, y, z): 1 2 3
The resulting sum vector is:
x: 2
y: 4
z: 6
./vector_sum
Please enter the values of the first vector (x, y, z): -5 30 -12
Please enter the values of the second vector (x, y, z): 15 -31 -30
The resulting sum vector is:
x: 10
y: -1
z: -42

Assumptions/Restrictions/Clarifications

  • You may assume that you will be given only integers as input.
  • You may assume that all input is valid.
  • You may notice that the starter code does not pass 1511 style. Defining a vector with x, y and z is perfectly fine in this case, even though they are 1 letter variables. Context is important here! Since x, y and z are common for dimensions in math, it is okay here. However, a vector with h, v, q would not be good style.
You can run an automated code style checker using the following command:
1511 style vector_sum.c

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

1511 autotest vector_sum

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

give cs1511 lab03_vector_sum vector_sum.c

You must run give before Monday 30 September 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.

Exercise
(●◌◌)
:

Debugging - Factorial

Download debug_factorial.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity debug_factorial

Debugging Tips!

Some debugging tips for you:

  • dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
  • print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like "the value of x is %d and y is %d". This lets you check that you got against what you expected.
  • COMP1511 debugging guide

The Task

This exercise takes in a positive integer as input, calculates the factorial of that number and prints it out. Currently it has some issues - it is your job to figure them out and fix the code.

Examples

dcc debug_factorial.c -o debug_factorial
./debug_factorial
Enter a number: 3
The factorial of 3 is 6
./debug_factorial
Enter a number: 7
The factorial of 7 is 5040
./debug_factorial
Enter a number: 1
The factorial of 1 is 1

Assumptions/Restrictions/Clarifications

  • You can assume you will not be given negative numbers
You can run an automated code style checker using the following command:
1511 style debug_factorial.c

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

1511 autotest debug_factorial

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

give cs1511 lab03_debug_factorial debug_factorial.c

You must run give before Monday 30 September 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.

Exercise
(●◌◌)
:

Print a Coordinate Grid

Write a program called print_grid.c that reads an integer n from standard input. and prints an n x n grid with all the coordinates of the grid shown.

Each of the points of the grid should be printed out in the form (row, col). This is different to your typical (x,y) coordinate grid and you should see if you can figure out why this is more convenient for us!

Examples

dcc print_grid.c -o print_grid
./print_grid 
Enter size: 4
(0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3)
./print_grid 
Enter size: 5
(0, 0) (0, 1) (0, 2) (0, 3) (0, 4)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 4)
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4)
./print_grid
Enter size: 8
(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7)
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7)
(5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7)
(6, 0) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6) (6, 7)
(7, 0) (7, 1) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

Assumptions/Restrictions/Clarifications

  • You may assume that your program will only scan in integers
  • Your program only has to handle sizes in the range 0 to 10 inclusive
  • Your program is not required to do any error checking
  • Your program is not permitted to use arrays
You can run an automated code style checker using the following command:
1511 style print_grid.c

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

1511 autotest print_grid

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

give cs1511 lab03_print_grid print_grid.c

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

Exercise
(●●◌)
:

Custom Cake

In this activity you will create a program called custom_cake.c.

The program will first ask for the number of layers (rows) of a cake. It will then scan in an ASCII character for each layer. Finally, the program will print out a row for each layer (ASCII character), equal in width to the number of layers.

For example, if the number of layers was 3, it should print out a cake that is 3x3 characters large. If the number of layers was 5, it should print out a cake that is 5x5 characters large.

Examples

dcc custom_cake.c -o custom_cake
./custom_cake
How many layers: 5
Please enter layers: *|=~=
*****
|||||
=====
~~~~~
=====
./custom_cake
How many layers: 3
Please enter layers: =&=
===
&&&
===

Assumptions/Restrictions/Clarifications

  • The number of layers will always be a non-zero positive integer.
  • The number of characters input will always match the number of layers.
  • The width of each layer will always be the same as the total number of layers.
  • You may assume that a whitesapce ' ', will never be the character for a layer.
You can run an automated code style checker using the following command:
1511 style custom_cake.c

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

1511 autotest custom_cake

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

give cs1511 lab03_custom_cake custom_cake.c

You must run give before Monday 30 September 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.

Exercise
(●●◌)
:

Letters Between

Design a program named letters_between.c that accepts a starting letter and target letter as input. The program should calculate the shortest path between the two given letters by either moving forwards or backwards through the alphabet. The program then prints out all the letters from the starting letter to the target letter along the shortest path.

For example, if 'c' and 'k' were entered as the starting and target letters respectivley, the program would calculate the path moving forwards through the alphabet to be cdefghijk, which takes 9 letters and the
the path moving backwards through the alphabet to be cbazyxwvutsrqponmlk, which takes 19 letters.

Hence the program will print cdefghijk as 9 letters is less than 19.

Note In the case where the number of letters are equal, the program will print the letters moving forwards through the alphabet.

Examples

dcc letters_between.c -o letters_between
./letters_between
Please enter starting letter: H
Please enter target letter: K
HIJK
./letters_between
Please enter starting letter: b
Please enter target letter: w
bazyxw
./letters_between
Please enter starting letter: Q
Please enter target letter: D
QRSTUVWXYZABCD
./letters_between
Please enter starting letter: m
Please enter target letter: m
m

Assumptions/Restrictions/Clarifications

  • The starting and target letters will either be both uppercase or both lowercase.
  • The result should be printed in the same case as the input.
  • Letters will be from the english alphabet (a,b,c ... x,y,z).
  • If the starting letter and target letter are the same the program should only print one character.
  • If the number of letters are the same moving forwards and backwards, choose forwards.
You can run an automated code style checker using the following command:
1511 style letters_between.c

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

1511 autotest letters_between

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

give cs1511 lab03_letters_between letters_between.c

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

Exercise
(●●◌)
:

Street Fighter

Download street_fighter.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity street_fighter
Write a C program street_fighter.c which allows the user to play a game of Street Fighter. The user should be able to customise the attack power and attack key-binding (the assignment of a key on a keyboard to a command) for each fighter.

Each round, the program should prompt each fighter to attack.

When a fighter attacks, their opponent's health should immediately be reduced by the attacking fighter's attack power. A fighter that is below 50% of their maximum health deals 50% more damage. If during a round a fighter is brought below 50% of their maximum health, they should immediately start dealing 50% more damage to their opponent from that point onwards.

If fighter 2 has no health remaining after fighter 1 attacks, fighter 2 still has the opportunity to attack before the fight is over.

At the end of each round, the program should print out the current health of each fighter. If either fighter has no health remaining at the end of a round, the fight is over and the program should print which fighter won.

If both fighters have no health remaining at the end of the fight your program should print It's a draw! instead.

Finally the program should then print GAME OVER and exit.

You are provided with struct fighter which contains the following fields:

  1. int attack: The attack power of the fighter. A fighter's attack power will come from the user and will be between 1 and 10.
  2. double health: The current health of the fighter. Fighter's begin with a maximum health of 50.
  3. char attack_command: The key binding for the attack command.

Examples

dcc street_fighter.c -o street_fighter
./street_fighter
Welcome to Street Fighter!

Enter Fighter 1's attack power (1-10): 7
Enter an ascii character for Fighter 1's attack command: a

Enter Fighter 2's attack power (1-10): 10
Enter an ascii character for Fighter 2's attack command: b

FIGHT!

Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press b to attack: b
Fighter 2 attacks!

Fighter 1's health: 40.0
Fighter 2's health: 43.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press b to attack: b
Fighter 2 attacks!

Fighter 1's health: 30.0
Fighter 2's health: 36.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press b to attack: b
Fighter 2 attacks!

Fighter 1's health: 20.0
Fighter 2's health: 29.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press b to attack: b
Fighter 2 attacks!

Fighter 1's health: 5.0
Fighter 2's health: 18.5
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press b to attack: b
Fighter 2 attacks!

Fighter 1's health: -10.0
Fighter 2's health: 8.0

Fighter 2 wins!
GAME OVER
./street_fighter
Welcome to Street Fighter!

Enter Fighter 1's attack power (1-10): 10
Enter an ascii character for Fighter 1's attack command: a

Enter Fighter 2's attack power (1-10): 9
Enter an ascii character for Fighter 2's attack command: s

FIGHT!

Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press s to attack: s
Fighter 2 attacks!

Fighter 1's health: 41.0
Fighter 2's health: 40.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press s to attack: s
Fighter 2 attacks!

Fighter 1's health: 32.0
Fighter 2's health: 30.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press s to attack: s
Fighter 2 attacks!

Fighter 1's health: 18.5
Fighter 2's health: 20.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press s to attack: s
Fighter 2 attacks!

Fighter 1's health: 5.0
Fighter 2's health: 5.0
Fighter 1, press a to attack: a
Fighter 1 attacks!
Fighter 2, press s to attack: s
Fighter 2 attacks!

Fighter 1's health: -8.5
Fighter 2's health: -10.0

It's a draw!
GAME OVER

Assumptions/Restrictions/Clarifications

  • You may find %0.1lf useful for formatting the health of each fighter.
  • Assume you will only be given valid inputs for the attack power and attack command.
You can run an automated code style checker using the following command:
1511 style street_fighter.c

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

1511 autotest street_fighter

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

give cs1511 lab03_street_fighter street_fighter.c

You must run give before Monday 30 September 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.

Exercise
(●●●)
:

Draw a fancy pattern

Write a program called xtreme.c that reads an (odd) integer n from standard input, and prints an n x n a square of asterisks and dashes in the following pattern:

Enter size: 9
*---*---*
-*-----*-
--*---*--
---*-*---
*---*---*
---*-*---
--*---*--
-*-----*-
*---*---*
Enter size: 15
*---*-----*---*
-*---*---*---*-
--*---*-*---*--
---*---*---*---
*---*-----*---*
-*---*---*---*-
--*---*-*---*--
---*---*---*---
--*---*-*---*--
-*---*---*---*-
*---*-----*---*
---*---*---*---
--*---*-*---*--
-*---*---*---*-
*---*-----*---*
Enter size: 25
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*

For more examples of this pattern, see the example program output at the end of this question

This exercise is designed to give you practice with while loops, if statements and some mathematical operators.

As the course has not taught arrays yet, you are not permitted to use an array in this exercise.

Examples

dcc xtreme.c -o xtreme
./xtreme
Enter size: 5
*---*
-*-*-
--*--
-*-*-
*---*
./xtreme
Enter size: 7
*-----*
-*---*-
--*-*--
---*---
--*-*--
-*---*-
*-----*
./xtreme
Enter size: 11
*---*-*---*
-*---*---*-
--*-----*--
---*---*---
*---*-*---*
-*---*---*-
*---*-*---*
---*---*---
--*-----*--
-*---*---*-
*---*-*---*
./xtreme
Enter size: 13
*---*---*---*
-*---*-*---*-
--*---*---*--
---*-----*---
*---*---*---*
-*---*-*---*-
--*---*---*--
-*---*-*---*-
*---*---*---*
---*-----*---
--*---*---*--
-*---*-*---*-
*---*---*---*
./xtreme
Enter size: 17
*---*---*---*---*
-*---*-----*---*-
--*---*---*---*--
---*---*-*---*---
*---*---*---*---*
-*---*-----*---*-
--*---*---*---*--
---*---*-*---*---
*---*---*---*---*
---*---*-*---*---
--*---*---*---*--
-*---*-----*---*-
*---*---*---*---*
---*---*-*---*---
--*---*---*---*--
-*---*-----*---*-
*---*---*---*---*

Assumptions/Restrictions/Hints

  • You can assume n is odd and n >= 5.
  • You may assume that you will be given only integers as input
  • You may assume that all input is valid
You can run an automated code style checker using the following command:
1511 style xtreme.c

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

1511 autotest xtreme

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

give cs1511 lab03_xtreme xtreme.c

You must run give before Monday 30 September 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.

Exercise
(●●●)
:

ABC Grid

For this activity you will create the program abc_grid.c.

The program will create a cascading pattern in the form of a grid of letters. First the program will ask the user to choose a grid size by entering the number of rows and then the number of columns. The program will then ask for a starting letter and the starting coordinates for this letter. This is where the pattern will start from.

In the example below the starting letter is Y. Lets say it's starting position was [x][y] where x and y are some starting co-ordinates. The letter following Y in the alphabet is Z and hence the immediate surrounding positions ([x + 1][y], [x - 1][y], [x][y + 1], [x][y - 1]) are Z. This pattern now continues from Z where the next letter is A and so on to B. This pattern will continue until the grid has been filled!

   B
  BAB
 BAZAB
BAZYZAB
 BAZAB
  BAB
   B

Examples

dcc abc_grid.c -o abc_grid
./abc_grid
How many rows: 3
How many columns: 2
Please choose starting letter: a
Please enter starting coordinates: 2 1
dc
cb
ba
./abc_grid
How many rows: 6
How many columns: 7
Please choose starting letter: W
Please enter starting coordinates: 2 3
BAZYZAB
AZYXYZA
ZYXWXYZ
AZYXYZA
BAZYZAB
CBAZABC

Assumptions/Restrictions/Clarifications

  • Starting indexing for rows and columns starts at 0 (i.e. first row = 0).
  • The ouput should be in the same case as the starting letter.
  • You will always be given a letter from the alphabet.
  • When reaching the letter 'z' it wraps around to 'a'.
  • Letter starting coordinates will always be within the given row and column range.
  • The number of rows and columns will always be integers greater than 0.
You can run an automated code style checker using the following command:
1511 style abc_grid.c

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

1511 autotest abc_grid

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

give cs1511 lab03_abc_grid abc_grid.c

You must run give before Monday 30 September 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.

Exercise
(☠)
:

Spiral

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

Examples

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

Assumptions/Restrictions/Clarifications

  • You can assume n is odd and n >= 5.
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

Exercise
(☠)
:

Decimal Spiral (extra-hard)

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

Examples

dcc decimal_spiral.c -o decimal_spiral
./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

Assumptions/Restrictions/Clarifications

  • You can assume n is odd and n >= 5.
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

Exercise — individual:
(Not For Marks) Debugging - Square

Download debug_square.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity debug_square

Note that this exercise is not marked or worth marks!

Debugging Tips!

Some debugging tips for you:

  • dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
  • print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like "the value of x is %d and y is %d". This lets you check that you got against what you expected.
  • COMP1511 debugging guide

The Task

This exercise takes in a positive integer 'n' as input, and prints the outline of a square of size 'n' by 'n' in asterisks ('x'). Currently it has some issues - it is your job to figure them out and fix the code.

Examples

dcc debug_square.c -o debug_square
./debug_square
Enter the size of the pattern: 2
**
**
./debug_square
Enter the size of the pattern: 5
*****
*   *
*   *
*   *
*****
./debug_square
Enter the size of the pattern: 1
*

Walkthrough

Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video

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

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

1511 autotest debug_square

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 4 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code 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, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

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