Programming Fundamentals

Objectives

  • using arrays
  • using struct arrays
  • creating functions
  • using while loops for repetition

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:

  • devowel
  • print_pi_style
  • scanning_into_array_style
  • race_results_style

Worth 1 mark(s) in total:

  • souffle_pancake
  • cs_calculator

Worth 0.5 mark(s) in total:

  • class_details

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
(●◌◌)
:

Devowelling Text

Write a C program devowel.c which reads characters from its input and writes the same characters to its output, except it does not write lower case vowels ('a', 'e', 'i', 'o', 'u').

Your program should stop only at the end of input.

Your code should match the following example exactly:
./devowel
Are you saying 'Boo' or 'Boo-Urns'?
Ar y syng 'B' r 'B-Urns'?
In this house, we obey the laws of thermodynamics!
In ths hs, w by th lws f thrmdynmcs!

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

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

1511 autotest devowel

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

give cs1511 lab04_devowel devowel.c

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

Exercise
(●◌◌)
:

Print out pi to a certain number of digits

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1511 style).

If 1511 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

1511 style print_pi_style.c

Write a C program, print_pi_style.c, which prints out the first n digits of pi, where n is specified by the user.

The main function has already been written for you. There's an empty function that has been provided for you called print_pi_style that takes in one integer as an argument.

Place your code (only the printing code) inside this function.

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

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

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

The output from your program should look exactly like this:

dcc print_pi_style.c -o print_pi_style
./print_pi_style
How many digits of pi would you like to print? 3
3.14
./print_pi_style
How many digits of pi would you like to print? 5
3.1415
./print_pi_style
How many digits of pi would you like to print? 10
3.141592653

You can assume that n >= 2 AND n <= 10, however, do not use this as a reason to avoid while loops.

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

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

1511 autotest print_pi_style

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

give cs1511 lab04_print_pi_style print_pi_style.c

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

Exercise
(●◌◌)
:

Scanning numbers into an array

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1511 style).

If 1511 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

1511 style scanning_into_array_style.c

Write a C program scanning_into_array_style.c which scans in a certain amount of numbers from the user and fills an array with them.

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

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

First, it scans in the quantity of numbers from the user.

Then, it scans in that many numbers from the user.

Once the array is filled with the numbers the user input, call a pre-written function that prints the minimum and maximum values in that array

Note: you do NOT need to write the minmax function, it is in the provided code, you are just required to call it properly after filling in the array yourself. This output verifies you scanned in the array correctly.

./scanning_into_array_style
How many numbers: 5
Please enter numbers: 1
2
3
4
5
Minimum: 1
Maximum: 5
./scanning_into_array_style
How many numbers: 4
Please enter numbers: 3
-2
7
1
Minimum: -2
Maximum: 7

Assumptions/Restrictions/Hints

  • The given inputs will always be integers.
  • The given list of numbers will never be more than 100 numbers.
You can run an automated code style checker using the following command:
1511 style scanning_into_array_style.c

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

1511 autotest scanning_into_array_style

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

give cs1511 lab04_scanning_into_array_style scanning_into_array_style.c

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

Exercise
(●◌◌)
:

Using struct arrays to record the results of a Formula One race

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1511 style).

If 1511 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

1511 style race_results_style.c

Complete the C program race_results_style.c, so that it scans the results of each car in a Formula One race into an array of structs. It should then prints out the results of all cars in the race.

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

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

First, it scans in the number of cars that participated in the race. Then, it scans in the details of each car: the car number and its race time. Afterwards, it prints out the list of cars and their details.

Your program should match the following output exactly.

./race_results_style
How many cars in the race? 3
Enter results:
1 90.5 
11 95.4
63 100.5
Results:
 1: 90.50
11: 95.40
63: 100.50
./race_results_style
How many cars in the race? 5
Enter results:
13 101.65
15 110.00
73 115.54
3 115.57
42 115.59
Results:
13: 101.65
15: 110.00
73: 115.54
 3: 115.57
42: 115.59

Assumptions/Restrictions/Hints

  • When asked for the number of cars, you will be given a non-zero positive integer.
  • All inputs will be the correct type and non-zero positive numbers.
  • The number of cars in a race will never be greater than 20.
  • You will never scan in the same car number twice.
  • The cars will always be given in ascending order of race times.
You can run an automated code style checker using the following command:
1511 style race_results_style.c

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

1511 autotest race_results_style

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

give cs1511 lab04_race_results_style race_results_style.c

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

Exercise
(●●◌)
:

Souffle Pancakes

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

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

Complete the C program, souffle_pancake.c.

The main function has already been written, which help the user calculate how many resources they will need to cook any given amount of souffle pancakes!

Unfortunately due to a strange bug in the DCC Compiler, the math.h library has been acting up, so Paula has asked you to help her out and write some useful functions similar to those in that library.

Your job is to write 3 functions that are currently being used in the main function. They are: divide, ceiling and a helper procedure print_time.

divide is a function that divides two doubles.

  • function input:
    • a double for the top of the divide
    • a double for the bottom of the divide
  • function output: double for the result
ceiling is a function that rounds up a given double to the closest integer. If the value is already a whole number, it stays the same value.
  • function input: a double for the number being ceiling-ed
  • function output: int for the result
print_time is a procedure that given minutes, will print out the time in hours and minutes
  • function input: an int for the cooking time in minutes
  • function output: void
  • It will print the time in the format "X hours and X minutes", even if either the minutes or hours is 0.

The output from your program should look exactly like this:

dcc souffle_pancake.c -o souffle_pancake
./souffle_pancake
How many souffle pancakes do you want to make? 3
You will need:
- 2 eggs
- 30g sugar
- 24g flour
Frying will take you 0 hours and 15 minutes.
./souffle_pancake
How many souffle pancakes do you want to make? 5
You will need:
- 4 eggs
- 50g sugar
- 40g flour
Frying will take you 0 hours and 30 minutes.
You can run an automated code style checker using the following command:
1511 style souffle_pancake.c

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

1511 autotest souffle_pancake

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

give cs1511 lab04_souffle_pancake souffle_pancake.c

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

Exercise
(●●◌)
:

Create a simple calculator, reading different numbers of integers

For this exercise, make a program called cs_calculator.c which will scan in instructions until End-Of-Input (which we enter to our terminal with the keyboard shortcut ctrd) and prints the output as specified below. An instruction is a character, followed by one or two positive integers. The first character identifies what type the instruction is.

  • If the first character in the instruction is 's', then your program should print out the square of the next number in the instruction.
  • If the first character in the instruction is 'p', then your program should print out the value of the next number raised to the power of the number after next.

You can assume that the first character in the instruction is only either 's' or 'p'

You can assume that for each instruction, the correct number of successive positive integers will be given.

./cs_calculator
Enter instruction: s 2
4
Enter instruction: p 5 3
125
Enter instruction: s 4
16
Enter instruction: p 3 4
81
Enter instruction: 
./cs_calculator
Enter instruction: p 3 3
27
Enter instruction: s 10
100
Enter instruction: 

Note: The autotest for this exercise expects your program to end WITHOUT a new line character when the user presses Ctrl+D. This means that the command prompt for the next command should be on the same line as the end of your program.

One major challenge of this exercise is figuring out how to use scanf effectively. The lessons you learn in this exercise regarding scanf will be useful in the assignment 1.

Hint: You can scan in the first character initially, and based on the value of that first character, scan in the correct amount of additional numbers.

Hint: scanf will jump over and ignore any whitespace characters including newline characters. Be careful not to overrun or underrun your input.

There is no need to print a newline character after the user presses Ctrl+D

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

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

1511 autotest cs_calculator

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

give cs1511 lab04_cs_calculator cs_calculator.c

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

Exercise
(●●●)
:

Harder Refactoring Code

For this activity, you've been provided with a fully working program: class_details.c.

While this code produces the correct output, it's code style is not very good.

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

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

In particular, class_detail.c is full of magic numbers and repeating code, and it's main function is more than 200 lines long! Try running 1511 style class_details.c to see even more of it's style issues.

For this activity Your job is to go through and improve the readability of the code by

  1. Replacing magic numbers with enums and #defines,
  2. Breaking it up into small functions (less than 40 lines),
  3. Using those functions to remove unnecessary repetition,
  4. Writing some function comments to document the code.
You shouldn't change any of the actual functionality of the program i.e. none of the changes you make should change any of the program's output.

Program description:

Below is a description of the program. Keep in mind that this has all already been implemented for you.

The provided program scans in and stores the details of students in a class. It first scans in the size of the class, and then scans in the details of each of the students including:

  • zID
  • Degree type (Undergraduate/Postgraduate)
  • Major (if an Undergraduate)
  • Assignments mark (Collated mark of assignments and labs)
  • Exam mark
  • Course grade

Once all student details have been scanned in, The program then loops and scans in commands. It doesn't stop looping until the QUIT command is entered. The program performs one of the following tasks:

  • Command 0 Help: Display program instructions
  • Command 1 (Display Student): Print the details of a specific student
  • Command 2 (Display Class): Print the details of all students in a class
  • Command 3 (Quit): Exit the program
./class_details
Enter Class Size: 1
Student 1: 
Enter zID: 5111111
Select Degree Type: 
0: Undergraduate
1: Postgraduate
0
Select Major: 
0: Computer Science
1: Database Systems
2: eCommerce Systems
3: Artificial Intelligence
4: Programming Languages
5: Computer Networks
6: Embedded Systems
7: Security Engineering
8: None
2
Enter Assignments mark (out of 60): 35.5
Enter exam mark (out of 40): 30
Enter Command Number (0 for Help): 0
Enter a number corresponding to one of the following commands: 
0 (Help): Display program instructions
1 (Display Student): Print the details of a specific student
2 (Display Class): Print the details of all students in a class
3 (Quit): Exit the program
Enter Command Number (0 for Help): 1
Enter Student zID: 5222222 
No student with that zID exists
Enter Command Number (0 for Help): 1
Enter Student zID: 5111111 
z5111111: {
        Degree Type: Undergraduate
        Major: eCommerce Systems
        Assignments Mark: 35.50/60
        Exam Mark: 30.00/40
        Course Grade: 65.50/100
}
Enter Command Number (0 for Help): 100
Invalid Command
Enter Command Number (0 for Help): 3
Exiting Program
./class_details
Enter Class Size: 2
Student 1: 
Enter zID: 5111111
Select Degree Type: 
0: Undergraduate
1: Postgraduate
0
Select Major: 
0: Computer Science
1: Database Systems
2: eCommerce Systems
3: Artificial Intelligence
4: Programming Languages
5: Computer Networks
6: Embedded Systems
7: Security Engineering
8: None
6
Enter Assignments mark (out of 60): 35.5
Enter exam mark (out of 40): 30
Student 2: 
Enter zID: 5222222
Select Degree Type: 
0: Undergraduate
1: Postgraduate
1
Enter Assignments mark (out of 60): 40.5
Enter exam mark (out of 40): 25.9
Enter Command Number (0 for Help): 2
Students: 
z5111111: {
        Degree Type: Undergraduate
        Major: Embedded Systems
        Assignments Mark: 35.50/60
        Exam Mark: 30.00/40
        Course Grade: 65.50/100
}
z5222222: {
        Degree Type: Postgraduate
        Assignments Mark: 40.50/60
        Exam Mark: 25.90/40
        Course Grade: 66.40/100
}
Enter Command Number (0 for Help): 1
Enter Student zID: 5222222
z5222222: {
        Degree Type: Postgraduate
        Assignments Mark: 40.50/60
        Exam Mark: 25.90/40
        Course Grade: 66.40/100
}
Enter Command Number (0 for Help): 3
Exiting Program

Assumptions/Restrictions/Hints

  • You should not change the functionality of the program in any way.
  • If there are any edge cases not handled by the original code (e.g. students sharing zids), you do not need to modify the code to handle them.
  • All inputs will positive, and be the correct type
You can run an automated code style checker using the following command:
1511 style class_details.c

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

1511 autotest class_details

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

give cs1511 lab04_class_details class_details.c

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

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

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