At the end of this lesson, you will be able to:
A2.3 write algorithms with nested structures (e.g., to count elements in an array, calculate a total, find highest or lowest value, or perform a linear search).
B3.1 design simple algorithms (e.g., add data to a sorted array, delete a datum from the middle of an array) according to specifications;
understand and use arrays (or lists in Python) with a For..Each loop
Write the code in Python to do the following:
declare an empty list of integers called list_of_int
populate the list with 10 random integers between 1 and 50
Create a function in Python that will accept a list of integers called some_list_of_int as an argument and return the MIN value in the list. This function should work for ANY size of list.
Note: Use the len() built-in function to find the number of elements in the list. For example:
size_of_list = len(some_list_of_int)
passing an array as a parameter
from the online book Computer Based Problem Solving by Patrick Coxall, read:
Create a program called “Min Value” that generates 10 random numbers between 0 and 100 and uses a function that has a For..In loop to find and return the min value.
The program places the numbers into a single variable (a list in Python or an array in C++) and prints them to the console.
You will need the following constants:
MAX_ARRAY_SIZE = 10
MIN_NUM = 0
MAX_NUM = 100
To declare an array in C++, use the following:
#include <array>
std::array<int, MAX_ARRAY_SIZE> arrayOfInt;
We will use the following to generate a random number in C++:
#include <ctime>
srand(time(NULL));
randomNumber = (rand() % RANGE+1) + MIN_VALUE;
The range of numbers would be from MIN_VALUE to MIN_VALUE+RANGE
The program has a function called “find_min_value()” in Python and “findMinValue()” in C++.
It accepts an array as a parameter.
The function MUST use a For..In loop to find the min value. Do NOT use a built-in function.
It returns a single variable - the min value in the array (or list).
The min value will then get displayed in main().
Do NOT use a hard-coded number (i.e. 10). Use the built-in length / size() functions.
In Python, use:
len(list_of_int)
In C++, use:
#include <array>
arrayOfInt.size()
Since your data is random, it is not possible to create test cases in advance.
Instead, insert 3 test cases of your program output. Then visually check to make sure your output is correct.
Note: The top-down design should be for the entire program. There should be a flowchart for each function.
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
Flow Chart
Pseudocode
Test Cases
complete the Daily Assignment section in Hãpara Workspace for this day
if Hãpara is not working, make a copy of this document
move it to your IMH-ICS folder for this course
recreate the same program in C++