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 chooses to exit. The program handles some basic matrix manipulation.  Pay attention to edge cases during the task – e.g. what happens if the user wishes to manipulate a matrix, that does not yet exist.

Example program

The example program is given to you as a binary that’s been compiled by us. This allows you to test the final expected solution and compare it to yours. There are three different example programs, showing you the application in various states of completion. Try those examples before programming your own solution. It should mimic the example as close as possible.

The example programs 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. Download from here: https://blue.pri.ee/ttu/files/iax0583/demoprogrammid/wk10_solution_binaries.zip

NB! You may need to add an execute flag to the program before you will be allowed to run it. 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 base task is divided into two separately graded subparts and expanded by extra tasks.

The program should work until the user chooses to exit. This is why it’s important to keep in mind the state of the application, i.e. in this case, the matrix. E.g. if you have 0 rows in the matrix, you wouldn’t be able to swap rows on such a 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. 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 working on.

  • 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 allowed to print out helping texts and prompts in the switch()  statement. You can also accept returned values from functions and do assignments and calculations. If necessary, you can also add some if  statements.
    • You are not allowed to add any new loops to the main()  function!
    • Do not declare any variables in the switch statement!
  • 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

  • User can generate an n * m matrix
    • Generating the matrix is done in the  GenerateMatrix() function
    • Matrix will be populated with randomly generated integers  in between 0 – 99
    • Values n and m are entered by the user
    • 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 ask for user input!
  • User can display a matrix
    • Printing the matrix is done in the  DisplayMatrix() function
    • You are only allowed to use  while()  loops in DisplayMatrix() function
    • Matrix must be properly aligned
  • User can add a new row to the end of the existing matrix
    • Adding the row is done in the  AddRow() function
    • You are only allowed to use  for()  loops in AddRow() function
  • User can exit the program

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

The second part of the lab task adds swapping of rows and columns. It also adds a comfort feature to automatically display the modified matrix and ability to control the automatic display.

Requirements:

  • User must be able to swap two rows that they choose
  • User must be able to swap two columns that they choose
  • User must be able to control the automatic printing of the matrix (enabled, disabled)
  • If the automatic printing of the matrix is enabled, it will only print after actions that modify the matrix or its elements.

Common mistakes

  • Magical numbers
  • Off by one issues. As a reminder, first row index is 0.
  • Allowing matrix operations before having a matrix. You cannot display a matrix that has not yet been generated!
  • Wrong loop types (first part has requirements for loop types)
  • 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]: Addition and deletion

This extra tasks improves by adding functions to increase and decrease the size of the matrix

  • User can add a new column to the end
  • User can delete their chosen row
  • User can delete their chosen column
  • 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 be reduced.
  • Add the required functionality to the menu

Extra task 2 [W10-4]: Mirrorong

Add additional features to your program

  • User can transpose the matrix
  • User can mirror the matrix horizontally
  • User can mirror the matrix vertically
  • 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