Schedule‎ > ‎


1: Introduction to Programming

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!


  • You are making a smart decision to take a course in computer science
  • Here are some good reasons why:

Computing Enhances Your Creativity

  • Computer science gives you the power to be creative in the digital world
  • Why merely create a FaceBook page when you can create the next FaceBook?
  • Or, why not create an app within Facebook? Or for your phone?
  • By creating programs you can improve your life by putting better ideas into practice using the power of computers

Computing Empowers You to do Good

  • Computers are a tool to help solve problems
  • With computers, you can connect technology to your community and make a world of difference, like:
  • Fight cancer with Artificial Intelligence
  • Also, computers give us new ways to learn things, some of which you will experience in this course

Computing Opens Doors

  • There are about two billion personal computers in use (source:, Worldometers)
  • In addition, there are many billions more computers embedded in everything from cell phones to car engines, iPods and video games
  • Because the number of computers is growing so fast, few careers are as rewarding as computing
  • Computer science pays well with average starting salaries of $71,534 for new grads in 2015 vs. $52,569 for the average College Graduate (source: NACE preliminary 2016)
  • Computers are used in every industry, making computing an important skill in any career
  • What majors are students in this class pursuing?

Coding = Your New Superpower

More Information

^ top

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
  • 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 if it works

More Information

^ top

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
  • First we have some samples from current and prior homework assignments:
    • blogtitles: Suggest titles for those who write blogs
    • printsquares: Draw shapes with ASCII text
    • eleet: Convert between leetspeak and standard English

More Examples

  • During the course you will learn how to develop programs like these:
  • While these are not assignments this semester, you will be capable of developing these types of programs

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?

^ top

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:

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
  • 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 the GCC g++ compiler
  • 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.
  4. While at Cabrillo, you can find a computer for programming in the ________.

More Information

^ top

Exercise 1.1: Introductions and Questions (10m)

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.

^ top

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

^ top

1.2.1: Course Design

  • The course is designed for learning basic computing concepts
  • It is organized into three parts 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
  • After 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


  • Your final grade is weighted based on your performance in the following areas:
    Assignments (projects, exercises, labs) 40%
    Midterms & Quizzes 30%
    Final exam 30%
  • 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 = 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

^ top

1.2.2: Succeeding in the Course

  • 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
  • You must learn the material 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.

  2. Take notes and study.

    Take notes during the lecture. Do NOT copy information verbatim; instead, jot down information that seems important or helpful in understanding the material. Schedule a study session within a day to review over your notes study the material and read the textbook. If something in the lecture or textbook does not make sense to you, ask the instructor to explain. The instructor wants you to succeed!

  3. 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 [2][3][4]. Students who multitask during a lecture score lower on exams than those who do not multitask[5]. Also, peers around the multitasker do worse on exams.

  4. Complete all your homework on time.

    Take responsibility for your learning and do your own homework. Remember Einstein's quote, "Genius is 1% talent and 99% hard work." Expect assignments to take time to complete, so start working on them long before they are due.

  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, or ask a classmate for help.

  6. Study for tests effectively.

    Take the quizzes and exams seriously and prepare. Don't wait until the night before to study! Prepare practice problems and test yourself. Review homework problems, and use the example problems in the textbook to help prepare for quizzes and test.

More Information

  1. Seven Keys for Academic SUCCESS
  2. Laptops hinder classroom learning for both users and nearby peers
  3. Ink on Paper: Some Notes on Note Taking
  4. The Pen Is Mightier Than the Keyboard: Advantages of Longhand Over Laptop Note Taking
  5. Think You're Multitasking? Think Again
  6. Meta-Analysis Summary: Learning Overview

^ top

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

  • 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 (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

Using Classroom Computers (in Watsonville):

  • Login with a generic student login and password
    • Username: your WebAdvisor id
    • Password: your initial WebAdvisor pw is your birthday in mmddyy format
  • 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 Cabrillo login and password to access Canvas, WiFi and other Cabrillo computers
  • See: How To Get Your Cabrillo Login and Password

Email and Texts

  • Please make sure your email address is correct in WebAdvisor
  • When sending me an email please state your name, course number and if you are in the day or Watsonville section
    • I really do not know who you are from an email address like:
  • Please make sure your emails are professional, that words are spelled correctly and that you use good grammar
  • You can 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
  • See Scholastic Honesty Policy in Syllabus for more information

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

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:
    • A word processor saves extra characters to control formatting whereas a text editor saves text with minimal formatting
    • Not all word processor files are interchangeable but text files are generally interchangeable
    • Word processors tend to be slow to start and complex to use while text editors are fast and simple to use
    • Most word processors can save files as plain text but we have to take care to specify the plain text format
  • Some of the text editors we can use are:
  • Make sure to use a text editor in plain text mode
  • We use TextPad in the classroom and we can download it free for evaluation at home
  • 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

^ top

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 break into teams of 3-4 people to find answers to the questions. The first team to find all the answers and to notify the instructor wins the treasure!

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 the first team wins.


  1. With your team, find the answers to the following Exercise Questions.
  2. Prepare a text file (NOT a Word document) named: questions.txt with the following questions copied into the file.
  3. At the top of the file, list the name of the people on your team
  4. Copy the following questions into the text file and then record the answers to the questions as well.
  5. When finished, save your file to upload to Canvas as part of assignment 1. Do NOT email the answers to the instructor.

    Note: 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. Where is the instructor's contact information located?
  4. What are the instructor's office hours and where are they?
  5. When & Where are the Tutored Study Sessions for this class?
  6. What is the Math class prerequisite for this course?
  7. What is CodeLab and why must you spend $25 to purchase it?
  8. What compiler must you use for this course?
  9. What are three things you should do to succeed in this course?
  10. How many hours per week can you expect to spend on this course outside of the classroom?
  11. What is scholastic dishonesty? Does your instructor feel strongly about this?
  12. What is an example of acceptable help and an example of unacceptable help on assignments?
  13. What is the Web address (URL) where all homework assignments are listed?
  14. What items are due for the first assignment (list them) and where do you submit the assignment?
  15. Can you turn in a late assignment?
  16. When are you allowed to use computers (even your own) in the classroom?
  17. Is mobile phone use during class a disruptive behavior?
  18. What are the consequences of disruptive behavior?

When finished, please verify your answers with one or more classmates.

^ top

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

^ top

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

    Computer: powerful but stupid

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
  • 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: 1 + 1
    instruction 2: 2 < 0?
    instruction 3: ...
    instruction 4:
    instruction 5:
computer diagram

Processor (CPU)Processor (CPU)

  • Input Devices: allows user to send information to a computer using keyboard, mouse, touch screen
  • Output devices: allows the computer to send information to the user with monitors, printers, speakers
  • 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
  • Main Memory: primary 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.

Activity (2m)

  1. Identify where the following parts of your computer are located
    1. Input devices
    2. Output devices
    3. CPU
    4. Main memory
    5. Secondary memory
  2. Verify your answer with a classmate and be prepared to show the class

How a Computer Works: Basic Bits

How Computers Work: Information

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

Play the Binary Game Binary Game

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. 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
  3. The purpose of main memory is to store both ________ and ________.
  4. True or false: computers can execute a large number of instructions in a fraction of a second.
  5. Computers are made up of ________ that are either on or off.
    1. bits
    2. bytes
    3. lights
    4. switches
  6. Because of how a computer is made, it is said to understand ________ numbers.
  7. Computers memory bits are organized into groups of eight known as a ________.
    1. byte
    2. chunk
    3. cookye
    4. set
  8. The decimal value of the binary number 1101 is ________.

More Information

^ top

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
  • 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 a very simple C++ program
  • 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.

^ top

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. 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
  3. True or false: only Windows users need to install Cygwin.
  4. True or false: Mac users will never need to use Cygwin.

^ top

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.

^ top

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

^ top

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

^ top

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 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. Submit hello.exe to Canvas in Exercise 1b.

When finished, please help those around you.

^ top

1.3.6: Summary and Reflection

  • A computer is a machine that only does what it is programmed to do
  • A computer carries out instructions exactly (no interpretation)
  • Computers carry out their instructions quickly
  • Computer programs are the instructions for the computer
  • Programs and data are stored in main memory, typically semiconductor 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 find the section and review the material.

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

^ top


Due Next:
A1-Getting Started (9/16/17)
  • When class is over, please shut down your computer
  • You may complete unfinished exercises at the end of the class or at any time before the next class.