COMP(1511|1911) 26T2 Lab Exam

Time allowed: 1 hours and 5 minutes (5 minutes reading time, 1 hours working time)

Total number of questions: 4

Total number of marks: 34

Questions are NOT worth the same amount of marks

The distribution of marks is as follows:

Attempt all questions

You should keep this paper confidential. Sharing it, DURING or AFTER the exam is prohibited.


Paper Information

Exam Condition Summary

Deliberate violation of these exam conditions will be referred to Student Integrity Unit as serious misconduct, which may result in penalties up to and including a mark of 0 in COMP(1511|1911) and exclusion from UNSW.

Exam Environment

Language Restriction

Fit to Sit

By sitting or submitting an assessment on the scheduled assessment date, a student is declaring that they are fit to do so and cannot later apply for Special Consideration.

If, during an exam you feel unwell to the point that you cannot continue with the exam, you should raise you hand and inform an invigilator, who will provide advice as to the best course of action.

Technical Issues

If you experience a technical issue, you should raise your hand and wait for an invigilator to assist.

Setup

The starter files for this exam will be automatically placed in your home directory. Programs (including your terminal and text editor) are available via the desktop menu (accessible by right-clicking the desktop).
A copy of the course website has been made available. You can access this via the desktop menu also (accessible by right-clicking the desktop).

To test your code (for example, for question 1), run the command:

autotest labexam_q1

To submit your code (for example, for question 1), run the command:

submit labexam_q1 labexam_q1.c

Questions

Question 1
(●◌◌◌)
:

This question is of similar style to the first array hurdle question in the final exam.
(12 marks)

The function count_alphabetical_pairs takes in a one-dimensional array of characters, array, with exactly size number of elements.

Your count_alphabetical_pairs function should count the number of consecutive elements in the array that are in alphabetical order. Character comparisons should be case-sensitive, meaning 'a' and 'b' form an alphabetical pair while 'a' and 'B' do not.

If the array does not have sufficient size to test any elements (i.e. has a size less than 2), your count_alphabetical_pairs function should return -1.

Examples

For example, given the array:

{'z', 'a', 'b', 'e', 'c', 'f', 'g', 'h'}

The function should return 3, since:

  • 'z' is followed by 'a'. Given the alphabetical pairs do not wrap around, this does not form an alphabetical pair.
  • 'a' is followed by 'b', which is the next letter in the alphabet, and therefore forms an alphabetical pair.
  • 'b' is followed by 'e', which is not the next letter in the alphabet, and therefore does not form an alphabetical pair.
  • 'e' is followed by 'c', which is not the next letter in the alphabet, and therefore does not form an alphabetical pair.
  • 'c' is followed by 'f', which is not the next letter in the alphabet, and therefore does not form an alphabetical pair.
  • 'f' is followed by 'g', which is the next letter in the alphabet, and therefore forms an alphabetical pair.
  • 'g' is followed by 'h', which is the next letter in the alphabet, and therefore forms an alphabetical pair.
  • 'h' is the last element in the array and therefore has no character after it and does not form an alphabetical pair.

Therefore, the array contains a total of 3 alphabetical pairs, from the pairs {'a', 'b'}, {'f', 'g'} and {'g', 'h'}.

For example, given the array:

{'l', 'M', 'N', 'o'}

The function should return 1, since:

  • 'l' is followed by 'M'. Since the character comparisons are case-sensitive, this does not form an alphabetical pair.
  • 'M' is followed by 'N', which is the next letter in the alphabet, and therefore does form an alphabetical pair.
  • 'N' is followed by 'o'. Since the character comparisons are case-sensitive, this does not form an alphabetical pair.
  • 'o' is the last element in the array and therefore has no character after it and does not form an alphabetical pair.

Therefore, the array contains a total of 1 alphabetical pair, from the pair {'M', 'N'}.

For example, given the array:

{'m'}

The function should return -1, since the array has a size less than 2 and therefore cannot have a pair present.

Testing

labexam_q1.c also contains a simple main function which allows you to test your count_alphabetical_pairs function. This function will not be marked.

Your count_alphabetical_pairs function will be called directly in marking.

Assumptions/Restrictions/Clarifications

  • The parameter size will have a value between 1 and 100, inclusive.
  • Your count_alphabetical_pairs function should return a single integer.
  • Your count_alphabetical_pairs function should not modify the array.
  • Your count_alphabetical_pairs function should not print anything (e.g., no printf).
  • Your count_alphabetical_pairs function should not call scanf or perform any input.
  • Your count_alphabetical_pairs function should return -1 if the array has a size less than 2.
  • Character comparisons should be case-sensitive, meaning 'a' and 'b' form an alphabetical pair while 'a' and 'B' do not.
  • Consecutive means continuously in a row or in other words, next to one another.
  • No wraparound should be applied when comparing consecutive characters, (i.e. 'z' followed by 'a' is not considered an alphabetical pair).
  • You may assume array will only contain alphabetical characters, either lowercase ('a'-'z') or uppercase ('A'-'Z').
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam labexam_q1

Question 2
(●●◌◌)
:

This question is of similar style to the second array hurdle question in the final exam.
(12 marks)

Your max_consec_row_diff function should look at every pair of adjacent rows in a given 2-dimensional array, and return the largest absolute difference between the sum of one row and the sum of the row directly below it.

max_consec_row_diff will be passed a two dimensional array with

  • num_rows rows and
  • NUM_COLS (#define'd to 5) columns.

For each pair of consecutive rows, work out the sum of each row, then take the absolute difference between those two sums. Compare every consecutive pair and return the maximum difference between any two adjacent rows. If the array has only one row, there is no pair of adjacent rows, so your function should return -1.

For example, if the 2D array contains these 25 elements, across 5 rows:

[3, 5, 1, 2, 0],
[6, 1, 5, 4, 3],
[1, 6, 2, 3, 7],
[19, 0, 0, 0, 0],
[0, 5, 1, 2, 3],

The absolute differences between the sums of each pair of adjacent rows are:

  1. Row 0 sum: 11, row 1 sum: 19 -> |11 - 19| = 8
  2. Row 1 sum: 19, row 2 sum: 19 -> |19 - 19| = 0
  3. Row 2 sum: 19, row 3 sum: 19 -> |19 - 19| = 0
  4. Row 3 sum: 19, row 4 sum: 11 -> |19 - 11| = 8
The largest of those differences is 8, so your function should return 8.

For example, if the 2D array contains these 5 elements, across 1 row:

[3, 5, 1, 2, 0]

Your function should return -1 as there is only 1 row so there are no consecutive pairs of rows to compare.

For example, with two rows:

[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
The first row sums to 5 and the second to 10, so the only consecutive difference is |5 - 10| = 5, and your function should return 5.

Testing

labexam_q2.c also contains a simple main function which allows you to test your max_consec_row_diff function.

Your max_consec_row_diff function will be called directly in marking. The main function is only to let you test your max_consec_row_diff function.

Assumptions/Restrictions/Clarifications.

  • max_consec_row_diff should not change the array it is given.
  • max_consec_row_diff should return a single integer.
  • max_consec_row_diff should not call scanf (or getchar or fgets).
  • max_consec_row_diff can assume the array always has at least 1 row and exactly 5 columns.
  • max_consec_row_diff should not print anything. It should not call printf.
  • Your submitted file may contain a main function. It will not be tested or marked.
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam labexam_q2

Question 3
(●◌◌◌)
:

This question is of a similar style to a debugging question in the final exam.
(5 marks)

You have been provided with a file called labexam_q3.c.

The code in labexam_q3.c is meant to do the following:

  • Scan in a movie start time as an hour and minute.
  • Scan in the length of the movie in minutes.
  • Calculate and print the time the movie will finish.
  • Print whether the movie can be watched before a 22:00 curfew.

Unfortunately, there are some problems with the provided program. Once fixed, your program should match the following examples exactly:

Examples

dcc labexam_q3.c -o labexam_q3
./labexam_q3
Enter movie start hour: 20
Enter movie start minute: 15
Enter movie length in minutes: 90
Movie will finish at hour: 21, minute: 45.
You can watch the movie!
./labexam_q3
Enter movie start hour: 21
Enter movie start minute: 30
Enter movie length in minutes: 45
Movie will finish at hour: 22, minute: 15.
Movie finishes too late!

There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, adding lines, or removing lines. Submit your working version of the code.

Assumptions/Restrictions/Clarifications.

  • The start time will be entered using 24-hour time.
  • The start minute will be between 0 and 59 inclusive.
  • The movie will always finish before midnight.
  • A movie that finishes exactly at hour: 22, minute: 0 can still be watched.
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam labexam_q3

Question 4
(●◌◌◌)
:

This question is of a similar style to a debugging question in the final exam.
(5 marks)

You have been provided with a file called labexam_q4.c.

The code in labexam_q4.c is meant to do the following:

  • Scan in a string.
  • Scan in a target character.
  • Divide the string into two parts:
    1. the part before the first instance of the target character, and
    2. the part after the first instance of the target character.
  • Print the two parts of the string to the terminal separately.
  • If the string does not contain the target character, print "Character not found in string.\n" to the terminal.

Testing

Once fixed, your program should match the following example exactly:

dcc labexam_q4.c -o labexam_q4
./labexam_q4
Enter a string: simple
Enter a character to split the string: m
Before: si
After: ple
./labexam_q4
Enter a string: simple
Enter a character to split the string: z
Character not found in string.
./labexam_q4
Enter a string: AbcdaBCD
Enter a character to split the string: a
Before: Abcd
After: BCD
./labexam_q4
Enter a string: abcdeabcde
Enter a character to split the string: b
Before: a
After: cdeabcde
./labexam_q4
Enter a string: porcupine
Enter a character to split the string: p
Before: 
After: orcupine
./labexam_q4
Enter a string: porcupine
Enter a character to split the string: e
Before: porcupin
After: 

There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, adding lines, or removing lines. Submit your working version of the code.

Assumptions/Restrictions/Clarifications

  • The input string will contain at least two characters.
  • Character comparisons should be case-sensitive, meaning 'a' and 'A' are treated as different characters.
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam labexam_q4