At the end of this lesson, you will be able to:
A1.1 use constants and variables, including integers, floating points, strings, and Boolean values, correctly in computer programs;
A1.3 use assignment statements correctly with both arithmetic and string expressions in computer programs;
A1.6 write programs that declare, initialize, modify, and access one-dimensional arrays.
B2.4 represent the structure and components of a program using industry-standard programming tools (e.g., structure chart, flow chart, UML [Unified Modeling Language], data flow diagram, pseudocode);
B4.1 describe the phases (i.e., problem definition, analysis, design, writing code, testing, implementation, maintenance), milestones (e.g., date of completion of program specification), and products (e.g., specification, flow chart, program, documentation, bug reports) of a software development life cycle;
understand what a variable is and how to use one
create an application that accepts input and saves it to a variable
understand what a top-down design is and how to create one
With a pencil and paper, answer the following questions:
What is "import math" used for in Python? What is its equivalent in C++?
Declare 2 variables, length with a value of 6cm and width with a value of 4cm.
Write the code that creates 2 variables, area and perimeter, and uses length and width to calculate the area and perimeter of a rectangle.
6-step process
modules in Python
import math
libraries in C++
#include <cmath>
from the online book Computer Based Problem Solving by Patrick Coxall, read:
what is a variable?
it is a container that stores a value that can change
in Python, we will use the following naming convention:
all lowercase with words separated by underscores
i.e. area_of_rectangle, time_of_day, number_of_students
what is an assignment statement?
used to assign a value to a variable
we always use a single "=" to do this!
for example:
score = 0 means score has the value 0
first_name = "Jolie" means the variable first_name is assigned "Jolie"
message_sent =true means that the variable message_sent is set to true
go over different data types in Python
#!/usr/bin/python3
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John Whyte" # A string
student_passed = true # A boolean
print (counter)
print (miles)
print (name)
print (student_passed)
to create a Top-Down Design in programming:
draw a box at the top with the name of the problem/program
underneath, draw 3 boxes, each with the names: Input, Processing, Output
connect each of the boxes with arrows pointing down
under the Input box
watch "Top-Down Design for Area & Perimeter of a Rectangle"
use draw.io with a Blank Template to create the top-down design
how do we get input from a user?
user input will ALWAYS be in the form of a "string" (a sequence of characters)
if you ask the user to enter a number, that number will first be in the form of a string
use type casting (or just casting) to convert the string to an int
type casting is used to convert one data type to another
for example, the following converts the string "5" to the integer 5:
score = int("5")
# just start using a variable right away in Python
length = int(input("Enter length of the rectangle (mm): "))
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
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
create the above program in C++
note the variable naming convention for C++:
lowercase to start, then uppercase, with words NOT separated to improve readability
for example:
float areaOfRectangle =94.78;
int numberOfStudents = 30;
std::string name = "Bob";
in C++ the user input does not necessarily have to be converted because it is not always in the form of a string like Python
// declare the variable first in C++
int length;
...
// then you can use it!
std::cout << "Enter length of the rectangle (mm): "<< std::endl;
std::cin >> length;