PR1EN5: Arrays

Lab content

Tasks

The task for this week is separated into two parts. First finish the first part and present your solution. Then improve your existing solution by solving the second part and present the solution again. The base task can be extended by two extra task.

Task 1 part 1

Part 1 will be solved as a UML activity diagram in the class. You will have to implement the algorithm as a program based on the starter code presented. In part 1, you will have 4 functions besides main() !

Download the starter code here: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/5_minmax_template.c

Requirements
  • Part 1 must match the logic described in the UML activity diagram.
  • You have to complete all 4 functions that are described in the starter code as comments and use them in your solution. You will need to also add missing parts like macros, variable declarations, function prototypes and function calls.
  • User will enter 6 numbers from the keyboard, which will be stored in an array.
  • The process of entering the numbers must be clear to the user. You must show which number they are entering out of how many at all times (e.g. 3/6 would be 3rd number from 6 in total).
  • You need to find and print the smallest number in the array. The smallest value needs to be found in a separate function, returned and printed in main().
  • You need to find and print the greatest number in the array. The smallest value needs to be found in a separate function, returned and printed in main().
  • You need to print out the original array. This must be done in a separate function.
Workflow guide

NB! For every function, first read the function comment to understand what the function does, which inputs are provided as parameters and what the result (return) has to be. Each function must be implemented below the comment.

  1. Start by completing ReadIntArray()  function given in the starter code. This is the first function after main() .
    You will need to write a loop that on each iteration, prints the prompt and then reads the value, storing it into the array.  There will be a very similar example done in the lab as live code.
  2. To test this, we need to call it out. For this, we need to declare the array in the main function and then call the function, passing it the array and its length.
    Assuming you have a macro called NUM_CNT  and an array called numbers , the function call will look like  ReadIntArray(numbers, NUM_CNT); Test and see if your reading code works.
  3. To validate our results, we should print what we got.  Complete the function PrintArray()  and call it from main() .  Note that this time you have to set the parameters and return value yourself.
  4. Now you have two more functions to complete. Both are specified as function comments in the starter code. Finish the functions, call them out and print the results.
    NB! you are not allowed to print the results in the functions that find min and max values. Printing statement must be in the main() function!
Testing

Test 1: min first, max last

Test 2: min last, max first

Test 3: Min value is negative. Both min and max are in the middle of the array.

Task 1 part 2

In part 1 you found the extreme values from the array. In part 2, you need to find the positions of those values and how many times they occurred.

Requirements
  • Print all positions of the greatest value in the array. Printing must be done in a separate function, outside of main() .
  • Print all positions of the smallest value in the array. Printing must be done in a separate function, outside of main() .
  • Print how many occurrences the min and max value had. Results must be found in a separate function and printed out in main() .
  • Positions printed must match the way you asked the user for input!
  • Implement the two functions specified under Workflow Guide. Reuse the created functions for both min and max values.
Workflow guide

To solve this part of the task, you need to create 2 functions. Both functions are given to you as function comments. You will need to implement the functions based on the comment.

First function will be to print out all the positions of the array. To do this, you need to loop through the array and every time you find the  number you are searching for (i.e. min value), you need to print out the position you are currently at in the loop! So in total, the function will consist of a loop, a conditional statement and a print statement – that’s it!

You need to reuse this function for both min and max! Note that the text describing the results should be printed in main (i.e. “Min value position(s): “)

After implementing this function, check that the code works before proceeding to the next function!

The second function will be very similar to the first one. In this one, you need to count instead of print. Once finished, just return the result you counted and print it in main() .  There is no printing of the result in the function itself!

Testing

Test 1: only 1 occurrence of min and max

Test 2: Multiple occurrences of min and max

Extra task 1:Statistics

In this extra task, we will calculate 3 more results from the data in the array.

Requirements

  • Implement 3 functions, that will find the sum, product and arithmetic.
  • Calculating arithmetic mean should reuse the function to calculate sum so you wouldn’t copy-paste code.
  • All 3 functions must use the array and the length of the array as their parameters.
  • All functions must calculate the result and return the answer. Result must be printed out in main() .
  • Display the arithmetic mean with 3 places after the comma.
Testing

The biggest stumbling block in this task is getting the arithmetic mean correct. Let’s test it.

Extra task 2: n numbers without VLA

To make the program a bit more useful, we’ll allow the user to enter the amount of numbers the program can work with.

NB! Even though most C compilers work with VLAs (variable length array), not all versions of the C standard  define it as mandatory property of the C language. Depending on the compiler, it may completely fail to work.

In addition, there are also downsides to performance of VLAs, which make them less preferred in performance-oriented tasks. In short, VLAs translate to more complex assembly code which takes more CPU cycles to execute. In addition, if no other checks are performed, it is extremely easy to crash programs using VLAs (i.e. I want to enter 10 000 000 numbers).

Because of this, you will not be allowed to declare an array where the size is defined by another variable (e.g.  int numbers[numberCount]; , where numberCount  is a variable specified by the user during runtime). The universal solution based on the standard is by using dynamic memory allocation, but that is a topic for Programming 2.

For the purpose of this lab task, you will decide on a maximum size that the user can enter and use that as the upper bound. This means, that most likely there will be unused slots in the array. However since our program is small, this won’t cause us trouble.

Note, that in general, use of VLAs is not forbidden in this course. The limitation is for this task.

Requirements
  • Allow the user to specify how many numbers they will be entering
  • Limit the program so that the user cannot enter a number greater than the size of the array defined.
  • Asking of the size from the user must be handled by a function you create for this task. The function will only be allowed to return if the input is within the specified limits.
    • Use parameters to specify the limits
    • The function you create should be reusable in other similar situations (but with different limits).
  • You can choose the “reasonable” size of the array yourself (e.g. 50 members).
  • Create error handling for situations where the user enters the array size which is either too big or small.
  • Usage of VLA (variable length array) is forbidden in this task!
  • NB! Make sure to update your coding style – variables cannot be written in the same style as macros!
Testing

Make sure to test the following

  • Minimal allowed number count.
  • Maximum allowed number count.
  • Number count that is different from the original task.

After the class, you should

  • Look into homework 1 requirements and go through the videos
  • Know how to perform type casting
  • Know about floating point precision and problems with it
  • Know how fixed point and floating point number representation works.
  • Know about math library and how to use it, including how to set the compiler flag, if necessary
  • What an array is
  • How to declare an array
  • How to initialize arrays
  • How to index each member of the array
  • How to index through an array using loops
  • How to find the extreme values in an array
  • How to pass arrays to functions

Additional content