At the end of this lesson, you will be able to:
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;
pass an array (or list in Python) as a parameter to a function
What is an array?
State the characteristics of an array.
True or False: There are no arrays in Python.
Write the line of code to do the following:
a) Declare an empty list called "list_of_int" in Python.
b) Declare an array of 5 integers called "arrayOfInt" in C++. Make sure to declare the size as a constant.
Write the line of code to do the following:
a) Append the value -4 to "list_of_int" in Python.
b) Insert the value 7 into the "arrayOfInt" at index 2 in C++.
Write the line of code to do the following:
a) In Python: Initialize a variable called sum. Then use a loop to add up all the numbers in "list_of_int". Note: Use the built-in len() function to find the number of elements in the list.
b) In C++: Initialize a variable called sum as an integer. Then use a loop to add up all the numbers in "arrayOfInt".
arrays
from the online book Computer Based Problem Solving by Patrick Coxall, read:
when passing an array to a function, the entire array is copied to the function parameter UNLESS it is passed by reference.
Create a program called “Max Value” that generates 10 random numbers between 0 and 100 and uses a function to find and return the max 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
This time, to declare an array in C++, use the following:
#include <array>
std::array<int, MAX_ARRAY_SIZE> arrayOfInt;
to generate a random number in C++:
#include <time.h>
srand(time(NULL));
int 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_max_value()” in Python and “findMaxValue()” in C++.
It accepts an array as a parameter.
The function MUST use a loop to find the max value. Do NOT use a built-in function.
It returns a single variable - the max value in the array (or list).
The max value will then get displayed in main().
Do NOT use a hard-coded number (i.e. 10). Use the built-in length / sizeof 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++