How do you find the largest value in an array without max in ICS3U?
Store the first element in a variable, largest = a[0], then loop over the indices 1 to the last one and replace largest each time an element is strictly greater. After the loop, largest holds the answer. Keep the index in a second variable if the question asks where the value is. Starting from the first element works for every array that is not empty, including an array of negative values.
Why should the maximum of an array not start at 0?
Because 0 may not be in the array. If every value is negative, no element is greater than 0, the variable is never replaced, and the program prints 0, a value that does not exist in the data, without any error message. Starting from the first element of the array avoids this, since that start is always a possible answer.
How do you insert a value into a sorted array in ICS3U?
Start at the last cell in use and move left: while the cell holds a value greater than the new one, copy it one cell to the right. When the loop stops, the cell just after the stopping point is free: write the new value there and add one to the number of cells in use. Shifting from the left instead would copy one value over all the others.
How do you delete an element from the middle of an array?
Find its position first, with a linear search. Then, from that position up to the second last cell in use, copy each next cell one step to the left, and subtract one from the number of cells in use. Without that last step, the final value appears twice. If the search returned minus one, the value is not there, and nothing must be shifted.
What does a linear search return when the value is not found?
By convention, minus one, a number that cannot be a valid index of the array, so the program can tell found from not found. The search starts with the position at minus one, changes it only on a match, and the program checks for minus one after the loop, before using the position. In Python, minus one used as an index silently reads the last cell, so the check matters.