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've written in a previous week.

This is also an opportunity for you to ask any questions you might have about the course content so far!

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

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 COMP1511 Help Session Microsoft Teams.

For face-to-face help sessions, the lab map can be found Here.

Activities To Be Completed

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

Worth 1 mark(s) in total:

  • negative
  • count_up_down
  • vector_sum
  • print_emoticon
  • zodiac_animal

Worth 1 mark(s) in total:

  • dice_range
  • print_grid

Worth 0.5 mark(s) in total:

  • xtreme

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.

Videos

The following short videos may be of some help to get started on the lab exercises.

If you have watched the videos and are still unsure how to start the exercises, ask your tutor for help :)

Exercise
(●◌◌)
:

Don't Be So Negative!

Download negative.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/negative/negative.c .

Complete the program negative.c so that it uses scanf to get a number from a user and prints "Don't be so negative!" if they entered a negative number.
If the number is positive, the program should print "You have entered a positive number."
If the user enters the number 0, the program should print "You have entered zero."

The Provided starter code contains definitions for procedures which print out the relevant messages. You should call these in your code.

Note: you can assume that the number will always be a whole number (i.e. an integer)
Your program should behave as follows:
dcc -o negative negative.c
./negative
3
You have entered a positive number.
./negative
-3
Don't be so negative!
./negative
0
You have entered zero.
You can run an automated code style checker using the following command:
1511 style negative.c

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

1511 autotest negative

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

give cs1511 lab03_negative negative.c

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

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. For example:

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 and your lab partner must both submit your work by running give:

give cs1511 lab03_count_up_down count_up_down.c

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

Exercise
(●◌◌)
:

Vector Sum

Download vector_sum.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/vector_sum/vector_sum.c .

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.

When the program is completed, it will look like this when run.

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

  • 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 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 and your lab partner must both submit your work by running give:

give cs1511 lab03_vector_sum vector_sum.c

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

Exercise
(●◌◌)
:

Print Emoticon

Download print_emoticon.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/print_emoticon/print_emoticon.c .

Complete the C program, print_emoticon.c.

The main function has already been written for you.

Your job is to complete the procedure called print_emoticon which prints out an ascii emoticon depending on what value is passed into the procedure.

Your code should print ^_^ for happy, T_T for sad, >_< for embarassed and *o* for shocked. Make sure to have a look at main to see what output we have already written print statements for!

You can assume that only one of the 4 valid enum values will be passed into your procedure.

You can NOT put your code in main for this exercise, main must remain unmodified.

Your procedure must be named print_emoticon.

The output from your program should look exactly like this:

dcc print_emoticon.c -o print_emoticon
./print_emoticon
How are you feeling today?
1: Happy
2: Sad
3: Embarassed
4: Shocked

Please enter the number for your emoticon: 1
^_^
You can run an automated code style checker using the following command:
1511 style print_emoticon.c

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

1511 autotest print_emoticon

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_emoticon print_emoticon.c

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

Exercise
(●◌◌)
:

What Zodiac Animal are you?

Download zodiac_animal.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/zodiac_animal/zodiac_animal.c .

Complete the C program zodiac_animal.c, which should first ask the user to enter their birth year. It should then print out what (approximate) Chinese zodiac animal that year corresponds to.

(For simplicity, we'll assume that the each calendar year corresponds exactly to a zodiac animal).

You can assume the year entered will be no earlier than 1900, (we will not test any years before 1900).

The starter code contains an enum zodiac_animal, which defines the different Chinese zodiac animals.

In your program, you should aim to:

  1. Scan in the year (as an integer)
  2. Calculate which zodiac animal that year corresponds to, and convert it into an enum zodiac_animal variable
  3. Use that enum to decide which message to print out.

The output from your program should look exactly like this:

dcc zodiac_animal.c -o zodiac_animal
./zodiac_animal
Enter the year of your birth: 1999
You were born in the year of the Rabbit!
./zodiac_animal
Enter the year of your birth: 2000
You were born in the year of the Dragon!
./zodiac_animal
Enter the year of your birth: 2001
You were born in the year of the Snake!
./zodiac_animal
Enter the year of your birth: 2002
You were born in the year of the Horse!
./zodiac_animal
Enter the year of your birth: 2003
You were born in the year of the Goat!
./zodiac_animal
Enter the year of your birth: 2004
You were born in the year of the Monkey!
./zodiac_animal
Enter the year of your birth: 2005
You were born in the year of the Rooster!
./zodiac_animal
Enter the year of your birth: 1900
You were born in the year of the Rat!
./zodiac_animal
Enter the year of your birth: 1901
You were born in the year of the Ox!
./zodiac_animal
Enter the year of your birth: 2022
You were born in the year of the Tiger!
./zodiac_animal
Enter the year of your birth: 1970
You were born in the year of the Dog!
./zodiac_animal
Enter the year of your birth: 1971
You were born in the year of the Pig!

Assumptions/Restrictions/Clarifications

  • You may assume that your program will only scan in integers
  • Your program is not required handle any years before 1900 (You don't need to check this).
You can run an automated code style checker using the following command:
1511 style zodiac_animal.c

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

1511 autotest zodiac_animal

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

give cs1511 lab03_zodiac_animal zodiac_animal.c

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

Exercise
(●●◌)
:

Dice Range

We will often roll multiple dice at the same time.

Write a C program dice_range.c that reads the number of sides on a set of dice and how many of them are being rolled. It then outputs the range of possible totals that these dice can produce as well as the average value.

Note: You can assume that the user will always enter 2 integers.

Hint: use the examples below to clarify the expected behaviour of your program.

For example:

dcc -o dice_range dice_range.c
./dice_range
Enter the number of sides on your dice: 6
Enter the number of dice being rolled: 2
Your dice range is 2 to 12. 
The average value is 7.000000
./dice_range
Enter the number of sides on your dice: 8
Enter the number of dice being rolled: 3
Your dice range is 3 to 24.
The average value is 13.500000
./dice_range
Enter the number of sides on your dice: 20
Enter the number of dice being rolled: 4
Your dice range is 4 to 80. 
The average value is 42.000000
You'll also need to check for invalid dice or situations where the range is empty. Those situations should produce these results:
./dice_range
Enter the number of sides on your dice: -5
Enter the number of dice being rolled: 4
These dice will not produce a range.
./dice_range
Enter the number of sides on your dice: 6
Enter the number of dice being rolled: 0
These dice will not produce a range.
You can run an automated code style checker using the following command:
1511 style dice_range.c

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

1511 autotest dice_range

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

give cs1511 lab03_dice_range dice_range.c

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

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!

Make your program match the examples below exactly.

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 Tuesday 07 March 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Draw a fancy pattern

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

n: 9
*---*---*
-*-----*-
--*---*--
---*-*---
*---*---*
---*-*---
--*---*--
-*-----*-
*---*---*
n: 15
*---*-----*---*
-*---*---*---*-
--*---*-*---*--
---*---*---*---
*---*-----*---*
-*---*---*---*-
--*---*-*---*--
---*---*---*---
--*---*-*---*--
-*---*---*---*-
*---*-----*---*
---*---*---*---
--*---*-*---*--
-*---*---*---*-
*---*-----*---*
n: 25
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
-*---*---*-----*---*---*-
--*---*---*---*---*---*--
---*---*---*-*---*---*---
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*
---*---*---*-*---*---*---
--*---*---*---*---*---*--
-*---*---*-----*---*---*-
*---*---*---*---*---*---*

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

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.

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

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

Assumptions/Restrictions/Hints

  • 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 and your lab partner must both submit your work by running give:

give cs1511 lab03_xtreme xtreme.c

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

Exercise
(☠)
:

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
*****************
----------------*
***************-*
*-------------*-*
*-***********-*-*
*-*---------*-*-*
*-*-*******-*-*-*
*-*-*-----*-*-*-*
*-*-*-***-*-*-*-*
*-*-*-*---*-*-*-*
*-*-*-*****-*-*-*
*-*-*-------*-*-*
*-*-*********-*-*
*-*-----------*-*
*-*************-*
*---------------*
*****************
You can run an automated code style checker using the following command:
1511 style spiral.c

Exercise
(☠)
:

Decimal Spiral (extra-hard)

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
You can run an automated code style checker using the following command:
1511 style decimal_spiral.c

Formatif Feedback (Optional)

Every week, you have the opportunity to submit a piece of code to formatif, and receive feedback on your work! This is entirely optional, but will provide a valuable opportunity to develop your understanding of C, and get advice on problem solving/algorithmic choices.

To get feedback, go to formatif, and upload the code that you'd like a tutor to look at (Note: This cannot be code from assignments - it must be from labs, or code written for your own practice). Once the feedback is ready, you'll receive an email with instructions on how to collect the feedback!

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

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