1. Introduction to C++

Review Topics

General Information

Housekeeping

  • Make sure you follow along with this page linked from Canvas
  • Please keep your microphone off unless you are asking a question
  • Please turn on camera if you can (optional)
  • Use chat if you would like to comment or ask questions

Announcements

See Announcements link in Canvas to keep up with what is going on. Here are a few to get started:

  • ZyBook required for course--email me if this is a financial hardship
  • Remember that the exercises from this page are due Sunday at 9:00pm.
  • Grace period--assignments due before 9/3 can completed be until 9/3 at 9:00pm (except Team quizzes)
  • Food & Housing Resources: free food, meals, temporary and permanent housing
  • COVID-19 Resources and Information: Includes loaner-laptop information
    • Make sure to get a laptop and not a Chromebook
    • We will install software later in the course
  • Campus WiFi Access: Aptos, Watsonville and other locations

Homework Help

1.1: Orientation

In this section we review basics of the course to amplify the Orientation module in Canvas and for those who may have questions.

1.1.1: Course Overview

  • The course is designed for learning basic computing concepts
  • Course organized into three parts, each followed by an exam:
    1. Basic skills: including variables, math, if-statements, and simple loops.
    2. Data and code organization: loops, lists and functions
    3. More advanced skills: objects, classes, files, and recursion as well as applying your skills to solve more complex problems.
  • Programs get more complex (and fun!) as we progress
Information + Practice

Information + Practice

Learning to Program

  • Programming is both concepts and skills.
  • To gain skills, we need both information and practice
  • Information + practice are like yin and yang
  • Information comes from instruction, reading, and preparing for exams
  • Practice comes from exercises, activities, and lab projects
  • Students will need to practice more in this class than most college courses

1.1.2: Required Materials

Computer and Internet

  • Computer with webcam, speakers and microphone
  • Good internet connection

If occasional Internet problems then we will work around the issue. Make sure to take screenshots to document situations.

Textbook

Title(s): ZyBook for CS-11 Introduction to Programming Concepts and Methodology, C++.
We will be doing extensive work in the book. Get your textbook either from the bookstore or directly from the publisher:

  1. Sign in or create an account at learn.zybooks.com
  2. Enter zyBook code: CABRILLOCS11ParrishFall2021
  3. Click "Subscribe" for School term

It is free to sign up and the first chapter can be read for free. For more information about the book see:

For technical issues email support@zybooks.com. If purchase is a financial hardship please email the instructor.

Canvas

Make sure to setup Canvas including:

  • Customizing your account: image, display name, etc.
  • Recommended notification settings:
    • Turn ON immediate notifications for Announcements, Grading, Submission Comments, and Conversation Messages.
    • Turn OFF immediate notifications for Discussion Posts.

1.1.3: Assignments

  • Before the first class meeting of a week students read the textbook and complete Participation Activities (PAs)
  • For PAs (and CAs), once an activity's completion has been recorded, that completion status never goes away
  • Thus you may freely re-attempt the PAs and CAs, which is a great way to refresh one's memory or study for exams
  • During class we have "review exercises" to improve our understanding of concepts
  • Students must turn in these exercises individually, even if the exercise is completed in a group
  • Outside of class students complete more challenging programs to improve their understanding and skills
  • Weekly labs may have many small programs or larger programs
  • Challenge Activities (CAs) due by end-of-week by Sunday 9:00pm.
  • Weekly lab projects due by next-week Tuesday 9:00pm
  • To receive credit for PAs, Cas and Labs, activate the assignment by clicking on the Load... button in Canvas
  • As you complete work, ZyBooks sends a percentage completed message to Canvas which then updates your score
  • Points in Zybook do not necessarily relect the points allocated in Canvas

Pair Programming

  • Starting in week 3 we complete labs using pair programming
  • With pair programming you work with one other student of your class for all lab projects of the week
  • Both partners must follow the simple Rules of Pair Programming
  • Both partners turn in the same lab code and receive the same grade

Supplemental Instruction

  • We offer supplemental instruction (SI) study sessions for this course
  • SI sessions are led by a student leader who has already taken the course material
  • Students meet with the SI leader to improve their understanding of course material, review and discuss important concepts, develop study strategies and prepare for exams
  • SI is open to all students enrolled in this class and I encourage everyone to attend
  • Attendance at SI sessions is free and voluntary
  • SI leader has the course materials for how to pass this class
  • Students who attend SI sessions weekly typically earn higher exam and course grades

1.1.4: Class Meetings

  • We hold class meetings online using ConferZoom
  • Attendance is expected with exams and quizzes given during meeting times
  • Students prepare for the week by reading the textbook and completing interactive activities in the textbook
  • Questions and discussion about the reading, PAs, and other computer topics are encouraged during class
  • Cooperative quizzes on the reading and PAs held at the start of the first class of the week--no makeups
  • During class we review problems and write programs to solve the problems
  • Students participate in the activities and work on the in-class review exercises during class
  • An adequate but not excessive amounts of time is allotted during class for exercises
  • If we run out of time during class for the exercises, students will need to complete them outside of class

Preparation

  • While reading the textbook (minimal reading), students complete Participation Activities (PAs)
  • After the reading and PAs, students may take an individual (solo) quiz
  • The individual quiz may be taken multiple times with the highest score kept for grading
  • You must complete the individual preparation before the first class meeting of the week with no late quizzes

Cooperative (Team) Quizzes

  • Cooperative quizzes taken as a team at the start of the first class of the week--no makeups
  • However, will have first quiz on Thursday this week because Tuesday is the first day of the semester
  • During the team quiz, discuss answers openly with your team
  • The team should strive to reach a consensus on quiz answers
  • Failing consensus, work it out the best you can
  • Each team member will receive the same score
  • The instructor will assign the members of each team

Appeals

  • After the quiz the team can appeal the answer
  • Appeals can be based on two criteria:
    • The question is factually wrong
    • The question is confusing based on it's wording.
  • Work with your teammates to develop and write your appeal(s)
  • All team members must aprticipate in the appeal to receive credit
  • See Canvas for more information

Problem Solving

  • Besides quizzes, we solve programming problems together
  • Problems are presented and discussed in class
  • Turn in problems to Canvas weekly for grading--usually due Sunday at 9:00pm

1.1.5: Using Zoom

  • Class meetings are in Canvas using ConferZoom
  • On the left menu is the ConferZoom link

    Zoom join link in Canvas

  • Clicking the link brings up a center panel
  • You will see a button that says Join

    Zoom Join button

  • Press the button to join the meeting

Audio

  • Before I arrive please mute your microphone
  • I have a mute all button I press if I hear dogs barking, dishes clattering, etc
  • Unmute when you want to talk

Answering Questions

  • I may call on you during class to answer in chat
  • You may answer by voice if preferred
  • However, I do not require you to turn on your camera or speak during regular class sessions

Toolbar

  • In Zoom you should see a toolbar like:

    Zoom toolbar with controls

  • May need to hover mouse at top of screen to see the toolbar
  • On the toolbar is a heads icon--click it to bring up all the people in the meeting
  • In that list you can rename yourself
  • Please use a name I can recognize
  • There is also a button on the list so you can raise you hand
  • So everyone raise your hand

Yes and No Buttons

  • There are Yes and No buttons as well
  • So lets take a survey: Who has been to the Mystery Spot in Santa Cruz?
  • If you've been then click Yes
  • If you've never been then click No
  • If I need your feedback during class, I can ask you to push the Yes or No buttons

Drawing on the Screen

  • At the top of your screen is a little bar that has View Options

    Zoom toolbar with controls

  • From the drop-down menu select Annotate
  • Annotate gives you a toolbar where you can draw things on the screen

Zoom annotation controls

  • Select the draw button and select items to draw

    Zoom drawing tools

  • Try this now!

Survey

  • Since I've got you here lets do another kind of survey
  • What type of computer do you use at home?

Operating system list

1.2: Developing Questions

Part of the reason to meet twice a week is to build community. Today we are going to start with a short exercise in a "breakout room".

Prework (3m)

  1. Take out a pen and paper or open a text editor on your computer.
  2. Pause and think about what you are starting with this course.
  3. Make a quick list of things you are uncertain about the course.
  4. Underline or mark (*) 1 or 2 of the uncertainties that stand out to you the most.

Preparing a question (2m)

Take your pre-work, select a stand-out idea, and write your question.

For example:

I want to know [something about the course].

Question Sharing

We will share our questions with small groups of people, which I will call teams.

  • Teams are a group of people with whom you share experiences and bond with over time, like a family.
  • During this Zoom session, our teams will remain constant
  • Teams encourage:
    • accountability
    • connection
    • working together

Breakout Rooms (10m)

  1. Read your question
  2. Team members listen, discuss the question and see if someone knows the answer
  3. If no one knows the answer then mark it for sharing with the class
  4. Repeat for all team members
  5. Return to large group for voluntary shares when time is up

No critiques and no ritual apologizing. You will be asked to go to a room so click on the button to go to the room. In your rooms, please turn your camera on and unmute yourself. If you are not following along in Canvas, take a picture of these instructions.

Voluntary Shares

For sharing, put a questions in the chat. Once you've written something go ahead and scroll back in the chat and see what people are saying. We will go through the questions and the instructor will answer them aloud as time permits.

1.3: Cooperative Quizzes

  • Quizzes assess the reading and participation activities due before the class meeting
  • Normally given on Tuesday, but on Thursday this week, since this is the first week.

Quiz Part 1: Individual Readiness Assessment

  • Complete this quiz solo to assess your reading comprehension and readiness
  • Must take this quiz before the class meeting to ensure you are ready for the team quiz
  • Quiz is open book and notes but timed
  • Highest score is counted so take the quiz multiple times

Quiz Part 2: Team Readiness Assessment (10m)

  • Must attend the class meeting to take this quiz
  • Login to Canvas
  • Will move to breakout rooms with your team
  • Make sure you have the access code for the exam
  • Submit the quiz individually during the first two weeks but work on it as a group
  • Openly discuss what you believe to be the best answers for the questions
  • Decide how to agree on the answers
    • Strive to reach a consensus on quiz answers
    • If no consensus, work it out as you and others in your group see fit
  • Turn in the quiz as a group
  • Each group member will receive the same score
  • Return to the main meeting room when finished

Quiz Appeals

  • After completing the group quiz, group members may appeal an answer
  • Appeals can be based on two criteria:
    1. Question is factually wrong

      Appeal must included citations to sources of information that document or support an alternative answer. Teams may access reference materials during the appeal.

    2. Question is confusing based on it's wording

      Appeal must include an appropriate rewrite of questions or answers that you interpret as ambiguous or confusing.

  • Work with teammates to develop and write any appeals
  • Team has up to 24 hours after the quiz to email appeal to instructor
  • If appeal is granted, only the teams that submitted appeal gets credit

1.4: Getting Started with C++

In this section we start programming in C++.

1.4.1: C++ Overview

  • Programming is about developing a list of instructions for a computer to follow in solving a problem
  • When we create a program, we first develop one or more algorithms
  • An algorithm is a sequence of precise instructions on how to accomplish a task
  • For example, to display "Hello World" to the screen we would say the algorithm step is
    output "Hello World"
    
  • We will be developing many algorithms during this course
  • Once we develop the algorithms, we convert them to C++ code
  • We type the C++ code into a text file known as a source code file

    source code: instructions for a computer written in a programming language

  • The following example shows the source code for the above algorithm
  • The program simply displays a message on our computer's screen
  • We will go through the meaning of each line of code later
  • For now, we are going to focus on how to turn this source code into a program that runs on our computer

1.4.2: Example C++ Source Code

Code for Hello World
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

Brief Explanation by Line Number

  1. Adds a library (pre-written code) to our program
  2. All the standard libraries use the std namespace
  3. A blank line we add to separate blocks of text
  4. the main() function where all C++ programs start
  5. The output statement (our algorithm)
  6. A statement to end the main() function
  7. The closing brace of the main() function

1.4.3: Compiling and Running Code

  • After creating source code file, we must convert the source code into machine instructions
  • The machine instructions are known as an executable program (or application)
  • We use a compiler program to convert source code to machine code
  • The process of using a compiler is known as compiling
  • As a compiler converts code, it may report errors

Compilation process

Which compiler?

  • Several different C++ compilers exist
  • Unfortunately, they all have minor differences and "extensions"
  • Ideally, we want a compiler that will work the same on all three major operating systems:
    • Linux/UNIX
    • Mac OS X
    • Windows
  • Also, we want a compiler that is free on all these systems
  • To meet these requirements, we will use a compiler named clang or g++
  • You must use one of these two compilers or you risk your code not compiling when I test your program
  • If you use a different compiler and your code does not compile using clang/g++, you will get a low score

To ensure the correct compiler I recommend one of the following options:

Compiling and Running Code with repl.it

  • To see how Rept.it works, open a browser tab in repl.it

    At some point in the course you will need to sign up for the full experience

    Sign up for the full experience

  • Notice the code window on the left side of the page:

    Replit code window

    This is where we type in our C++ code.

  • Also notice the console/terminal window on the right-side of the page:

    Replit terminal window

    This is where we we type program input and view program output.

  • The  > is known as the "prompt"
  • We may type commands in the terminal window after the prompt
  • To compile and run in repl.it we:
    1. Type or paste our code into the code window
    2. Press the  Run ▶  button
    3. View output and type program input into the terminal window
  • Later in the course we will use a more sophisticated compiling command that warns of more problems
  • Also, remember to resolve any warnings the compiler generates

Exercise 1.4a: Compiling and running a program (10m)

In this exercise we write compile and run a program together.

Specifications

Compile and run the following program using the repl.it option above together. Here is an example program we will compile and run:

#include <iostream>
using namespace std;

int main() {
    cout << "Enter a number: ";
    int num;
    cin >> num;
    cout << "Hello " << num << endl;
    return 0;
}
  1. Open a browser tab in repl.it
  2. Copy and paste the above code into the code window
  3. Press the  Run ▶  button
  4. Enter a number in the terminal window
  5. Verify the program outputs Hello followed by the number you entered, like:
    >  clang++-7 -pthread -std=c++17 -o main main.cpp
    >  ./main
    Enter a number: 42
    Hello 42
    > 
    

1.4.4: Saving Source Code

To save programs, we use a text editor. For convenience, do NOT use word processing programs like Word, LibreOffice, OpenOffice or Google Docs for code. If so, must save as Plain Text.

Some text editors are:

If you use TextEdit on the Mac, make sure to set it to be in plain text mode. Google textedit plain text

Directory tree on Windows

Where to save your code

  • After developing code we must save a source code file to our computer
  • We must save the file in a location that is easy to find and access
  • The easiest way is to create a folder on the Desktop
  • Within the folder, save all CS-11 files with a unique name
  • Most lab projects will specify a unique name for each file submitted to Canvas

Navigating Windows Folders

  • Everything in a computer system is stored in a file
  • Files are grouped together into computer folders, also called directories
  • Folders are organized in a hierarchical structure starting at the root folder
  • A path is the folders the computer must follow to find a particular file
  • To move around in a file system, we navigate a path through the folders
  • To see the folder structure in Windows:
    1. Right-click on the Start button
    2. Open Windows File Explorer
  • The left-hand side shows the folder structure
  • The Address bar shows the path

Directory tree on Mac

Navigating Mac Folders

  • To see the folder structure on the Mac open a Finder window, and click the icon for your hard drive
  • You should see at least four folders: Applications, Library, System, and Users
  • Within the Users folder, each user with an account on this Mac has a set of folders
  • Within your Users folder you will find your personal Home folder
  • Your Home folder is identified by your login name and a house icon
  • Within your Home folder you will find your Desktop folder
  • To better understand folders, display the path bar at the bottom of your windows by choosing

    View → Show Path Bar

Exercise 1.4b: Saving our code and output (10m)

In this exercise we save code to our computer.

For this exercise we break into teams based upon Linux, Mac (Apple) or Windows operating systems. Within the team, work with each other on problems encountered.

Choose your own team based on the following groups:

  1. Linux
  2. Mac (Apple)
  3. Mac (Apple)
  4. Windows
  5. Windows

Specifications

  1. Open a text editor on your computer (see list above) and think of a location to save your files.
  2. Copy the source code from repl.it into your computers clipboard:
    1. Click in the code window of repl.it.
    2. Highlight the code using the mouse or keyboard commands.
      (On Windows, use Ctrl-A; on Mac ⌘ Command-A)
    3. Copy the code to the clipboard.
      (On Windows, use Ctrl-C; on Mac ⌘ Command-C)
  3. Paste your code into your text editor:
    1. Click in the text editor's window.
    2. Paste the code to the text editor's window.
      (On Windows, use Ctrl-V; on Mac ⌘ Command-V)
  4. Using the text editor, save your source code as plain text in a file named hello.cpp.
  5. Run your program and then copy the terminal output into a text editor:
    1. Click in the terminal window of repl.it.
    2. Highlight the output using the mouse (keyboard does not work).
    3. Copy the output to the clipboard.
      (On Windows, use Ctrl-C; on Mac ⌘ Command-C)
  6. Save the terminal output in a file named output4b.txt.
    1. Click in the text editor's window.
    2. Paste the output to the text editor's window.
      (On Windows, use Ctrl-V; on Mac ⌘ Command-V)
    3. Using the text editor, save your terminal output data as plain text in a file named output4b.txt.
  7. Remember where you saved your files so you can upload them to Canvas.

    Turn in both hello.cpp and output4b.txt to Canvas after all exercises for the week are completed as described in Deliverables.

1.5: Problem Solving

  • Creating programs is like solving puzzles: they take some work to figure out
  • Programming is largely about problem solving: creating a methodical solution to a given task.

Computer parts: input, storage, processor, output
Image source: code.org

1.5.1: Parts of a Computer Program

  • Remember that all computers have four parts
    1. Input
    2. Processing
    3. Storage
    4. Output
  • Similarly, a computer program has at least three parts
    1. Input and store data
    2. Process and store data
    3. Output information
  • Each part may be repeated many times
  • Most programs we write have all these parts

1.5.2: Four Steps to Solving a Problem

  • A computer program is an algorithm implemented in computer code

    Algorithm: A sequence of precise instructions leading to a solution

  • To write a program we must develop a computer algorithm
  • One approach is based on Polya's Four Step Problem Solving Process and has four parts
    1. Understand the problem
    2. Create a plan and check it
    3. Translate to code and perfect the plan
    4. Test and debug the code
  • The end result is a computer program that solves a problem

1.5.3: Check Yourself

  1. A sequence of precise instructions to accomplish a task is called a(n) ________.
  2. Most all computer programs include steps to ________.
    1. input data
    2. store data
    3. process data
    4. output information
    5. do all of these
  3. The first step to solve a problem with a computer program is to ________.
    1. create a plan and check it
    2. test and debug the code
    3. translate to code and perfect the plan
    4. understand the problem

Exercise 1.5: Read and display two inputs (10m)

In this exercise we solve a problem asking the user for two inputs for a program and then displaying formatted information.

For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.

  1. Start Replit
  2. To the supplied starter code, add the following after line 1:
    using namespace std;
    
  3. Inside main(), declare two int variables: birthMonth and birthYear.
  4. Write two cin statements to get input values into birthMonth and birthYear.
  5. Write a statement to output the month, a dash, and the year; end with newline.
  6. Run the program to verify correct output. For example, if the user enters 1 2000 the output is:
    1-2000
    
  7. Copy the file into a text editor and save the file as "input2.cpp".
  8. Submit your input2.cpp file to Canvas as described in Deliverables below.

Deliverables

For this week's exercises we are turning in three files:

  • hello.cpp (from exercise 1.4b)
  • output4b.txt (from exercise 1.4b)
  • input2.cpp (from exercise 1.5)

Remember that when submitting multiple files to Canvas, you must submit all files in the same session. For instuction on submitting multiple files for an assignment see the Canvas guide on How do I upload a file as an assignment submission in Canvas? section: Submit Assignment. Submit code files and not photos for this assignment.

Last Updated: September 01 2021 @00:53:51