COMP1511 26T1 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 COMP1511 and exclusion from UNSW.

Exam Hurdle Requirement

The course has TWO hurdles in the final exam that you must meet to pass the course:

This exam contains example array hurdle questions only . Example linked list hurdle questions will be in the Week 10 Prac Exam.

Exam Environment (In-Person)

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.

Questions

Question 1
(●◌◌◌)
:

This question is of similar style to the first array hurdle question in the final exam.
(12 marks)
You can fetch the starter code for this question here or by running 1511 fetch-labexam exam_q1

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

Your print_from_target function should:

  1. Print Please enter a target character: and scan in a target character from standard input.
  2. Print Please enter an integer: and scan in a non-negative integer n from standard input.
  3. Find the first position where that character appears in the array.
  4. Print the next n elements in the array (the elements immediately after that occurrence), each on a new line.

You may assume that the target character always appears at least once in the array, and that the next n elements after its first occurrence all lie within the array (you will not read past the end of the array).

Examples

For example, given the array:

['b', 'a', 'n', 'a', 'n', 'a']

And the target character a with n = 3 entered via standard input, the function should print:

n
a
n

Since the first 'a' is at index 1, the next three elements are 'n', 'a', 'n' at indices 2, 3, 4.

For example, given the array:

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

And the target character e with n = 5 entered via standard input, the function should print:

l
l
o
 
w

Since the first 'e' is at index 1, the next five elements are at indices 2 through 6.

For example, given the array:

['a', 'b', 'c', 'd', 'e']

And the target character c with n = 2 entered via standard input, the function should print:

d
e

Since the first 'c' is at index 2, the next two elements are 'd' and 'e'.

Testing

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

Your print_from_target function will be called directly in marking.

Assumptions/Restrictions/Clarifications

  • If n has a value of 0, nothing should be printed.
  • Your print_from_target function should not return anything.
  • Your print_from_target function should not modify the array.
  • You may assume the target always appears in the array.
  • You may assume that n is chosen so that those n elements exist (no out-of-bounds access).
  • You may also assume that n will always be a positive integer.
  • The array can contain any ascii character.
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam exam_q1

Question 2
(●●◌◌)
:

This question is of similar style to the second array hurdle question in the final exam.
(12 marks)
You can fetch the starter code for this question here or by running 1511 fetch-labexam exam_q2

Your count_odds_evens function should scan each row of a given two-dimensional array and return how many rows contain exactly the same number of odd integers as even integers.

count_odds_evens will be passed a two-dimensional array with

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

Each row has an even length (6 values), so it is possible for a row to be balanced (that is, three odds and three evens). Add code so that count_odds_evens counts every row where the number of odd values equals the number of even values, and returns that count.

For example, if the two-dimensional array contains these 24 elements, across 4 rows:

[1, 2, 3, 4, 5, 6],
[2, 2, 2, 2, 2, 2],
[0, 1, 2, 3, 4, 5],
[1, 1, 1, 1, 2, 2],
  • Row 0 has three odds and three evens (balanced).
  • Row 1 has zero odds and six evens (not balanced).
  • Row 2 has three odds and three evens (balanced).
  • Row 3 has four odds and two evens (not balanced).
So your function should return 2, as there are 2 balanced rows.

For example, if the two-dimensional array contains these 6 elements, across 1 row:

[1, 1, 1, 1, 1, 1]

Your function should return 0 as that row has six odd values and no even values.

Testing

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

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

Assumptions/Restrictions/Clarifications.

  • count_odds_evens should not change the array it is given.
  • count_odds_evens should return a single integer (the count of balanced rows).
  • count_odds_evens should not call scanf (or getchar or fgets).
  • count_odds_evens can assume the array always has at least 1 row and all rows will have exactly 6 columns.
  • count_odds_evens 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 exam_q2

Question 3
(●◌◌◌)
:

This question is of a similar style to a debugging question in the final exam.
(5 marks)
You can fetch the starter code for this question here or by running 1511 fetch-labexam exam_q3

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

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

  1. Scan in a student's first initial as a single character.
  2. Scan in their second initial as a single character.
  3. Scan in their WAM as an integer.
  4. Print exactly one line of the form: firstInitial.secondInitial has a <band> wam where <band> is the correct grade band:
    • fail if WAM is less than 50.
    • pass if WAM is from 50 to 64 (inclusive).
    • credit if WAM is from 65 to 74 (inclusive).
    • distinction if WAM is from 75 to 84 (inclusive).
    • high distinction if WAM is from 85 to 100 (inclusive).

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

Examples

dcc exam_q3.c -o exam_q3
./exam_q3
Enter first initial: A
Enter second initial: B
Enter WAM: 72
A.B has a credit wam
./exam_q3
Enter first initial: Z
Enter second initial: Y
Enter WAM: 45
Z.Y has a fail wam
./exam_q3
Enter first initial: M
Enter second initial: K
Enter WAM: 91
M.K has a high distinction wam

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, moving lines, or removing lines. Submit your working version of the code.

Assumptions/Restrictions/Clarifications.

  • You may assume each initial is a valid single letter (or character) as given.
  • You may assume the WAM is an integer between 0 and 100 (inclusive).
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam exam_q3

Question 4
(●◌◌◌)
:

This question is of a similar style to a debugging question in the final exam.
(5 marks)
You can fetch the starter code for this question here or by running 1511 fetch-labexam exam_q4

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

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

  • Scan in a string.
  • Replace each vowel in the string with the letter next in the alphabet. I.e., replace 'a' with 'b', 'e' with 'f' and so on as outlined by the table below.
  • Print the resulting string to the terminal.
  • If the string does not contain a vowel, the string should be printed unchanged.

Testing

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

dcc exam_q4.c -o exam_q4
./exam_q4
Enter a string: water
Result: wbtfr
./exam_q4
Enter a string: aeiou
Result: bfjpv
./exam_q4
Enter a string: rhythm
Result: rhythm
./exam_q4
Enter a string: apple pie
Result: bpplf pjf
./exam_q4
Enter a string: !hi5!
Result: !hj5!
./exam_q4
Enter a string: a
Result: b
./exam_q4
Enter a string: 9
Result: 9

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 one character.
  • Alphabetical characters entered will only be lowercase.
  • The string entered will have a maximum of 50 characters including the newline and null terminator.
  • Any non-alphabetical characters should be left unchanged.
  • If the string does not contain a vowel, the string should be printed unchanged.
You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam exam_q4