8. lab: functions

Lab content

Lab tasks

Task 1: finding results from an array

The purpose of this task will be to create a program, that:

  • Reads 5 integers from the user that will be in between -100 and 100.
  • Prints the entered numbers out.
  • Finds and prints the arithmetic mean with 2 places after the comma.
  • Finds and prints the smallest member in the array.
  • Allows the user to enter a number and checks if it exists in the array or not.

The task has the following limitations (in addition to previous practices):

  • All of the functions described below will have to be implemented and used to find the results.
  • Constants defined using macros must be passed as a function parameter and not directly used in the functions.
  • If the function’s purpose is to find a results (e.g. arithmetic mean, smallest number, …), the result must be returned to main(). Side effects (printing the results within the functions) is not allowed.

Solve the program step-by-step. Test each function after creating it before proceeding. Avoid writing the code without functions at first! The functions given below are in the order we recommend writing them.

We have written a simple writeup on the parameters for each functions.  If you wish, you can add additional parameters to the second function (read array), but nothing else!

The writeup on the functions is simplified and allows the developer to make some decisions on their own. It is not enough to be used as a sample for homework 2!

1. function: reading an integer

Description: Reads one integer from the user, that is checked to be in the allowed range. The function must not return before the entered value is within the allowed range. If the user entered number wasn’t between the allowed minimum and maximum, the user will be warned and prompted again.

Parameters:

  • integer – minimum allowed value.
  • integer – maximum allowed value.

Return: integer, that is between the allowed minimum and maximum.

2. function: filling an array

Description: This functions reads n integers and stores them into the array. To use each integer, the previously completed function (reading an integer) is called. It is done repeatedly in a look for each array member that needs to be read.

Parameters:

  • integer array – the array that will be filled.
  • integer – array length.

Return: none.

3. function: printing an array

Description: Function prints values stored in the array.

Parameters:

  • integer array – the array that will be printed.
  • integer – array length.

Return: none.

4. function: arithmetic mean

Description: Function finds the arithmetic mean from the array and returns it.

Parameters:

  • integer array – array, that contains the members of which the average will be calculated.
  • integer – array length.

Return: float – arithmetic mean.

5. function: minimum value

Description: Function finds the smallest member of the array and returns it.

Parameters:

  • integer array – values from where the minimum value will be searched for.
  • integer – array length.

Return: integer – smallest number in the array.

6. function: check if value in array

Description: Function looks if the entered value is within the array or not. The result will be coded as an integer and returned.

Parameters:

  • integer array – values from were the search key is being looked for.
  • integer – array length.
  • integer – the value that is being looked for.

Return: integer / boolean – was the entered number present in the array or not.

Advanced: multiples of n to a new array

  • User enters a multiplier (positive integer)
  • Create a function that will create the new array
    • Values where the absolute value is a multiple of the entered multiplier will be added to the new array. New array members must also be absolute values.
    • Function itself must not print out the new array nor call a function to do so.
  • The newly created array must be printed out. Use a function that already exists.

Task 2: Converting an existing code to use functions

Take the following code and split it into functions:
https://blue.pri.ee/ttu/files/iax0583/warehouse_kontaktope.c

Create the following functions, move the code from main to those functions:

  • Array print (without return, code reuse)
  • Print message about how much will fit (without return)
  • Sorting (without return)
  • Combining in sorting facility (returns number of items moved to the third array)
    Note: This function can work with code reuse (called twice), but doesn’t have to!

Advanced: weight limit

Create a limit for the cargo exiting the sorting facility.

E.g. a truck can deliver up to 1500 kg at once. Fill it with heavier items first. If some items don’t fit the limit, try if a lighter item will fit.

Print out the items that did fit on the truck and then fit separately the items that were left in the sorting facility.

After the class, you should

  • Understand what a function is
  • Understand that we have been using functions from the first week. Both functions that return values and those that don’t!
  • Understand, that we can create functions just like those we have been using.
  • Understand the following terms
    • Function prototype
    • Function return type
    • Function arguments
    • Function parameters
    • Function header
    • Function body
  • Be able to create proper functions
  • Be able to pass variables and arrays to functions
  • Be able to store the value returned from a function
  • Be able to reuse functions with different arguments
  • Be able to divide your code to functions
  • Have a small list of universal functions that you can reuse in future codes!

Additional content