CS 134: Programming Fundamentals, Summer 2022
Assignment turn-in link: CANVAS TURN IN
Exercises are meant to be solo effort, please try to complete it on your own. You can ask the instructor or a tutor questions if you get really stuck, but all the information you need to complete the assignment is in here, or in the related reading/lectures.
If you get stuck, please post questions in the Unit 1 'Help!' discussion board.
For this assignment you will need to write a text document in something like MS Word or LibreOffice Writer (free)!
Make sure to EXPORT your document to PDF when you're ready to upload your work.
MS Word - How to save a Microsoft Word document as a PDF file
LibreOffice - How to save docs as PDF
Google Docs - How to save docs as PDF
You will submit your document to the [CS134.U01.EX] 🏋️ Program design basics – Exercise on Canvas.
Afterwards, you will be sharing one of your flowcharts on the [CS134.U01.PR] 🔎 Program design basics – Peer Review discussion board.
Quick jump: Part 1: You are the computer | Part 2: Flowcharts
⭐
First off we're going to step through a series of "code" as if we were the computer to see how a computer works through code line-by-line.
One of the fundamental commands of programs is the ability to OUTPUT information
to the screen. Different languages use different names for OUTPUT, but Python
uses print
(even though we're not working with a printer.)
Each print
statement will display text on its own line, from the top
of the screen to the bottom.
Example Python program
print( "Welcome to CS134!" )
print( "Hopefully" )
print( "these 8 weeks" )
print( "will be fun!" )
Stepping through the code
Trace through the code of the following. Log the Program Execution and the Program Output.
print( "Hello, my name is _____!" )
print( "I am taking CS134 at JCCC!" )
print( "My teacher is Rachel Wil Singh!" )
Python can use the input
command to get input from the user.
When we get input we need to store it in a variable somewhere
so we use the assignment operator =
to
assign the result from input
into a variable.
We can also assign values to our variables directly,
also using the assignment operator =
. The variable name
always goes on the LEFT-HAND SIDE of the =
and the
value it receives always goes on the RIGHT-HAND SIDE.
We can use the print
command to display
text in double quotes " " (known as a string literal),
or we can give it a variable to display the value of.
We can also combine these with the concatenation operator +
.
Since this program uses input
to get entries from the user,
we can come up with example inputs as we are stepping through the program.
Example Python program
print( "ADDRESS PROGRAM" )
schoolName = "JCCC"
print( "School: " + schoolName )
# Get inputs
streetAddress = input( "What is the street address of the school? " )
city = input( "What is the city the school is in? " )
state = input( "What is the state the school is in? " )
zip = input( "What is the zip code the school is in? " )
# Display formatted address
print( schoolName )
print( streetAddress )
print( city + ", " + state + " " + zip )
Stepping through the code
Trace through the code of the following. Log the Program Execution and the Program Output.
print( "PETS PROGRAM" )
print( "Let me introduce you to my pets!" )
pet1 = "Kabe"
pet2 = "Luna"
pet3 = "Pixel"
pet4 = "Korra"
print( pet1 + " is my oldest cat" )
print( pet2 + " is my most mischevious cat" )
print( pet3 + " is my most precious cat" )
print( pet4 + " is my youngest cat" )
yourPet = input( "What's the name of one of your pets? " )
print( "Pleased to meet you, " + yourPet + "!" )
We can also do basic arithmetic in our programs using the
addition operator +
, the subtraction operator -
,
the multiplication operator *
, and the division operator /
.
However, to use math operations on variables, we need to make sure they are either
int types (whole numbers) or float types first.
By default, any input from the user is a string, so we do conversions with
the int()
and float()
functions.
If we have an int or a float but want to display it to the screen, then we
need to turn the number back into a string with the str()
function.
Example Python program
print( "CALCULATOR" )
number1 = float( input( "Enter a number: " ) )
number2 = float( Input( "Enter another number: " ) )
product = number1 * number2
print( "The result of " + str( number1 ) + "*" + str( number2 ) )
print( "is " + str( product ) )
Stepping through the code
Trace through the code of the following. Log the Program Execution and the Program Output.
print( "RECTANGLES" )
length = float( input( "Enter the length of the rectangle: " ) )
width = float( input( "Enter the width of the rectangle: " ) )
area = length * width
print( "The area of the rectangle is " + str( area ) )
⭐
Before we jump into coding something it can be useful to give ourselves a roadmap. Flowcharts are a way for us to graphically represent the steps of our program and the program flow.
The different shapes we can use in a flowchart are:
Circle: Terminal
The circle marks the beginning or ending of a sequence, usually the start and end of a program.
Rectangle: Process step
Program processing, such as doing math operations or assigning values to variables, are written in rectangles. This data is not displayed to the user, it is an internal step.
Diamond: Decision
The diamond is for making decisions, which we have not yet talked about, code-wise. We can ask a "yes/no" question and then take one of two paths based on whether the result is "yes" or "no" (often thought of as "true" or "false").
Parallelogram: Input/output
The parallelogram is used to represent any time the user enters data (input) or any time we display something to the screen (output), such as with a print statement.
Arrow line: Program flow
Steps of a program are connected together with arrow lines. This shows which way the program flow continues on.
Using flowcharts can be useful for planning out steps of a program before you begin coding.
Flowcharts can be drawn using tools like:
Or you can draw it on paper, take a picture, and send it to your computer to embed into your document.
Create a flowchart for a program that does the following:
Stepping through the code
Create a flowchart for a program that does the following:
When you're done creating a flowchart you should use the EXPORT command to save it as an image file (e.g., a PNG file) and then put it in your text document.
The diamond "decision" shape is an important shape. Programs wouldn't be all that useful if we couldn't make decisions and execute certain steps given certain criteria. We use the diamond shape to create "yes/no" questions, and go down one path if the answer is "yes", or a different path if the answer is "no".
Create a flowchart for a program that does the following:
Stepping through the code
Create a flowchart for a program that does the following:
When you're done creating a flowchart you should use the EXPORT command to save it as an image file (e.g., a PNG file) and then put it in your text document.