1: Introduction to Programming

What We Will Cover


  • Parking
    • Warning tickets in student lots first week
    • Staff lots will be ticketed
  • STEM Success Coaching service--your personal coach to help you succeed!

1.1: Introduction to Programming Concepts and Methodology, C++

Learner Outcomes

At the end of the lesson the student will be able to:

  • Set your goals for the course
  • View examples of what you will learn
  • Decide on interest in the course content

1.1.1: Why Study Computing and Computer Science?

  • Congratulations! Click to show message
  • You are making a smart decision to take a course in computer science
  • Here are some good reasons why:


Computing Enhances Your Creativity


Computing Empowers You to do Good


Computing Opens Doors

  • There are about two billion personal computers in use (source: Quora, Worldometers)
  • In addition, there are many billions more computers embedded in everything from cell phones to car engines, iPods and video games
  • New types of computers continue being developed such as DNA computers and quantum computers
  • Improvements in computing such as artificial intelligence are now considered requirements for business (source: Forbes)
  • Because the number of computers and computer technologies are growing so fast, few careers as rewarding as computing
  • Computer science pays well with average starting salaries of $81292 for new grads in 2019 vs. $55,280 for the average among College Graduates (source: NACE Fall 2019)
  • Computers are used in every industry, making computing an important skill in any career
  • Projected new jobs for computer occupations are much more than any other STEM field (Chart source BLS p.11)
  • Why merely use WhatsApp when you can create the next WhatsApp?
  • What majors are students in this class pursuing?

Coding = Your New Superpower

More Information

1.1.2: What is Computer Science and Programming?

Computer Science: the study of the principles, experimentation, design and engineering of computers.

Computer: a device for storing and processing data automatically.

  • Sometimes computer science is equated to using, or even programming, computers
  • However, computer science is no more about using computers than astronomy is about using telescopes or biology is about using microscopes
  • Science is not about tools, but how we use them and what we find out when we use the tools
  • Computer science studies how to efficiently organize, store, retrieve and process data
  • Like biologists use microscopes, we use computers and programs to develop our understanding of computing
  • Computer science studies mechanized procedures, or algorithms, among other things
  • An algorithm is a sequence of precise instructions on how to accomplish a task
  • We use computers to implement and study our algorithms using programs or coded instructions

Phonebook Example

  • As an example of retrieving data, consider how to find a number in a phone book for a given name
  • One way is to start at the beginning and look at each name until we find the one we want
    • Usually takes a long time
  • However, the data in a phone book has a special property -- it is organized alphabetically
  • We can use this property to quickly find the page we want
  • How would we describe this process?
  • We start in the middle of the book, like with the letter M
  • Noting that P (for Parrish) comes after M, we disregard all the pages before M
    • This reduces the remaining number of pages by half
  • We repeat this process for the remaining pages, discarding half the pages with each step
  • Quite soon, we find the page we need
  • If the phone book is 1000 pages, then we might need 10 steps at most
  • What if the problem were expanded so we had 4 billion pages (about what a web search engine looks at), how many steps would we need?
  • Not as many as you may think -- only about 32 steps
  • To make fast searches, we have to organize the data and come up with an efficient algorithm


Counting People

  • Suppose we want to know how many people are in a room
  • We could count each person one at a time
  • If there are n people, we need to make the count n times
  • How can we reduce the number of steps by half? (answer)
  • Here is an even better algorithm:
    1. Stand up and say to yourself, "I am #1"
    2. Pair off with someone standing, add your number to theirs and take the sum as your new number
    3. One of you sit down and the other goes to step 2
  • Theoretically, this only takes log₂(n + 1) steps, a much faster approach
  • Let us try this approach and see how it works, counting the steps as we go

More Information

1.1.3: Examples of What You Will Learn

  • As we study computing, we will solve some interesting problems
  • Let us take a look at some examples of what you will learn in this course
  • Samples from prior homework assignments:
    • carbot: a program that conducts a conversation
    • drought: analysis of current rainfall to detrmine drought conditions
    • printsquares: Draw shapes with ASCII text
    • wisdom: computer generated "wise" sayings
    • store: Develop a virtual business
    • realfake: determine if news stories (like rocketman.txt) are real or fake
        realfake < rocketman.txt  
  • This semester we will develop software programs like these

Student Projects

  • Toward the end of the course you will complete a project
  • The purpose of the project is to utilize what you learned during the course to produce your own unique program
  • In previous semesters students have completed many fascinating projects including:
    • 3D shape displays
    • Address books
    • Adventure games
    • Ant farm simulation
    • Astronomy calculations and simulations
    • Bank account programs
    • Battleship games (text and graphical versions)
    • Dart games
    • Database programs (store and retrieve selected data)
    • Card games
    • Digital photo manipulation (like Photoshop)
    • Fortune tellers
    • Enhanced Mad Libs game programs
    • Lithophane production on CNC mills from images
    • Mine sweeper
    • Pac-Man
    • Pong
    • Spectrographic analysis
  • You too will be able to create programs like these
  • For what would you like to develop a program?

1.1.4: Programming languages and C++

  • There are many programming languages for computers
  • In this course we learn how to program using C++
  • C++ is one of the most popular programming languages
  • It is used in many problem domains including Engineering and game programming
  • Examples of programs written in C++ include:
    • Adobe Acrobat, Photoshop and all major applications
    • Microsoft Windows
    • Chrome and Mozilla Firefox browsers
    • More: see C++ Applications

C++ and Compilers

  • C++ is a compiled language
  • We must use a program known as a compiler to produce C++ programs
  • A compiler is a program that translates C++ (or other programming language) into the language of the computer (machine code)
  • There are hundreds or thousands of different C++ compilers
  • Unfortunately, they are not interchangeable and do not all work the same
  • To solve this problem, we will use only one compiler for this course: g++
  • The g++ compiler is a standard-conformant compiler that runs on Apple, Linux and MS Windows computers
  • We will discuss compilers in more depth next week
  • However, you can get set up now and it is best if you start early
  • For Windows, you need to install Cygwin, which contains the g++ compiler
    • Cygwin is the environment available in the CTC, STEM Center and our classroom
    • Follow my instructions on How To Install Cygwin for best results
  • For Apple Mac users, Apple's tools include a g++ compatible compiler named Clang
  • For Linux/UNIX users, you can install GCC and g++ compilers from your distribution update
  • If you do not have access to a computer, use one in the Computer Technology Center (CTC) or STEM Center (Google Map)

Check Yourself

  1. True or false: C++ is a popular and widely used programming language.
  2. Of the following names, the compiler is ________.
    1. Cygwin
    2. g++
    3. TextPad
    4. Word
  3. True or false: you can use any compiler you want for this course.

More Information

Exercise 1.1: Introductions and Questions (5m)

Use the next 10 minutes to complete the following.

  1. Join a group of 3-4 people and introduce yourself to each other.
  2. As a group, develop one "burning question" about the course.
  3. Write the single question on the board.
  4. Select one member from your group as a spokesperson to describe your single question to the class.

Note: You do not have to turn in the answers to this exercise. However, you are expected to submit answers to subsequent exercises.

1.2: Exploring the Course

Learner Outcomes

At the end of the lesson the student will be able to:

  • Navigate the course web site
  • Discuss how the course operates
  • Know when assignments are due

1.2.1: Course Design

  • The course is designed for learning basic computing concepts
  • It is organized into three parts, each followed by an exam:
    1. Basic skills: including variables, if-statements, loops and algorithms.
    2. Organizing code: for larger programs using functions, objects, classes and lists.
    3. Advanced skills: learning more about objects, lists and files, as well as applying your skills to solve more complex problems.

Class Meetings

  • Quizzes on the last weeks material are given most weeks before class
  • New concepts are presented each class meeting followed by activities to engage in the concepts
  • Discussion is encouraged as is outside material relevant to topics being covered
  • Students are expected to participate in the activities and work on the in-class "lesson exercises" during the allotted time in class
  • An adequate but not excessive amounts of time is allotted for the activities
  • If we run out of time during class for the activities, students will need to complete them outside of class


  • Programming is not just an intellectual activity
  • Writing programs is necessary to master programming skills
  • Each week we cover new concepts during class that we then develop into programs
  • Before class meetings students are expected to prepare for class by:
    • Reading the textbook and/or lecture notes
    • Completing an online quiz
  • During class we have "lesson exercises" to engage in and try out the new concepts presented
  • Students must turn in these exercises individually, even if the exercise is completed in a group
  • Outside of class students will be given more challenging projects to improve their understanding and skills
  • In addition, students will be given "drill problems" to practice their skills
  • All parts of the assignment (preparation, lesson exercises, projects and drill labs) are turned in each week for grading
  • Thus assignments have many parts as we can see in the first assignment

Assignment Grading

  • The instructor uses a computer (robot) to compile and run programs
  • In addition, the robot scans student files for accomplishments and problems
  • Final grading is performed by the instructor


  • Your final grade is weighted based on your performance in the following areas:
    Assignments (projects, exercises, labs) 30%
    Midterm exam 1 15%
    Midterm exam 2 20%
    Final exam 35%
  • Late assignments are not accepted except for a single one-time exception of up to two days late
  • Most assignments have some extra-credit opportunities
  • Quizzes on lesson concepts and homework are given most weeks in Canvas
  • Exams must be taken when given except by prior arrangement for an extremely compelling reason
Information + Practice

Information + Practice

Information + Practice = Expertise

  • Information + practice are like yin and yang
  • Information comes from instruction, reading, preparing for tests
  • Practice comes from exercises, labs, projects
  • Competence and mastery only comes from both information and practice

More Information

Even Einstein Had to Try

Even Einstein had to work to learn!

1.2.2: Succeeding in the Course

  • Learning to program is not easy, but all of us can succeed
  • No prior programming experience needed for this course
  • You must take an active part in the learning process
  • As an active learner, take responsibility for your learning
  • As the instructor, I present ideas and examples
  • As the learner, you work with the ideas to truly learn
  • Rote learning and memorization is not enough
  • You must learn to solve problems and work creatively with code to pass the course

Why College Students Fail

  • Lack of preparedness: mentally, emotionally and academically
  • Poor attendance: miss important information needed for assignments and tests
  • Poor study habits: college requires much more time than high school; 2 hours outside class for every unit
  • Faulty time management: little planning, not enough time spent studying, starting homework late

Optimizing Your Learning

  1. Go to class and participate [1][2].
    1. Silence and put away your cell phones
    2. Pay attention to the lecture, ask questions, and participate in the activities
    3. Get to know other students during activities and breaks
    4. Look forward to learning!
  2. Use your computer only during specified times and for specified tasks
    1. Students who take notes on a computer do worse on exams than those who take notes on paper [3][4][5][6]
    2. Students who multitask during a lecture score lower on exams than those who do not multitask [7]
    3. Students around the multitasker do worse on exams because they become distracted [7]
  3. Quiz yourself often on the course material [8][9]
    1. Learning research shows that practice tests are the best way to learn factual information
    2. Spreading study over time is more effective than cramming
    3. Review the Check Yourself questions from the lecture notes within a day of class
    4. Practice recalling the important material using flash cards (physical or digital)
    5. Answer the sample questions at the end of the textbook section or chapter
  4. Always do your own homework - and start it as early as possible!
    1. College is not like High School: time in class is small compared to time spent on assignments and studying
    2. Expect this class to be hard and the work time consuming--plan accordingly!
    3. Remember Einstein's quote, "Genius is 1% talent and 99% hard work."
    4. Hard and smart work is what matters, not native "intelligence"
    5. Start homework early and code every day
  5. When you are stuck, get help!
    1. Programming is frustrating at times--even professional programmers struggle at times
    2. If you find you cannot figure out a problem:
      • Take a break and come back to the problem later
      • Talk to the professor, SI leader, a tutor or a classmate for ideas
    3. With effort and tenacity you can learn and succeed! [11][12]

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 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
  • Students who attend SI sessions weekly typically earn higher exam and course grades
  • Scheduled SI time is: MW 3:30-5 pm in room 828
  • How many students can attend at this time?
  • What other times work?

More Information

  1. Seven Keys for Academic SUCCESS: suggestions from Cabrillo faculty
  2. Meta-Analysis Summary: Learning Styles: for the most part, we all learn the same way
  3. Laptops hinder classroom learning for both users and nearby peers
  4. Ink on Paper: Some Notes on Note Taking: writing notes is more effective than typing notes
  5. The Pen Is Mightier Than the Keyboard: Advantages of Longhand Over Laptop Note Taking
  6. The impact of computer usage on academic performance: Evidence from a randomized trial at the United States Military Academy
  7. Think You're Multitasking? Think Again
  8. Three Things Top Performing Students Know That Their Peers Miss
  9. What Works, What Doesn't: meta-review of study techniques
  10. Learning Styles: Concepts and Evidence: comprehensive research showing no evidence
  11. The Effort Effect: you'll reach new heights if you learn to embrace the occasional tumble
  12. The power of believing that you can improve: TED talk by Carol Dweck
  13. What Causes Students to Fail Courses in College?

1.2.3: Useful Information and Course Resources

  • I provide you with a lot of information and resources for you to succeed in the course
  • Here are some of the highlights

Course Website

  • www.edparrish.net has a link to the course web site and has contact information for the instructor
  • Home Page describes the online content of the course
  • Syllabus describes the policies of the course including prerequisites and grading
  • Canvas is where you turn in assignments, take tests and can look at your grades and written feedback on assignments
  • Schedule page has links to lecture notes, reading and assignments
    • Read the assigned reading BEFORE class
    • Assignments are due before class
  • Dates and times listed in Canvas takes precedence over those on the schedule page
  • Late homework is not accepted, except for a one-time exemption of up to two days
  • See Syllabus for more information on assignment grading policies
  • For add code instructions see: Add Instructions for Students

Classroom Computers

  • Logins for Canvas, Hawknet and most computers on Campus are the same as your WebAdvisor login
  • However, logins for this classroom are different than for other computers
  • We login with a generic username and password
    • Username: student
    • Password: Cabri11o (a.k.a. funny Cabrillo)
  • You lose all contents of C:\ when you log out
  • The contents of D:\ are preserved
    • The D:\ drive is shared by everyone
    • Don't delete other people's stuff
    • Don't leave anything there that's personal or precious!

Electronic Communication

  • Please make sure your email address in WebAdvisor is one you actually use
  • Check your email at least once per day or you will miss important course information
  • When sending me an email please include your name and course number
    • I really do not know who you are from an email address like: fandango246@hotmail.com
  • Set up Canvas notifications so you get Announcements, Grading and Discussion Post notifications via email or text (SMS)

Online Lab Hours

  • The schedule of classes lists, "3 hr 5 min online lab per week".
  • These hours are in addition to the usual 2 hours preparation per unit (see Orientation FAQs "homework")
  • You are graded, in part, on completing the online labs each week
  • These online lab hours are there to complete the lab exercises in CodeLab
  • You must sign up for and later purchase a CodeLab subscription
  • CodeLab gives you practice coding with immediate feedback
  • Use your own computer or one in the Computer Technology Center (CTC) or STEM Center (Map)
  • If you do not have your own computer, please see me for options

Grading Options

  • By default, everyone gets a letter grade
  • If you want pass/no-pass option, Cabrillo policy is to fill out an eForm: AR - Pass/No Pass Request
  • With pass/no-pass, you must complete the course work at a satisfactory ("C") or better level to pass
  • Once you choose pass/no-pass and submit the form, the choice is not reversible
  • The last day to request pass/no-pass is shown on the A&R Dates and Deadlines

Academic Honesty

  • Do not cheat, lie or help others to cheat
  • Its OK to get help and to help others on homework, within limits
  • However, do not share code with others or copy code from others without attribution
  • Copying any amount of code without attribution is plagiarism, a form of cheating
  • When helping someone, do not give them the answer but show them how to find or figure out the answer
  • When you help someone, do not touch their keyboard as this immediately stops their learning
  • Similarly, do not give them a copy of your code
  • When someone helps you, do not let them touch your keyboard or send you code
  • See the Scholastic Honesty Policy in Syllabus for more information
  • Two or more pieces of code that are too similar will both receive a 0 score

Cell Phones and Texting

  • Cell phones are wonderful devices but are disruptive in the classroom
  • Texting and other cell phone use are prohibited during class

Classroom Environment

  • I ensure a safe learning environment for everybody
  • I will treat you with courtesy and respect
  • I expect the same from you
  • No sarcasm, put downs, intolerance, or demeaning remarks
  • Listen while others are speaking
  • No disruptive behaviors
  • See Student Behavior Expectations in the Syllabus for more information
  • If you need help ask; if you already know, help other people to learn.
  • It's okay to be wrong -- but you need the courage to find out

Other Useful Links

  • Site Search at the top of most pages to help find which page has the information you need
  • Class Expectations describes what you can expect from me and what I expect from you during the course, including when you are late to class
  • Classroom Policies a few rules, including those for food and drink, to make our learning more comfortable and fun
  • College Regulations: a few reminders to keep you out of trouble ;)
  • CodeLab Registration and Purchasing
  • Help: check out this page when stuck on an assignment, feeling stressed, or in need of assistance

1.2.4: Using a Text Editor

  • In the following exercise we need to use a text editor
  • A text editor is a basic tool used by programmers like a word processor is used by writers
  • Programmers use a text editor, rather than a word processor, because compilers cannot compile word processor files
  • Some of the text editors we use are:
  • Make sure to use a text editor in plain text mode
  • Since we need to learn how to use programming tools, we will use text editors in this course
  • Do NOT use word processing programs like Word, LibreOffice or OpenOffice for assignments
  • To get us started, we will work together to use a text editor for the next exercise

About File Extensions

  • A filename extension is the characters after the period in a file name, like:
  • Note that the file extension for text files must be txt, like:
  • Unfortunately, many computers hide the file extension by default
  • To see file extensions on your computer:

Exercise 1.2: Treasure Hunt! (15m)

In this exercise we explore the course web site to see how many of our questions we can answer. We will discuss the questions and answers you submit on Thursday.


  1. Prepare a text file (NOT a Word document) named: questions.txt
  2. Copy the following Exercise Questions into the text file and then record the answer after each question.
  3. Find the answers to the following Exercise Questions.

    Hint: not everything is on this page. You will need to follow links to other pages of the course web site. See lesson 1.2.3: Useful Information and Course Resources for a list of possible links.

  4. Submit your questions.txt file to Canvas in the assignment named Treasure Hunt before class on Thursday. Do NOT email the answers to the instructor.

    If using a Word Processing program like Google Docs (not recommended), make sure to save the file as plain text and with the correct file name and file extension.

Exercise Questions

  1. Why do we use a text editor rather than a word processor for this exercise?
  2. What do you do if you are late to class?
  3. What is the Web address (URL) where the instructor's contact information is located?
  4. How do you get to the instructor's office from the classroom?
  5. What is CodeLab and why must you spend $25 to purchase it?
  6. What compiler must you use for this course?
  7. What are three free support services offered to students (list them)?
  8. How many hours per week can you expect to spend on this course outside of the classroom?
  9. What is scholastic honesty?
  10. What is an example of acceptable help and an example of unacceptable help on assignments?
  11. What is the Web address (URL) listing all homework assignments?
  12. What items are due for the A1: Getting Started assignment (list them)?
  13. When is the first assignment due and how do you turn it in?
  14. How many late assignments are accepted and how long do you have to complete a late assignment?
  15. When are you allowed to use computers (even your own) in the classroom?
  16. Why is cell phone use during class a disruptive behavior?
  17. What are the consequences of disruptive behavior?
  18. What food or drink is allowed in the classroom, if any?
  19. When emailing the instructor, what two pieces of information should you include at a minimum?
  20. What is the last day to request pass/no-pass grading and how do you request it?

Assessing the Treasure (Thursday: 5m)

  1. Login to Canvas and find A0-Treasure Hunt!
  2. Click on the "Download questions.txt" link and save your file to your computer
  3. Open the file in a text editor and display your answers on your computer screen.
  4. Find another student to review your answers while you review their answers.
  5. Review the answers of the other student and discuss any errors you find.
  6. Click on the "Submission Details" link and put the reviewer's name in a Canvas comment like:
    Reviewed by: Reviewer's name

1.3: Getting Started with C++

Learner Outcomes

At the end of the lesson the student will be able to:

  • Describe how a computer works
  • Describe the activity of programming
  • View and save source code
  • Compile and execute C++ programs

1.3.1: How a Computer Works

  • The computer is often called an electronic brain or referred to as intelligent
  • However, a computer is really just a machine that stores data and follows instructions
  • Because a computer is a machine, it can be powerful but is stupid: Click to show message
  • A computer is powerful because it flawlessly executes (performs) billions of instructions per second
  • A computer is stupid because it only mechanically follows simple instructions
  • Unlike humans, a computer has no innate understanding of what it is doing

Parts of a Computer

  • Computers have two main sections: hardware and software
  • Hardware is all the physical components like chips, circuits and wires
  • Software is the sets of instructions that tells a computer how to do something

(Take notes during in-class videos)

Check Yourself

  1. True or false: one thing that makes computers powerful is they can execute a large number of instructions in a fraction of a second.
  2. Computer hardware includes ________.
    1. applications (apps)
    2. circuits
    3. games
    4. web pages
  3. Computer software includes ________.
    1. chips
    2. circuits
    3. games
    4. wires
  4. CPU instructions are executed ________.
    1. algebraically
    2. left to right
    3. randomly
    4. sequentially
  5. True or false: Program instructions are stored in binary but programmers write code with English-like commands.

How Computer Programs Work

  • A computer is a machine the simply follows instructions
  • Computers have no insight or understanding and their instructions are nothing like a human's thought
  • Instead computer instructions are very simple like:
    instruction 1: Get number x from memory location Y
    instruction 2: x + 1
    instruction 3: x < 0?
    instruction 4: Store x in memory location Z
    instruction 5: ...
  • These instructions are entered, stored, processed and output all as binary numbers
  • So how does a computer work with these binary numbers?

Group Activity: Binary Numbers

I need 5 volunteers to come up to front of the class!

  • Each person will be given a piece of paper with 0 on one side 1 on the other
  • Everyone will form a line with all 0s facing outward toward the class
  • We will practice converting numbers from decimal (0-31) to binary

Check Yourself (hover to check answers)

  1. Computers are machines that ________.
    1. always know what to do
    2. mechanically follow instructions
    3. think intuitively
    4. are imprecise and slow
  2. Computers are made up of ________ that are either on or off.
    1. bits
    2. bites
    3. lights
    4. switches
  3. Because of how a computer is made, it is said to "understand" ________ numbers.
  4. The decimal value of the binary number 1101 is ________.

More Information

1.3.2: Developing a C++ Program

  • 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

Example C++ Source Code

#include <iostream>
using namespace std;

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

Entering and Saving Source Code

  • We can use almost any text editor to enter and save C++ source code
  • We use TextPad in the classroom and you can download it for use at home
  • We enter our source code into TextPad and save the file as: hello.cpp
    • Suggestion: save the code to the Desktop directory
    • Using the desktop allows us to see our code files on the computer screen
  • Make sure you save the source-code file using the extension: .cpp
  • All C++ source code files must have the correct extension or the compiler program will not work

About Using IDEs

  • Many Integrated Development Environments (IDEs) are available for C++
  • IDEs typically provide visual tools for designing forms and debugging code
  • I do not recommend using an IDE while learning C++ because:
    • An IDE will generate code for you, which does not help you learn
    • IDEs are complex tools that themselves are difficult to learn
    • An IDE can distract you from your goal of learning C++
  • I do recommend using IDEs once you have a basic understanding of C++:
    • Improves programmer productivity
    • Easier to develop user interfaces
  • We will look at how to use an IDE later in the course

Check Yourself

  1. True or false: source code is text written in a programming language.
  2. True or false: To write C++ source code you should use Microsoft Word.
  3. Our C++ source code files have the file extension ________.
  4. A sequence of precise instructions on how to accomplish a task is known as a(n) ________.
    1. algorithm
    2. definition
    3. problem
    4. source code

1.3.3: Introducing Our Compiler

  • After creating a source code file, we must convert the source code into machine instructions
  • We need to use a compiler program to convert source code to machine code
  • The process of using a compiler is known as compiling
  • 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: g++ (clang on Mac)
  • You must use this compiler or you have a risk of your code not compiling when I test your program
  • If you use a different compiler and your code does not compile using g++, you will get a low score
  • Note that the g++ compiler is installed on the CTC and the STEM Center computers

Installing g++

  • Follow the software installation instructions for your type of computer.
  • The g++ compiler comes installed on most Linux systems
  • Apple provides the g++ compiler free for their computers
  • An excellent way to install g++ on Windows is to install it with Cygwin
  • Cygwin is a set of free software tools that lets us run some Linux programs on Windows
  • The Cygwin software includes the g++ compiler
  • We can easily install Cygwin on Windows, with g++ included, by following my instructions:
  • As part of its tools, Cygwin provides a Linux-like shell (command line) environment
  • Linux is an operating system derived from Unix, which was written primarily in the C programming language
  • This command line (terminal) environment works the same on Linux, Mac OS X computers and Windows computers with Cygwin installed
  • Windows users, just like Mac and Linux users, need to use this terminal in this course

Check Yourself

  1. The name of our compiler is ________.
  2. True or false: you can use any compiler you want for this course.
  3. Cygwin is ________.
    1. Linux tools for Windows
    2. a free compiler for Windows
    3. a software application named after a citrus fruit
    4. free software for the Macintosh
  4. True or false: only Windows users need to install Cygwin.
  5. True or false: Mac users will never need to use Cygwin.

Exercise 1.3a: Installing g++ (12m)

In this exercise we install the g++ compiler on a computer. If you brought your own computer, you may work on installing the software for your computer. However, if you get stuck during installation you may need to get help after class (or during SI) as we have limited time for this exercise.


  1. Follow the software installation instructions for your type of computer.
  2. If you have problems, ask a classmate or the instructor for help.

When finished, please help those around you.

1.3.4: Using the Terminal Window

  • As we learn to develop programs we will need to use the command line
  • The command line interface is available using a terminal window

Opening a Terminal Window

  • Windows: search for "cygwin" and select the Cygwin! Cygwin Terminal
  • OS X: search for "terminal" and click the Terminal application (more...)
  • Linux: search for "terminal" and select the Terminal application (more...)

Using a Terminal Window

  • To use a terminal window, we type instructions at a command prompt
    $ g++
    • $ is the command prompt (do not type the $)
    • g++ is the command we type
  • After typing an instruction, we press the Enter key
  • If our g++ installation was successful, we will see a message like:
    $ g++
    g++: fatal error: no input files
    compilation terminated.

Directory tree on Windows

Navigating Folders

  • To get around in a file system, we navigate a path
  • Everything in a file system is stored in a computer file
  • Files are grouped together into computer directories, also called folders
  • 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 see the folder structure in the Windows explorer:
    1. Right-click on the Start button
    2. Open Windows Explorer
  • The left-hand side shows the directory structure
  • The Address bar shows the path
  • In a Terminal window we can only type characters
  • Thus we work with the path as a string of characters

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

Navigation Using the Command Line

  • When opening a terminal window, we usually start in our home directory
  • We can check the current folder path using pwd (print working directory)
  • We can list the files in the current folder using ls
  • We can change directories (folders) on our computer using the cd command in various ways
  • To change to a lower subfolder level: cd folderName
    cd Desktop
  • To change up one folder level: cd ..
    cd ..
  • To change to our home folder: cd ~
    cd ~
  • To change to the root of the file system: cd /
    cd /
  • To change to any arbitrary folder: cd path
    cd /cygdrive/c/Users/Student/Desktop

    Notice that Cygwin calls the C: drive: /cygdrive/c/

Stopping a Program

  • Stop any program running in a terminal by typing Ctrl+C (pressing the Ctrl and C keys at the same time)

Check Yourself

  1. After entering a command, always press the special key named ________.
  2. To display the current directory in the terminal window, enter the command ________.
  3. To list all the files in the current directory, enter the command ________.
  4. To navigate to your home directory, enter the command ________.
  5. To navigate to a subdirectory named Desktop, enter the command ________.

More Information

1.3.5: Compiling and Running Programs Using g++

  • Now that we can use the command line in a terminal window, we will look at how to compile programs
  • Here is an example program we can compile
    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, World!\n";
        return 0;
  • The command for compiling a C++ program is:
    g++ -Wall -Wextra -Wpedantic -std=c++11 -o programName sourceFile.cpp
  • The complete command includes the following compiler options:
    • -Wall: Enable all the warnings about questionable code (/* /* */)
    • -Wextra: Print extra warning messages for some problems (like unused-parameter)
    • -Wpedantic: Show all the warnings demanded by strict ISO compliance
    • -std=c++11: Allows more modern C++ language and catches more problem code
    • -o programName: place the executable output in programName
    • sourceFile.cpp: the name of our source code file
  • Upper and lower case matters for all these commands and options
  • For example, to compile a source code file named hello.cpp we type:
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp
  • This command produces the file hello.exe (just hello on Mac/Linux) from the source file hello.cpp
  • When we type in this command, a whole series of steps take place
  • These steps are shown in the following diagram
  • We will look at these steps in more detail as the course progresses

Compilation Process

Compilation process

Running a Compiled Program

  • To run a compiled program type ./ and its name after the command prompt
  • The operating system runs the file named programName
  • On Windows an .exe suffix is automatically appended

Step-By-Step Instructions for Compiling and Running hello.cpp at Home

  1. Make sure you have installed the software environment (AKA toolchain).

    Windows: How to Install Cygwin
    Mac OS X: How to Install g++ on the Mac

  2. Open a Terminal window.

    Windows: from the Start menu select Cygwin Terminal.
    Mac OS X: press ⌘ Command+Space, type Terminal in the search field, and press the Return key.

  3. Open a text editor.

    Windows: from the Start menu select TextPad, NotePad++ or NotePad.
    Mac OS X: press ⌘ Command+Space, type Textedit in the search field, and press the Return key.

  4. Save your source-code file with a text editor into your home folder.

    Windows: save in the Cygwin home folder: C:\Cygwin\home\username
    Mac OS X: save in your home folder: /Users/username/
    where username is your login name on your computer

  5. Type the compile command into the Terminal window:
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp

    If no errors occur, the compiler creates the file: hello.exe. You can see this new file by typing: ls

  6. After compiling, run (execute) the program by typing:

    You should see the program display a message.

Check Yourself

  1. The recommended place to save C++ source code files when compiling from the command line, assuming it is a Windows machine, is to the ________ folder.
  2. What command do you type at the command line to compile a program named "foo.cpp"
  3. What command do you type at the command line to run the program you compiled in the previous question?

More Information

Exercise 1.3b: Compiling and Running Programs (6m)

In this exercise we explore how to compile and run C++ programs on the computers in the classroom. Use a school computer and not your own computer for this exercise.


  1. Start Cygwin by selecting the Cygwin! Cygwin Bash Shell from the Start menu.

    In the terminal window, also known as the console window, you can use commands like cd to change directories, ls to list files and pwd to print the working directory. For more information on the commands see sections: 1.3.4: Using the Terminal Window.

  2. Start TextPad or NotePad++ and copy-paste this code:
    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, World!\n";
        return 0;
    Select the above code and press Ctrl+C to copy. Then click in the the text editor window and press Ctrl+V to paste.
  3. Save this code in a file named hello.cpp to the Cygwin home directory:
  4. In the command window, use the ls command to list all your files. You should see hello.cpp in the command window like the following:

Cygwin ls

  1. Compile the hello.cpp program by typing the following command at the command prompt and then pressing the Enter key:
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp
    After running the command, verify you see a window like the following:

Cygwin compiled

  1. To run the program type:
    After running the command, verify you see "Hello, World!" displayed in the terminal like the following:

Cygwin saying hello

  1. Create another text file named compiling.txt and record your answers to the following questions:

    Q1: What do you type to compile a program named foo.cpp?

    Q2: What do you type to run the source code you compile with question 1?

  2. Submit compiling.txt to Canvas as part of assignment 1.

When finished, please help those around you.

1.3.6: Summary and Reflection

computer diagram

Processor (CPU)

Processor (CPU)

  • A computer is a machine with four main parts:
  • Input Devices: allows a user to send information to a computer using devices like keyboards, mice, touch screens
  • Memory: Which is divided into multiple parts:
    • Main Memory: Typically semiconductor memory for data and program storage while a program is executing.
    • Secondary memory: Non-volatile storage using low cost memory devices like hard disks, flash drives, and cloud storage. Data must be transferred to main memory to be used by the CPU.
  • Processor (CPU): controls which instruction to execute and performs data processing like:
    • Retrieve numbers from main memory
    • Perform simple arithmetic
    • Save numbers to main memory
  • Output devices: allows the computer to send information to the user with monitors, printers, speakers

How a Computer Works

  • A computer is a machine that only does what it is programmed to do
  • A computer carries out instructions exactly (no interpretation) and quickly
  • Computer programs are the instructions for the computer
  • Programs and data are stored in main memory
  • 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
  • Once the algorithms are developed, we convert the algorithms to C++ source code
  • To write, edit and save source code, we use a text editor
    • Make sure to save the source-code file using the extension: cpp
  • To translate the source code to machine instructions, we use a compiler
  • The compiler we must use for this course is called: g++
  • The g++ compiler comes installed on most Linux systems
  • Also, the g++ compiler is available free from Apple for their computers
  • The best way to install g++ on Windows is to install it with Cygwin
  • Cygwin is a set of free software tools that lets us run most UNIX programs on Windows, including g++
  • You can easily install Cygwin on Windows, with g++ included, by following my instructions:
  • To develop programs, we must learn to use the command line
  • To compile a program, use the g++ command with appropriate options
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp
  • To run a program, type ./ and its name after the command prompt


Answer these questions to check your understanding. If you are not sure, then follow the links to the section and review the material.

  1. What are the four parts of a computer? (1.3.1)
  2. How does a computer work? (1.3.1)
  3. What is a C++ program? (1.3.2)
  4. What is the importance of source code? (1.3.2)
  5. What extension must you add to a C++ source code file name? (1.3.2)
  6. Why do we use text editors like TextPad for writing and editing source code rather than Word Processors like MS Word? (1.3.2)
  7. What is the importance of a compiler? (1.3.3)
  8. Why must everyone use the same compiler? (1.3.3)
  9. Try the following commands at the command line and describe what they do. (1.3.4)
    1. cd
    2. ls
    3. pwd
    4. cat hello.cpp
    5. exit
  10. What file does a compiler produce? (1.3.5)
  11. We used the following command to compile our hello.cpp program. Describe what does each word of the command does. (1.3.5)
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp


Due Next:
Treasure Hunt! (1/30/20)
Quiz 1 (Canvas) (2/4/20)
A1-Getting Started (2/6/20)
  • When class is over, please shut down your computer
  • Clean up around your desk area
  • You may complete unfinished exercises at the end of the class or at any time before the next class.
  • For help: email me or stop by during office hours (see home page)
  • Attend a Supplemental Instruction (SI) session
Last Updated: January 31 2020 @14:41:59