10. lab: Menu-program

Theory and reminders

Characters, strings and output functions

Even though we will deep-dive into strings in a few weeks, lets take a look at a few things

Strings is a sequence of characters. It can be a word or a sentence, but also just a sequence of symbols. Strings are between double quotes.

Examples:

    • "a"
    • "Hello!"
    • "I do wish we could chat longer, but I'm having an old friend for dinner."
    • "lFRD=)m+0FDj2"

Characters are written in between single quotes. A string is composed of such characters.

NB! Symbols starting with a backslash are called escape sequences and they are stored as a single character in the computer memory (e.g.   \n  ).

Examples:

    • 'a'
    • 'y'
    • '\n'
    • '\t'

Now to some functions for text

printf()   – print formatted. This is the most complicated output function that we’ve looked at so far. We can use it to print text, format it and also output the contents of variables. Newlines must be manually written.

Example: printf("Hello, %s!\n", name); puts()  – put string. This is a simple output function used for printing text on separate lines. It does not support formatting and printing contents of variables. It however will print a newline after the text.

Example: puts("Hello, user!"); putchar()   – put character. This is the simplest output function out of them. It allows to print a single character only. It is good to use it for printing newlines for an example.

Example: putchar('\n');

Passing arrays to functions

When passing arrays to functions, we are actually passing a reference to the location of the original data. This means we can modify arrays in any function without returning.

Most of the time, you will need to also pass the size of the array in C. This is due to the fact that it will not be possible to calculate the size after it has been passed to a function! An exception would be strings, which we’ll discuss further later.

Some more tricks and specifics

  • If you want to pass the whole array, regardless of the number of dimensions, we only specify the name of the array Func(numbers);
  • If you wish to pass only a single value from a 1-dimensional array int numbers[N]; , we will specify the index of that member Func(numbers[2]); . In the function we will use it as a single integer Func(int val);
  • If you wish to pass the beginning of a row from a 2-dimensional array int numbers[N][M]; , we will specify the index of that row and the length of it Func(numbers[2], M); . In the function we will use it as a 1-dimensional array Func(int numbs[], int n);
  • If you wish to pass only a single value from a 2-dimensional array int numbers[N][M]; , we will specify both indexes  Func(numbers[2][1]); . In the function we will use it as a single integer Func(int val);

Swapping two values in an array

To swap to values, we can use a method we learned during bubble sort. For this, we need a temporary variable.

Note: to apply this swapping for a swapping of rows or columns, you only need a single variable.

It is only beneficial to use a temporary array instead of a variable if you can apply parallel operations on a SIMD (sing-instruction-multiple-data) type of architectures such as found in GPUs (graphics processing units).

Zero-initialising an array

To zero-initialize a whole array, we put the constant in curly braces.

If it is a 2-dimensional array, need two sets of curly braces.

Tasks

In this task we’ll create a menu-program where the user can choose when they want to exit. We’ll be working with matrix manipulations in this program.

To start off, try the sample program, after which you should continue to solve the task.

Sample program

You are given a sample of the program you will create within this lab task. It is given as a compiled binary – you can run and test it to take a look at what the final product should look ike. In the end, your program should work similarly.

Both versions of the sample program are compiled for x86-64 architecture. They are guaranteed to work on the lab computers, but should also work on most of your personal computers. There can be issues with ARM processors without using x86-64 emulation.

NB! On Linux you must give the permission to execute it manually (safety first!). Changing the permission is a one-time task.

This will add the execute permission for the user.

  • chmod – change mode
  • u – user
  • x – execute

After this you can run the program as usual ( ./program_name)

Task description

The lab task is separated into two separately graded parts, which can be continued by adding extra tasks.

The program should work until the user chooses to exit. This may require you to add additional checks for the state of the matrix.

The maximum size of the matrix in memory is defined using macros LIMIT_ROWS and LIMIT_COLS , which define the maximum row and column counts. Make sure to use the correct ones!

Since the user can choose to use less than the maximum matrix size, part of the matrix may be left unused. To account for this, all functions should keep track of how much of the matrix is in use. FOr this, you will see  int rows  and int cols  being used in some functions for the amount of rows and cols currently active.

Download the starter code: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/10_switch_basecode.c

General requirements

These requirements apply regardless of which part of the program you are presenting.

  • Your solution must mimic the sample program as close as possible
  • The work must be built on the starter code
    • You are allowed to modify the starter code within reason. E.g. it might be beneficial to reorder some statements in the main function or add another parameter to the function GetIntInRange() . For any other modifications, think hard if they are necessary or useful!
    • Starter code already has 2 functions made for you. Use them as needed in the parts that you will code on your own!
    • Starter code also has the prototypes of the first two functions that you need to create during the first part of the task.
  • All the numbers in the matrix must be randomly generated and from 0 – 99.
  • All actions must be repeatable without exiting the program.
  • All of the processing code for the menu actions must be created as functions
    • You are free to print out helping texts and prompts in the switch() itself, as well as accept returned values from functions. If necessary, you can also add some if statemens.
    • You are not allowed to add any new loops into the main() function!
  • The program must be consistent and clear for the user. It must work until the user chooses to exit.

Lab task part 1

In the first part, we’ll complete the generation and displaying of the matrix.

Requirements

  • Use can generate an n * m matrix.
    • Matrix will be populated with randomly generated numbers (0 – 99)
    • Values n and m are entered by the user. If necessary, create limits for user input.
    • You are only allowed to use do while()  loops in  GenerateMatrix() function.
    • Reminder! C functions are simple and only do one thing at a time (and do it well)! Function   GenerateMatrix() is not allowed to handle user input!
  • User can display a matrix
    • You are only allowed to use  while()  loops in DisplayMatrix() function.
    • Matrix must be nicely aligned.
  • User can exit the program

Lab task part 2

In the second part, we will create functions for swapping rows and columns.

Requirements:

  • User must be able to swap two rows.
    • You are only allowed to use for()  loops in the swapping function.
  • User must be able to swap two columns.

Common mistakes

  • Copy-pasting, also known as repetitive code. Mostly this will happen in the input part (e.g. number of rows, columns etc)
  • Off by one issues. As a reminder, first row index is 0.
  • Magical numbers
  • Allowing matrix operations before having a matrix. You cannot display a matrix that has not yet been generated!
  • Wrong loop types (part 1 and 2 have fixed loop types that you can use!).
  • Mixing up row and column counts. Try the following
    • generate a 3 x 5 matrix, swap rows 1 and 3
    • generate a 5 x 3 matrix, swap columns 1 and 3

Extra task 1: deletion

Add additional features to your program

  • User can delete a row that they desire.
  • User can delete a column that they desire.
  • After deleting a row, all rows following it will rise higher by 1. Similarly after deleing a column, all columns following it will move the the left by 1. The size of the matrix will shrink.
  • Add the required functionality to the menu.

Extra task 2: addition

Add additional features to your program

  • User can add a new row to the desired location.
  • User can add a new column to the desired location.
  • All elements for the added row / column will be generated randomly.
  • Any rows or columns after the desired location will move forward by 1. The size of the matrix increases.
  • Add the required functionality to the menu.

Extra task 3: transpose

Add additional features to your program

  • User can transpose the matrix.
  • Add the required functionality to the menu.

After this class, you should

  • Be able to create infinitely running menu-programs.
  • Know a few additional functions for text output.

Additional content