At the end of this lesson, you will be able to:
A1.5 describe the structure of one-dimensional arrays and related concepts, including elements, indexes, and bounds;
A1.6 write programs that declare, initialize, modify, and access one-dimensional arrays.
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 create an array (or list) in Python
What is the difference between pass by reference and pass by value?
True or False: In pass by value, it is possible to change the original value that was passed to the function.
Write a function in C++ called IncByFive() that takes an integer called anInt as a parameter passed by reference and increments it by 5.
Write the code in C++ that declares an integer called num with the value of 7. It then calls the function above, passing to it num.
Write a function in Python called change_by() that takes a list of integers called some_number and an integer called num_to_add. It increments the first element of some_number by num_to_add.
Write the code in Python that declares an empty list called a_number and append to it the value -11. Then declare a variable called by_amount that has the value -4. Then call the above function with these two arguments, passing a_number by reference.
pass by reference
how to generate a random number in Python:
import random
random_variable = random.randint(0, 100) # a number between 0 and 100
from the online book Computer Based Problem Solving by Patrick Coxall, read:
Arrays:
a collection of items of the SAME TYPE
index always starts at 0
we often use a loop to populate an array
Python actually does not have arrays. It has LISTS!
We will assume arrays are the same as lists for now.
Create a program called “Number Generator” that generates 10 random numbers between 1 and 100.
You will need the following constants:
MAX_ARRAY_SIZE = 10
MIN_NUM = 0
MAX_NUM = 100
The program places the numbers into a single variable (a list in Python or an array in C++) and prints them to the console.
After the array is full, your program calculates the average of all the numbers and displays it.
You MUST use a loop to calculate the average. Do NOT use a built-in function.
Remember the formula: average = sum of all numbers / number of numbers
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 use a calculator to add the numbers and calculate the average. Show your work for each test case to make sure the program results are correct.
Don’t worry about using a function to calculate the average. Just do everything in main().
Note:
To declare a list of integers in Python, use:
list_of_int = []
To declare an array of integers in C++, use:
int arrayOfInt[MAX_ARRAY_SIZE];
Note:
Every array will know its current length!
To find the number of items in a list in Python, use the len() built-in function:
list_length = len(list_of_int)
To find the length of an array of C++, use the sizeof() built-in function:
int arrSize = sizeof(arr)/sizeof(arr[0]);
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++
#include <iostream>
#include <random>
int main() {
int someRandomNumber;
std::random_device rseed;
std::mt19937 rgen(rseed()); // mersenne_twister
std::uniform_int_distribution<int> idist(0,100); // [0,100]
someRandomNumber = idist(rgen);
std::cout << someRandomNumber << std::endl;
}