PR1EN6: Arrays continued

Lab content

Theory nugget 1: Passing text to a function

Many functions that otherwise seem generic and easily reusable, such as input functions or output functions, might have an issue that the text printed on the screen does not really work for us.

One way to handle this is what we did before – we printed the text separately and called the generic input/output function (such as read array, read number, print array etc). In general, this still remains my recommendation. This way you have less complexity that could otherwise hinder the reusability of this function.

However, as an alternative, I’ll give another idea. Let’s follow the basic principle of when to create a function – if you are copy-pasting code because you have a marginal difference between them, try to find a way to handle it using function parameters. This will now offer us a solution – we can pass the text we wish to print as a function parameter.

This works quite well if we want to give the user an input prompt on entering a value, such as an integer. It also can work reasonably well for printing an array with a label before it.

NB! Two additional important thoughts! Both of which we will talk in greater detail in the second part of this semester.

  1. Text is an array, composed of individual character. Each character also has its own index.
  2. As opposed to integer and float arrays, we do not need to pass the length of a text array to print it (we still need it for processing).

Theory nugget 2: Passing an array vs a member of an array

In the last lesson, we discussed how arrays behave with functions. Some key points

  1. Arrays were passed as the reference to the location of the array.
  2. We can manipulate arrays in functions. Modifications are persistent.
  3. Array length needs to be passed to the function alongside the array
  4. When passing an array to a function, we use the name of the array only, nothing else.

Additionally, as a reminder, when we put the square brackets after the name of the array in the middle of the code, we are indexing the said array.

This leads us to the following example, where we are highlighting passing an array to a function vs passing a member of the array to a function.

Tasks

In this class, you need to solve two tasks. Additional points are available for extra tasks.

Lab task 1: neg-pos reordering

The lab task is based on an algorithm specified by: https://blue.pri.ee/ttu/programming-i/algorithm-tasks/#Algorithm_3_Reordering_negative_and_positive_numbers

The algorithm is given to you.

The task extends the algorithm with additional functionality.

Algorithm

Requirements
  • Program must be based on the provided algorithm and extended based on the requirements.
  • The user is asked for 6 numbers, which are stored in an array.
  • A second array is created based on the rules set in the algorithm. In the algorithm, it’s referred to as the reordered array. In the task description, we will also refer to it as the algorithmically created array.
  • A third array is created, which will only contain numbers greater than zero. The length of the array might end up being smaller than the initial array.
  • After all 3 arrays are populated, they are printed out.
  • The program must contain a total of four functions. One to read the numbers, one to print an array, one to compose the array made of positive numbers and one to compose the array specified by the algorithm.
  • All functions for creating and printing of the arrays must be called from the main()  function.
Hints and recommendations

Start by completing the reading and printing of the array functions. Both of these you have done previously. Create the necessary array and reuse the functions you created last week. When copying the functions, don’t forget to copy the function comment!

Now lets create a function to make the algorithmically created array. First, create a new array that has the same length as the array where the numbers are entered in. Remember, you cannot return an array from a function. You need to declare this array in the main()  function and pass it to this function. To create the new array, follow the algorithm provided. Then print out the array you created. The code should look something similar once completed (names of functions and variables can be different).

If you feel that your main() function is starting to be overcrowded, the printed text can be passed with the function call. Look at the first theory nugget.

Now move on to the last function, where you need to create an array with only positive numbers. Most of it will look very similar to what you just did, just shorter as you don’t need to go through the array twice. Think carefully about how to get the length of the new array back to main()  function! This array will likely be shorter and have unused slots in the end.  Once you have the length of this array, call the printing function from main()  to print out the third array.

Testing

In the first test, we’ll run through the most typical case for this program, where we have both positive and negative numbers as well as a zero.

The program should should also account for the situation where no positive input values were given.

Lab task 2: Array indexing and comparisons

  • Read 5 integers from the keyboard
  • Ask user for two indexes.
    • Check that they are within the array bounds before proceeding. If needed, prompt the user again!
    • Numeration must be the same as when asking for input
    • Recommendation: Print the values corresponding to those indexes from the array for self-check
  • Using those two values you found, find the following results
    • Compare them, print out their relation to each other (e.g. is the first number smaller, greater or equal to the second)
    • Create a division operation. The operation must be formed in a way, that the dividend is always greater than the divisor. If needed, swap them around.
    • Division answer must be given as a real number. Display 1 place after the comma.
  • For this task, you need to create four functions. Functions are described for you in varying detail.
Functions to create

1. Reading the array. Reuse the code that you have done before. Copy it in with its comment.

2. Reading the index. For this, you need a new function. Let’s make it a generic and reusable function that you can use in the future – reading an integer in range. This way you can use it whenever you need one (i.e. from 0 to 5; from -10 to 10; etc). If you completed the extra task from last week, you will likely already have this function.

I’m proposing two ways to write this function. Pick the one that you prefer and implement it.

First version assumes that the question (what to enter) is presented to the user before this function is called.

The second version will present the prompt to the user by the function itself. This requires the prompt to be passed to this function (theory nugget 1).

3. Comparison of values. For this function, you need to pass the two numbers you are comparing. You need to print both numbers and their relation to each other (which one is bigger or are they equal). Output should be using the format “a < b”, “a > b” or “a = b”

NB! To pass numbers to this function, look at the second nugget of information.

4. Division. Similarly to the last function, this one also expects two values. Again, you need to do the comparison as the last time, but this time instead of printing the results, you need to choose the operands for the division operation.

Sample solution

NB! The sample has a visible debugging printout (starts with DEBUG). It’s recommended that you also do this to verify that you are indexing the array correctly. Once you are sure about your indexes and your code works, comment this out.

Testing

This is a partial list of the suggested tests:

  • Try a normal scenario that shouldn’t cause any errors.
  • Try out of bounds indexes ( -10, 10). Test them both as the first and the second index.
  • Try the out of bounds indexes multiple times to make sure that the program wouldn’t continue unless the inputs are usable.
  • Try indexes on the edge cases. E.g.
    • If you count from 0 – 6, try -1, 0, 6, 7
    • If you count from 1 – 7, try 0, 1, 7, 8
  • Try all 3 different results for comparison (<, >, ==)
  • Try to divide by zero. NB! Dividing by 0 is undefined! Just doing the division by zero may crash the program or reset the entire computer (it’s common for controllers to completely reset themselves if this happens). You are not allowed to perform divisions by zero!

Extra task 1: non-recurrent numbers

This is a continuation of lab task #1.

Requirements
  • Task must be built on the same base as the base task. The functionality of the base task must remain the program.
  • Solution can include both advanced tasks.
  • Create a new array based on the entered numbers. The new array must be built in a way that only a single instance of each number can exist in it.
  • Create functions as seem suitable for solving this task.
Testing

Extra task 2: additive inverses

This is a continuation of lab task #1.

Requirements
  • Task must be built on the same base as the base task. The functionality of the base task must remain the program.
  • Solution can include both advanced tasks.
  • Find and display the additive inverses of the numbers in the array.
  • Same numbers can be used in a pair only once, regardless of their ordering or repetition count.
  • Create functions as seem suitable for solving this task.
Testing

Make sure that the pairs don’t repeat.

After the class, you should

  • Know how to pass text (strings) into functions.
  • Know how and when to pass either the entire array or just a member from an array to a function.
  • Know and be able to compose and use arrays where not all members from the declared array are in use.
  • Be more comfortable indexing an array in other ways than from first to last element.
  • Be able to copy values from one array to another using two different indexes in the same loop.