9. lab: matrices

 

Lab content

Lab tasks

This lab has two gradable tasks. Second task is extended by two extra tasks.

Task 1: Calculating various results from a matrix

In this task we’ll practice navigating a matrix and finding results from it.

The task is graded separately in 3 parts.  Look for “Graded parts” below.

To start, download the following files.

Note: the task must be shown with stream redirection with the provided data file in order to be accepted!

Requirements
  • You have been given a starter code and an input file to use
  • Program must be shown using stream redirection
  • Add the printout of the matrix
  • Find and print the following results
    • The sum of negative elements on the main diagonal
    • The product of positive elements above the anti-diagonal
    • The greatest value in every row
  • All values must be stored as integers. Data types such as float, double and such are not allowed.
  • If you want to use smaller or bigger data types than a 32-bit int, you must use it precisely (e.g. through inttypes.h ). Data types such as long  and long long  are not allowed.
  • The solution must be cross-platform. Formats such as %ld  and %lld  are not allowed.
  • You must complete at least the listed functions below under “required functions”.
Required functions

You need to have at least 5 user-defined functions created in your program. You are allowed to create more if it suits the purpose.

  • Reading the matrix. During the function, all values were be stored into your 2-dimensional array (matrix). This has been done for you.
  • Printing of the matrix. During the function, the matrix will be printed onto the screen as shown in the example below. Function has no return value. Prototype for it has been done for you.
  • Finding the sum of negative numbers from the main diagonal. Function returns the sum to main() function, where it will be printed. Side effects are forbidden! Prototype for it has been done for you.
    NB! Think carefully about which values need to be summed up and watch out for excess work. Check out the indexes where your values are and make sure that you only go through those and nothing else. E.g., going through the entire 49-member array only to add up 7 values is not ok. Imagine if this was a 1000 x 1000 matrix – in that case you would visit a million numbers only to find 1000 of them. This would make the useful work to be 0.1% (reduction in efficiency is exponential if you go through the entire array).
  • Finding the product of positive values that are positioned above the anti-diagonal. Function returns the product to main() function, where it will be printed. Side effects are forbidden.
  • Finding the greatest values in each row. This one is entirely up to you how to design. You can have side effects and print the results inside of this function. You can add some helper functions as needed as well.
Grading

The task has 3 graded parts, marked separately.

There are 2 requirements which must be completed regardless of which part you intend to present

  1. Program must be executed using stream redirection
  2. Matrix must be printed on the screen

Separately graded parts

  • Part 1: the sum of negative elements on the main diagonal
  • Part 2: the product of positive elements above the anti-diagonal
  • Part 3: the greatest value in every row
Hints
  • Look at how ReadMatrix()  is written. You can write the parameter list for your other functions based on this
  • Printing: Again, take inspiration from ReadMatrix()  function. This already has everything you need to go through all the positions. Now you just need to format the printout nicely. To figure out the line change, think of what is the purpose of inner and outer loops!
  • Part 1: Write the indexes that you need to sum up. Take a look at those indexes and think of how many variables (and loops) you even need to go through all of them!
  • Part 2: Remind yourself what happened during an integer overflow!
  • Part 3: You can pass a single row of a matrix into a function. For this, we will specify the index of the row we wish to pass as well as pass the length of that row. This way you can re-use a function that you’ve created previously. E.g. FindMaxValue(matrix[rowIndex], rowLength)
Testing

This include the answers expected from the given matrix.

Task 2: Cinema hall

We have been given a layout with the current bookings for a cinema hall. It is represented as a 2-dimensional array. Your task will be to correctly interpret the values to display the hall plan.

All seats in the hall are coded with the following integers:

  • 0 – the seat does not exist at this location
  • 1 – the seat is free
  • 2 – the seat is already booked

The array position 0, 0 corresponds with the top left seat of the movie hall.

Download the starter code with the hall plan here: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/9_2_cinema_init.c

Requirements
  • Print the hall plan. Program’s output must match the one given in the sample
    • Row numbers are placed on the left side
    • All rows start with the seat number 1. You must account for missing seats (and not count them – look at the example)
    • Free seats are marked as seat numbers
    • Missing seats are marked as an upper case ‘X’ character
    • Seats that don’t exist are not depicted
    • The location of the screen is shown in the plan
  • Ask the user for a seat (row, seat number) and print if it is free or not.
  • At minimum, you should have 2 functions besides main. One for printing the plan, another for checking the availability of the chose seat. You are free to create more as you see fit.
Workflow recommendation
  1. Print out the floor plan just as regular matrix with all numbers. Do not try to adjust what’s printed yet.
  2. Start altering the output (interpreting the coded values in the array). Create a conditional statement to print “nothing” instead of the code that represents missing seats
  3. Continue to interpret the other codes.
  4. Now that the floorplan is printed, print out the row numbers on the side.
  5. Ask the user for their preferred seat. Don’t forget to sanity check them!
  6. The row index can be calculated in one step. Column index has to be adjusted for the missing seats.
  7. Print out the indexes in the array data structure to validate that you got the correct position! 
  8. Now finish the program.
Hints
  • The floor plan is flipped compared to the array indexes. It may help you to draw it out and write the indexes for the first and last row compared to the floorplan.
    • Row 14 has the row index of 0
    • Row 1 has the row index of 13
  • To figure out the seat number, use a separate counter
  • A really clean solution to handle such a coded integer is a switch()  statement.
Testing of the program and samples

NB! The sample also uses colors in code, your solution doesn’t have to (but can 🙂 )

We’ve added a small debugging line where we are showing the index of the array we are checking for seat availability. It’s recommended to add it to yours as well, as it will be easier to check for correctness of it.

Sample output after solving the base task:

Click me to see the image

Sample output after solving both extra tasks:

Open me to see the image

Extra task 1: n tickets

Lets add a feature so that the user can book multiple tickets with one purchase. Add this to the base task.

  • User is asked for the number of tickets required.
  • All of the seats offered must be in the same row, next to each other.
  • Use the entered seat position as the starting point for your search. The other seats can booked to the left or to the right of the chosen seat, including to both sides at the same time.
  • Display the offered seats

Extra task 2: confirmation

Lets add a confirmation before the seats are booked

  • You must have both the base task and extra task 1 completed and in the same program for this.
  • After giving the offer to book the seats, prompt the user a confirmation if the offered seats are OK.
  • If the user refuses the seats you offered or there were not enough seats available at the chosen location, allow them to choose again (both seat position and number of tickets)

Hint: There are 3 coded values in the initial matrix. If you wish, you can add them. As an alternative, you may just want to make a copy of the initial matrix.

After the class, you should

  • Be able to use various length integers in code, including a 64 bit integer in cross-platform compatible code.
  • Understand how integer overflows happen in code
  • Be able to visualize a two-dimensional array
  • Understand how to declare and index a two-dimensional array
  • Be able to index only a specific part out of that array (e.g. first row, last row, first three rows, second element from the third row etc.)
  • Understand and be able to use matrix diagonal properties
  • Be able to go through the array both row by row and column by column

Additional content