PR1EN10: Menu-program

Theory and reminders

Characters, strings and output functions

Even though we will deep-dive into strings in a few weeks, we will introduce a few properties today.

A strings is a sequence of characters. It can be a word or a sentence, but could also just a sequence of characters. 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 also coded (and stored) as a single character in the computer memory (e.g.   \n  is really a single character, it takes up just one byte).

Examples:

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

Notice, that 'a'  and "a"  were both shown in the example and the are different. One is a string, the other is a character.

Now lets take a look at some functions that are used for printing strings and characters.

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 added.

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 will print a newline in the end of the string.

Example: puts("Hello, user!"); putchar()   – put character. This is the simplest output function out of them. It only allows printing of a single character. It is often used for printing single newlines or other special characters.

Example: putchar('\n');

Passing arrays to functions

When passing arrays to functions, we are actually passing a reference to the location of the array. This means we can modify  the array members in all functions without using a return statement.

In C, you typically need to also pass the size of the array. 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, you can use a method we learned during bubble sort lesson. For this, you will need a temporary variable.

Note: to apply this method for a swapping of rows or columns in the lab task, you only need a single variable.

Using an array can be beneficial if you have support for parallel processing – e.g. you can copy over multiple values at the same time instead of sequentially. This is a common approach for graphics card, which are optimized for highly parallelized workloads. This is also a very common for any kind of AI based solutions, that are typically based off matrix calculation, that you will learn in linear algebra.

Zero-initializing an array

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

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

Tasks

In this class, you will create a menu based program, that will work until the user desires to exit. The program handles some basic matrix manipulation.  Pay attention to edge cases during the task – i.e. what happens if the user wishes to manipulate a matrix, that does not yet exist.

Sample program

You are given a sample of the program you that you need to mimic for 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 like. 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 (this is a security requirement). 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 not to mix them up!

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 large of an area 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 solution 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.
  • The application should work with a matrix that has different number of rows and columns, i.e. is not a square matrix.
  • All the numbers in the matrix must be randomly generated and be in range of 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  statements.
    • Do not declare any variables in the switch statement!
    • You are not allowed to add any new loops to the main()  function!
  • The program must run until the user chooses to exit. All prompts for the user must be clear. The program must work as a whole, as you add new parts to it.

Lab task part 1 [W10-1]: Generating and displaying of the matrix

In the first part, we’ll complete the generation and displaying of the matrix. You will also be practicing nesting different loop types!

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 properly aligned.
  • User can exit the program

Lab task part 2 [W10-2]: Swapping rows and columns

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 [W10-3]: 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 deleting 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 [W10-4]: 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 [W10-5]: Transposing

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.
  • Know the difference between a character and a string in C code
  • Be able to pass to a function
    • a whole matrix
    • one row from a matrix
    • one element from a matrix

Additional content