Programming1 Lecture 2
Programming1 Lecture 2
C++
LECTURE 2
Computer Programming I
CMPE 121
✓ Basics of Programming
✓ Programming Languages
✓ Introduction to C++
✓ Main concepts of Programming
✓ Analytic thinking and problem solving
✓ Flowcharts
✓ Flowgorithm program
2
Tony Gaddis, Starting Out with C++: From Control Structures
through Objects, 8th Edition
✓ Chapter 2 (from page 27 to 32)
• Series of instructions to a computer to accomplish a
task
• Instructions must be written in a way the computer can
understand
• Programming languages are used to write programs
4
• Set of commands that a computer has been
“taught” to understand
• Types of Programming Languages
– Machine Language
– Assembly Language
– High-Level Language
• We will use High-Level Languages
– C++ Language
5
6
7
// A simple C++ program
8
Line 2 looks like this:
#include <iostream>
10
Line 5 reads:
int main()
The word int stands for “integer.” It indicates that the function
sends an integer value back to the operating system when it is
finished executing.
All C++ programs have more than one function, every C++ program
must have a function called main. It is the starting point of the
program. If you are ever reading someone else’s C++ program and
want to find where it starts, just look for the function named main. 11
Line 6 contains a single, solitary character:
{
If you look at the third line down from the opening brace you’ll see
the closing brace. Everything between the two braces is the contents
of the function main.
12
After the opening brace you see the following statement in line 7:
To put it simply, this line displays a message on the screen. You will
read more about cout and the << operator later in this chapter. The
message “Programming is great fun!” is printed without the
quotation marks.
In programming terms, the group of characters inside the quotation
marks is called a string literal or string constant.
At the end of the line is a semicolon. Just as a period marks the end
of a sentence, a semicolon marks the end of a complete statement in
C++. Comments are ignored by the compiler, so the semicolon isn’t
required at the end of a comment
13
14
cout in detail
15
16
Line 8 reads:
return 0;
This sends the integer value 0 back to the operating system upon the
program’s completion.
The value 0 usually indicates that a program executed successfully.
17
In the sample program you encountered several sets of special
characters.
Table 2-1 provides a short summary of how they were used.
18
• A flowchart is a picture (graphical
representation) of the problem-solving
process.
• A flowchart gives a step-by-step procedure
for solution of a problem.
• Using flowcharts can show the sequence
and logic of each step before writing a
computer program.
• Even people with little programming
knowledge can understand flowcharts.
19
• Identify input and output.
• Apply reasoning skills to solve the problem.
• Draw the flowchart using the appropriate symbols and
arrows to show the sequence of steps in solving the
problem.
20
• Flowchart elements are various geometrical shaped boxes that
represent the steps of the solution.
• The boxes are connected by arrows to show the flow of the
solution.
Yes
Ellipse Parallelogram Rectangle Diamond
No
Arrows
21
Flowchart Geometric Purpose
Symbol shape
Ellipse is used to indicate the start and end of a
Ellipse flowchart.
int main()
Corresponding
{ code
cout << "Programming is great fun!";
return 0;
}
Corresponding
code
Conventional definitions
Telling a very fast moron exactly what to do
A plan for solving a problem on a computer
Specifying the order of a program execution
But modern programs often involve millions of lines of code
And manipulation of data is central
Definition from another domain (academia)
A … program is an organized and directed accumulation of resources
to accomplish specific … objectives …
Good, but no mention of actually doing anything
The definition we’ll use
Specifying the structure and behavior of a program, and testing that
the program performs its task correctly and with acceptable
performance
Never forget to check that “it” works
Software == one or more programs
26
C++ source code
C++ compiler
Object code
Executable program
linker
Library Object code
You write C++ source code
Source code is (in principle) human readable
The compiler translates what you wrote into object code (sometimes called
machine code)
Object code is simple enough for a computer to “understand”
The linker links your code to system code needed to execute
E.g., input/output libraries, operating system code, and windowing code
The result is an executable program
E.g., a .exe file on windows or an a.out file on Unix
27