Week 06 Extra Exercises

Information

  • This page contains extra exercises for week 06.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●◌◌)
:

Count Bigger

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/count_bigger/count_bigger.c .

count_bigger should return a single integer: the number of values in the array which are larger than 99 or smaller than -99.

For example if the array contains these 8 elements:

141, 5, 92, 6, 535, -89, -752, -3

Your function should return 3, because these 3 elements are bigger than 99 or smaller than -99:

141, 535, -752

Assumptions/Restrictions/Clarifications

  • count_bigger should return a single integer
  • count_bigger should not change the array it is given
  • count_bigger should not call scanf (or getchar or fgets)
  • You can assume the array contains at least one integer
  • count_bigger should not print anything. It should not call printf
  • Your submitted file may contain a main function. It will not be tested or marked

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

1091 autotest count_bigger

Exercise
(●◌◌)
:

Identity Matrix

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/identity_matrix/identity_matrix.c .

Your task is to add code to this function in identity_matrix.c:

//Makes a square matrix into an identity matrix
void make_identity(int size, int matrix[size][size]) {
    //TODO: Complete this function
}

The above file identity_matrix.c contains a function make_identity(), which should change the matrix passed in to be the identity matrix of coresponding size.

The identity matrix is a square, 2D array with all the cells containing 0, excluding those on the diagonal from top left to bottom right, which contain a value of 1.

For example, the identity matrix of:

    0 1 2
    3 4 5
    6 7 8
is:
    1 0 0 
    0 1 0
    0 0 1

The provided function doesn't actually work. Your task is to complete this function.

You can assume the 2D array will always be square and only contain integers from 0 to 9.

The file also contains a main function and a print function which you can use to help test your make_identity() function. It has one simple test case.

This main function and the print function will not be marked -- you must write all of your code in the make_identity() function. You may modify the main function if you wish (e.g. to add further tests), but only the make_identity() function will be marked.

Once your program is working, the output from the provided test in the main function should be:

dcc -o identity_matrix identity_matrix.c 
./identity_matrix 
1 0 0
0 1 0
0 0 1

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

1091 autotest identity_matrix

Exercise
(●●●)
:

Harder Refactoring Code

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/class_details/class_details.c .

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.

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

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

1091 autotest class_details