COMP(1511|1911) 25T2 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 Hurdle Requirement

COMP1911 Assessment Students

The course has ONE hurdle in the final exam that you must meet to pass the course:

Please note, you are NOT required to pass the Linked list hurdle to pass COMP1911.

COMP1511 Assessment Students

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

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

In this task, you will write a program that is given an array with exactly size number of elements. unique_sum should return the sum of the unique integers in that array.

For every unique value in the array, your program should add it to the sum of unique values. If there are no unique values in the array, your program should return 0.

Examples

If the array contains these elements:

{1, 2, 3, 4, 5, 6, 7}

Your function should return 28 because each integer in that array is unique.

In another example, if the array contains these elements:

{2, 4, 2, 2}

Your function should return 4, as there is only one unique integer in the array.

Testing

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

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

Assumptions/Restrictions/Clarifications.

  • You can assume that the array will have at most 100 values.
  • unique_sum should not modify the array it is provided.
  • unique_sum should not call scanf (or getchar or fgets).
  • unique_sum should not print anything, i.e. It should not call printf.
  • The provided starter code contains a main function. You can change it for your own testing, but it will not be autotested or marked.
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 magic_square function should compute the sum of each row, column and both diagonals in a given 2D array. If all rows, columns and diagonals sum to the same number, then the array is a magic square, and you will return this sum.

magic_square will be passed size and a square two dimensional array with:

  • size rows and
  • size columns.

Add code so that the magic_square function compares the sum of each column, row, and each set of the diagonals to see if they are equal. Your magic_square should return the sum you have found if the two dimensional array is a magic square, and 0 if it is not.

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

{1, 1, 2, 2, 1}, 
{1, 0, 0, 0, 6},
{1, 6, 0, 0, 7}, 
{13, 0, 1, 1, 0}, 
{5, 5, 1, 2, 2}, 

This is not a magic square and your function should return 0.

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

{17, 24,  1,  8, 15},
{23,  5,  7, 14, 16},
{ 4,  6, 13, 20, 22},
{10, 12, 19, 21,  3},
{11, 18, 25,  2,  9}

This is a magic square, as each row, column and diagonal sum to 65, and your function should return 65.

Testing

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

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

Assumptions/Restrictions/Clarifications.

  • magic_square should return a single integer.
  • magic_square should not change the array it is given.
  • magic_square should not call scanf (or getchar or fgets).
  • magic_square can assume the array should always be a square.
  • magic_square 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:

This program should scan in a budget price, 3 grocery items and their costs into a struct array, and determine which individual items are over budget.

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 a budget: 6
Enter 3 grocery items and their costs:
Apples
3.60
Bananas
4.50
Strawberries
7.20
Over budget items:
Strawberries
./exam_q3
Enter a budget: 10
Enter 3 grocery items and their costs:
Carrots
3.40
Lettuce
5.75
Tomato
2.80
Great work! All your items are within budget!

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.

  • Your code should produce no errors or warnings when compiling.
  • 3 different grocery item names will be given as input.
  • If multiple grocery items, but not all grocery items are over budget, they should be printed in the order they were inputted.
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.
  • Scan in a position for a character in the string.
  • Remove the character from that position in the string.
  • Print the updated string to the terminal.

Testing

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

dcc exam_q4.c -o exam_q4
./exam_q4
Enter a string: abcd
Enter a position to remove: 1
Result: bcd
./exam_q4
Enter a string: abcd
Enter a position to remove: 2
Result: acd
./exam_q4
Enter a string: abcd
Enter a position to remove: 3
Result: abd
./exam_q4
Enter a string: abcd
Enter a position to remove: 4
Result: abc
./exam_q4
Enter a string: The quick brown fox jumped over the lazy dog.
Enter a position to remove: 4
Result: Thequick brown fox jumped over the lazy dog.

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.
  • The position entered will correspond to a character in the string.

You can re-fetch the starter code for this question here
You can autotest this code with 1511 autotest-labexam exam_q4