Lab content
- Slides: Matrices
Lab tasks
This lab has two gradable tasks. Second task is extended by two extra tasks.
Task 1 [W09-1]: Calculating various results from a matrix
For this task, you will need to practice traversing through and indexing a matrix. You will need to complete four different subtasks. The task must be presented using stream redirection.
To begin with the task, download the following files.
- Starter code: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/9_1_matrix_starter.c
- Input file for stream redirection: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/9_1_matrix.txt
Requirements
- You are given a starter code and an input file that you need to use
- Program must be defended using stream redirection
- Add the printout of the matrix. It must be printed properly aligned, as shown in the testing section.
- Find and display the following results
- The element that is located in the user entered row and column position
- 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 important data (matrix itself, results) must be stored as integers. Data types intended for real numbers, such as float and double , are not permitted
- All integer type variables, that contain either data or results must be held in bit-precise variables. Data types such as short , int , long , long long are not permitted.
- The solution must be cross-platform. Formats such as %hd , %ld and %lld are not permitted.
- At minimum, the functions specified in the next chapter must be completed
Workflow guide
- Start by reading the matrix in the function ReadMatrix() . Function is mostly pre-completed for you. You need to complete the scanf() statement by specifying the correct format. Data format must be inttypes.h library, i.e. it must start with SCN .
- To validate, that all numbers were correctly read, print the matrix out in the function PrintMatrix() . The prototype is already given, implement the function. Don’t forget to correctly align the output.
- Now we start with the first subtask, where the user enters row and column position and you need to print the value located at that position
- Add the two numbers representing the positions into the input file
- Ask the user for the row and column values. For this, create a function that asks the input from the user in range of min … max, i.e.
GetIntinRange() . Both
min and
max values must be passed as parameters. Reuse the function to get both inputs.
Hint: You already completed this function and likely used it multiple times in previous weeks. - Print the value from the matrix at that position
- Now for the second subtask. Create a function that calculates and returns the sum of negative numbers on the main diagonal. The prototype for this function,
GetMainDiagonalNegSum(), has already been given in the starter code. Print the result in the
main() function. This function should have no side-effects.
NB! Avoid doing excess processing! Write on paper the indices that you need to traverse through. For our 7 x 7 matrix, you only need 7 positions – i.e. traversing through all 49 is excessive. Imagine for a moment traversing through the entirety of a 1000 x 1000 matrix – only 1000 positions out of 1 000 000 would be useful, making 99,9% of work a waste of resources. - Create a function that calculates and returns the product from positive values that are located above the anti-diagonal. Return the product to the main() function and print it there.
- Create a function to find and print the greatest values in each row. The design of this function is left for you to create. You can either find everything in this function or use a helper function (look at hints). Avoid having a any loops in the main() function!
Hints
- For printing the matrix, you can use a similar structure of loops as was shown in reading of the matrix. There are two important things two notice
- You must leave enough room for integers for them to align, including if they are negative.
- Place the printing of the newline in an appropriate location. Think of the purpose for the inner and outer loops. The location for newline can be derived from that.
- When traversing on the main diagonal, look at the row and column index values. They are identical! This means no need for nested loops. You don’t even need to check if (i == j) . Actually, even the variable j is unnecessary! For indexing, only the value of the index is important.
- If you encounter an unexpected result for the product, start by printing out all the intermediate products – i.e. put a printf() statement inside of the if() statement to keep track of the progression for the product.
- To find the greatest value, you should use the solution that you learned a number of weeks ago. You can either nest it inside of a loop to pick a row or you can also completely reuse the previous solution by using a “trick” where you will pass it the location for the beginning of a row in the matrix. To do that, you can call the function in the following way: FindMaxValue(matrix[rowIndex], rowLength)
Testing
This include the answers expected from the given matrix.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Given matrix: 10 9 8 0 -10 8 5 7 6 9 1 -7 -9 4 -5 5 -5 5 -5 5 -5 8 7 6 5 4 3 2 1 2 3 4 5 6 7 -1 -2 -3 -4 -3 -2 -1 0 0 0 0 0 0 0 The value for the given row (2) and column (3) is 9 The sum of negative elements on the main diagonal is -7 The product of positive elements above the anti-diagonal is 36578304000 Greatest number on row 1 is 10 Greatest number on row 2 is 9 Greatest number on row 3 is 5 Greatest number on row 4 is 8 Greatest number on row 5 is 7 Greatest number on row 6 is -1 Greatest number on row 7 is 0 |
Task 2 [W09-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_starter.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
- Print out the floor plan just as regular matrix with all numbers. Do not try to adjust what’s printed yet.
- 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
- Continue to interpret the other codes.
- Now that the floorplan is printed, print out the row numbers on the side.
- Ask the user for their preferred seat. Don’t forget to sanity check them!
- The row index can be calculated in one step. Column index has to be adjusted for the missing seats.
- Print out the indexes in the array data structure to validate that you got the correct position!
- 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:
Sample output after solving both extra tasks:
Extra task 1 [W09-3]: 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 [W09-4]: 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

