1: Introduction to Programming

What We Will Cover


  • Parking
    • Warning tickets in student lots first week
    • Staff lots will be ticketed

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

Learner Outcomes

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

  • Describe what they will learn in this course

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 $71,916 for new grads in 2017 vs. $50,219 for the average College Graduate (source: NACE Class of 2016)
  • 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 create a FaceBook page when you can create the next FaceBook?
  • 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 and uses of computers.

  • Sometimes Computer Science is equated to computers or computer programming
  • However, Computer Science is no more about computers or programming than astronomy is about telescopes or biology is about microscopes
  • Science is not about tools, but how we use them and what we find out when we do
  • 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 Computer Science
  • Computer Science is the study of mechanized procedures, or algorithms, to process information
  • An algorithm is a sequence of precise instructions on how to accomplish a task
  • We use computers to implement and study our algorithms

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 could we halve the number of steps?
  • Here is a possibly 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:

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
    • 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
  • Which of these look the most interesting to you?
  • What would be a more interesting program for you to develop?

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
    • Another option for installing Cygwin is Babun which provides simpler installation
  • 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 CD
  • If you do not have access to a computer, you may use one in the Computer Technology Center (CTC) or STEM Center (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 is administered
  • 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. First part: basic skills including variables, if-statements, loops and algorithms.
    2. Second part: how to organize code and data for larger programs using functions, objects, classes and lists.
    3. Third part: learning more about objects, lists and files, as well as applying your skills to solve more complex problems.

Class Meetings

  • 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, then 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 need to work into programs
  • Before class meetings students are expected to prepare for class by:
    • Reading the textbook
    • Completing tutorial labs
  • During class we have "lesson exercises" to engage in and try out the new concepts presented
  • Students will need to 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

  • As an experiment this semester, I am using a grading robot [*_*]
  • The purpose is to allow students to submit multiple times up to the due date
  • This gives students a chance to get feedback and improve an assignment
  • Final grading will still be 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 may be given from time to time
  • Students must attend class to take the quiz and no makeups are given
  • 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 learn to program C++ in this course
  • You need to 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 need to work with the ideas to truly learn them
  • Rote learning and memorization is not enough
  • You must learn to solve problems with, and work creatively with, code to pass the course

Optimizing Your Learning

  1. Go to class and participate.

    Silence your cell phones, pay attention to the lecture and participate in the activities. Ask questions in class. Establish good rapport with your classmates and the instructor. Get to know other students in the class during activities and breaks. Look forward to learning! [1][2]

  2. Use your computer only during specified times and for specified tasks

    When using a computer do not become distracted or try to multitask. Students who take notes on a computer do worse on exams than those who take notes on paper [3][4][5][6]. Students who multitask during a lecture score lower on exams than those who do not multitask [7]. Also, peers around the multitasker do worse on exams because they become distracted.

  3. Quiz yourself on course material

    Review the Check Yourself questions from the lecture notes after class. Practice recalling the important material using flash cards (physical or digital). Answer the sample questions at the end of the textbook section or chapter. Learning research shows that practice tests are the best way to learn factual information. Spreading study over time is more effective than cramming. [8][9]

  4. Always do your own homework - and start it as early as possible!

    College is not like High School--the time you spend in class is small compared to the time you spend on assignments and studying. Take responsibility for your learning and do your own homework. Remember Einstein's quote, "Genius is 1% talent and 99% hard work." Hard and smart work is what matters, not native "intelligence". Start your homework early. Code a little bit every day. Expect this class to be hard work, expect it to be time-consuming, and plan accordingly. [8][9][10]

  5. When you are stuck, don't give up!

    Programming is frustrating at times. Even professional programmers get stuck. Have confidence that you will find the solution to your problem. If you find you are stuck, take a break and come back to it later, talk to the professor, go to the STEM Center and ask a tutor, or talk to a classmate for help. With tenacity and effort you can change your learning outcome and succeed. [11][12]

More Information

  1. Seven Keys for Academic SUCCESS: suggestions from Cabrillo faculty
  2. Meta-Analysis Summary: Learning Overview: 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

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

Using the Course Website

  • www.edparrish.net has a link to the course web site and has contact information for the instructor
  • Syllabus describes the policies of the course including prerequisites and grading
  • Home Page describes the online content of the course
  • Canvas is where you turn in assignments, take tests and can look at your grades and written feedback on assignments (events)
  • Schedule page has links to lecture notes, reading and assignments
    • Read the assigned reading BEFORE class
    • Assignments are due before class
  • Date and time listed in Canvas takes precedence over 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: How do I add a class after the term has started?

Using Classroom Computers

  • Logins for Canvas, Hawknet and most computers on Campus are the same your WebAdvisor login.
  • However, logins for this classroom are different than for other computers
  • Login with a generic student login and password
    • Username: student
    • Password: Cabri11o (a.k.a. funny Cabrillo)
  • All contents of C:\ will be lost 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 precious!
  • You need your own Cabrillo login and password to access Canvas, WiFi and Cabrillo lab computers
  • See: How To Get Your Cabrillo Login and Password

Email and Texts

  • Please make sure your email address is correct in WebAdvisor
  • Check your email at least once per day or you will miss important information
  • When sending me an email please state your name, course number and if you are in the day or evening section
    • I really do not know who you are from an email address like: fandango246@hotmail.com
  • Please make sure your emails are professional, that words are spelled correctly and that you use good grammar
  • Suggestion: setup Canvas to provide text (SMS) notifications

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)
  • 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
  • If you do not have access to a computer, you may use one in the Computer Technology Center (CTC) or STEM Center (Map)

Grading Options

  • On the first assignment, you determine your grading option: default, letter, credit/no-credit
  • To change your mind later in the course, up to the deadline, send me an email

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
  • Copying any amount of code without attribution is plagiarism, a form of cheating
  • When helping someone, do not to just 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
  • When someone helps you, do not let them touch your keyboard
  • See 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 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

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

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. To find the answers, look at the section we just covered: 1.2.3: Useful Information and Course Resources. We will discuss the questions and answers after you finish answering.


  1. Find the answers to the following Exercise Questions.
  2. Prepare a text file (NOT a Word document) named: questions.txt
  3. Copy the following Exercise Questions into the text file and then record the answer after each question.
  4. At the top of the file, list the name of person the who verified your answers are correct.
  5. When finished, save your file to upload to Canvas as part of assignment 1. Do NOT email the answers to the instructor.

    Remember that you must save your file somewhere besides a classroom computer. Your classroom computer will delete your file when you logout.

Exercise Questions

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

Reviewing the Treasure (5m)

  • Display your answers on your computer screen.
  • Find another student to review your answers while you review their answers.
  • Review the answers and discuss any errors you found.
  • Put your name at the top of the Treasure Hunt file of the student you are reviewing as follows:
    Reviewed by: Your 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
  • Develop algorithms to solve simple computational problems

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

How a Computer Works

  • 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
  • So, what physical parts makes up the machine called a computer and how do the parts work together?

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. Of the following, the main hardware of a computer does not include ________.
    1. input
    2. memory
    3. processor
    4. software
  3. The purpose of memory is to store both ________ and ________.
  4. The Central Processing Unit (CPU) is primarily responsible for ________.
    1. controlling programs and processing data
    2. ensuring data persists when electrical power is turned off
    3. enabling a human user to interact with the computer
    4. interconnecting computers that are separated by distance
  5. An example of a computer output device is a ________.
    1. display screen
    2. keyboard
    3. mouse
    4. processor

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. design computer programs
    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 is
    Print "Hello World"
  • We will be developing many algorithms during this course
  • Once the algorithms are developed, we convert the algorithms 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
    • Save the code to the Desktop directory
    • That way we can see our code files on our 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. Our C++ source code files have the file extension ________.
  3. True or false: To write C++ source code you should use a text editor.

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 use 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++
  • 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
    • The instructor will compile your code on g++ running under Cygwin
  • Note that the g++ compiler is installed in the CTC and the STEM Center at Cabrillo

Installing g++

  • The g++ compiler comes installed on most Linux systems
  • Also, the g++ compiler is available free from Apple 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 environment works the same on Linux, Mac OS X computers and Windows computers with Cygwin installed
  • Windows users, in addition to Mac and Linux users, will need to use this command line 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 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 a 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
  • Linux: search for "terminal" and select the Terminal application (more...)

Using a Terminal Window

  • To use a terminal window, we type commands at a command prompt
    $ g++
    • $ is the command prompt (do not type this)
    • g++ is the command we type
  • After typing a command, 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.

Exiting a Terminal Window

  • At the command prompt, type exit
  • Another option is to press the "Close Window" (X) button

Stopping a Program

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

Navigating Folders

  • To get around in a file system, we need to navigate its directory tree
  • Everything in a file system is stored in a computer file
  • Files are grouped together into computer folders, also called directories
  • Folders are organized in a hierarchical structure starting at the root folder

    Directory tree

  • A path is the folders the computer must go through to get to a particular file
  • We can see the folder structure in the Windows explorer:
    • Right-click on the Start button
    • Select Explore from the list
  • The left-hand side shows the directory structure
  • The Address bar shows the path
  • At the command line, we only work with the path
  • We must understand folders and paths to work with our files

Navigation Using the Command Line

  • When the command line starts, we are located in our home directory
    • The home directory depends on your computer
  • 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 our home folder: cd ~
    cd ~
  • To change up one folder level: cd ..
    cd ..
  • To change to a lower subfolder level: cd folderName
    cd Desktop
  • 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/

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 -std=c++11 -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.exe
  • Note that an .exe suffix is automatically appended
  • Thus, we do not need to type the .exe part of the program name

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

  1. Start Cygwin if using Windows

    Select the Cygwin Terminal from the Start menu. In the classroom, Cygwin is located in the CS & CIS submenu.

  2. Save your hello.cpp source-code file using a text editor into your home folder. On Windows, this will be the Cygwin home folder:
  3. Type the compile command:
    g++ -Wall -Wextra -Wpedantic -std=c++11 -o hello hello.cpp

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

  4. At the command prompt type:

    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 in the classroom.


  1. Start TextPad and enter this code:
    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, World!\n";
        return 0;
  2. Save this code in a file named hello.cpp to the your Cygwin home directory:
  3. 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 you can use see sections: 1.3.4: Using the Terminal Window.

  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 -std=c++11 -o hello hello.cpp
    After running the command, you should see a window like the following:

Cygwin compiled

  1. To run the program type:
    After running the command, you should see a window displaying, "Hello, World!", like the following:

Cygwin saying hello

  1. In a second text file named compiling.txt, 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 -std=c++11 -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 -std=c++11 -o hello hello.cpp


Due Next:
A1-Getting Started (2/8/18)
  • When class is over, please shut down your computer
  • Clean up around your desk area
  • For help: email me following istructions on my home page
  • You may complete unfinished exercises at the end of the class or at any time before the next class.
Last Updated: February 14 2018 @16:22:56