0% found this document useful (0 votes)
3 views

01_introduction_to-computers_programs_and_cpp

The document provides an introduction to computers, programming languages, and the C++ programming language, covering essential concepts such as the components of a computer, the programming cycle, and types of programming errors. It includes a simple C++ program example and discusses the toolchain involved in compiling and executing C++ programs. The document is structured as a course outline for teaching computer skills to engineers.

Uploaded by

qusaishalan85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01_introduction_to-computers_programs_and_cpp

The document provides an introduction to computers, programming languages, and the C++ programming language, covering essential concepts such as the components of a computer, the programming cycle, and types of programming errors. It includes a simple C++ program example and discusses the toolchain involved in compiling and executing C++ programs. The document is structured as a course outline for teaching computer skills to engineers.

Uploaded by

qusaishalan85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 1: Introduction to

Computers, Programs, and C++

Sections 1.1−1.3, 1.6−1.9


Textbooks: Y. Daniel Liang, Introduction to Programming with C++, 3rd Edition
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.

These slides were adapted by Prof. Gheith Abandah from the Computer Engineering Department of the University
of Jordan for the Course: Computer Skills for Engineers (0907101)
Updated by Dr. Ashraf Suyyagh (Summer 2021)
1
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

2
What is a Computer?
A computer consists of a CPU, memory, hard disk,
monitor, and communication devices.

Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

3
CPU
The central processing unit (CPU) is the brain of a
computer. It retrieves instructions from memory and
executes them. The CPU speed is measured in megahertz
(MHz), with 1 megahertz equaling 1 million pulses per
second. The speed of the CPU has been improved
continuously. If you buy a PC now, you can get an Intel
Core i7 Processor at 3 gigahertz (1 gigahertz is 1000
megahertz).
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
4
Memory
Memory is to store data and program instructions for CPU
to execute. A memory unit is an ordered sequence of
bytes, each holds eight bits. A program and its data must
be brought to memory before they can be executed. A
memory byte is never empty, but its initial content may be
meaningless to your program. The current content of a
memory byte is lost whenever new information is placed
in it.
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
5
How Data is Stored?
• Data of various kinds are
encoded as a series of bits (zeros
and ones).
• The encoding scheme varies. For
example, character ‘J’ is
represented by 01001010 in one
byte.
• A small number such as 3 can be
stored in a single byte.
• If computer needs to store a
large number that cannot fit into
a single byte, it uses a number of
adjacent bytes.
• A byte is the minimum storage
unit.
6
Storage Devices
Memory is volatile, because information is lost when the
power is off. Programs and data are permanently stored
on storage devices and are moved to memory when the
computer actually uses them. There are four main types
of storage devices: Disk drives (hard disks), Solid-state
devices (SSD, Flash), CD drives (CD-R and CD-RW), and
Tape drives.
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

7
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

8
Programs
Computer programs, known as software, are instructions
to the computer.

You tell a computer what to do through programs. Without


programs, a computer is an empty machine. Computers do
not understand human languages, so you need to use
computer languages to communicate with them.

Programs are written using programming languages.

9
Programming Languages
Machine Language Assembly Language High-Level Language
Machine language is a set of primitive Early computer systems used to punch
instructions built into every computer. (‫ )يثقب‬the 0’s and 1’s on special
The instructions are in the form of cardboard paper (punch cards)
binary code, so you have to enter Imagine making a single mistake and
binary codes for various instructions. you have to do it all again! Impractical
Program with native machine language
is a tedious process. Moreover, the
programs are highly difficult to read
and modify.
For example, to add two numbers, you
might write an instruction in binary like
this:
1101101010011010
e.g., computer designers will agree
ADD 42 26 what operation the first 4 bits indicate

10
Programming Languages
Machine Language Assembly Language High-Level Language
Assembly languages were For example, to add two numbers,
developed to make programming a you might write an instruction in
bit easier. assembly code like this:

Instead of writing programs in 0’s add 2, 3, result


and 1’s, we write the program in
terms of the desired operation
(ADD, SUB, MOV), the numerical
numbers, memory locations, etc.

Since the computer cannot


understand assembly language, a
program called assembler is used to
convert assembly language
programs back into machine code.

11
Programming Languages
Machine Language Assembly Language High-Level Language
▪ There are dozens of computer processor types: INTEL/AMD (found
mostly in PCs, laptops, servers), ARM (found mostly in tablets, phones,
some laptops), MIPS, RISC-V, etc.
▪ Each has a different design→ different machine code → different
assembly language.
▪ Imagine learning different assembly languages to develop a program
that runs on all these different computers. IMPRACTICAL!
▪ Solution: high-level languages written once, and translated to different
assembly languages as needed (one program → many machines)
▪ The high-level languages are English-like and easy to learn and program.
For example, the following is a high-level language statement that
computes the area of a circle with radius 5:
area = 5 * 5 * 3.1416;
12
High-Level Languages
Old High-Level Languages:
COBOL (COmmon Business Oriented Language), FORTRAN (FORmula TRANslation),
BASIC (Beginner All-purpose Symbolic Instructional Code), Pascal, Ada, Delphi
Popular High-Level Languages:
• C (used a lot in hardware programming / similar to C++)
• Visual Basic (Basic-like visual language developed by Microsoft)
• C++ (an object-oriented language, based on C)
• Java (a popular object-oriented language, similar to C++)
• C# (a Java-like developed my Microsoft)
• MATLAB (oriented for engineering and scientific applications)
• Python (used widely in data analysis and artificial intelligence )
• JavaScript (front-end/back-end, and mobile development)
• Also: Go, Ruby, Swift, Objective-C, Rust, Scala, Perl
13
From High-Level to Machine Code:
Compiling versus Interpretation
• Some programming languages like Python/MATLAB have interpreters that
translate and execute a program line by line as long as they are correct as seen in
Fig. (a). Think of it as being in a conference room, and someone is translating the
speech from English to Arabic as the person speaks.
• C++ needs a compiler that translates the entire source program into a machine-
language file for execution Fig. (b). All the program must be in machine code
before execution. Think of it as giving an English document to a translator and
they hand you the Arabic version.

14
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

15
A Simple C++ Program
Let us begin with a simple C++ program that displays the
message “Welcome to C++!” on the console.
#include <iostream>
using namespace std;
int main()
{
// Display Welcome to C++ to the console
cout << "Welcome to C++!" << endl;
return 0;
} Note: Clicking the green button displays the source code with
interactive animation and live run. Internet connection is needed for
this button.
Welcome Note: Clicking the blue button runs the code from Windows. To
enable the buttons, you must download the entire slide file slide.zip
Run and unzip the files into a directory (e.g., c:\slide). If you are using
Office 2010 or higher, check PowerPoint2010.doc located in the
same folder with this ppt file. 16
Special Characters in C++

17
Comments in C++

18
Extending the Simple C++ Program
Once you understand the program, it is easy to extend it to
display more messages. For example, you can rewrite the
program to display three messages.
#include <iostream>
using namespace std;
int main()
{
cout << "Programming is fun!" << endl;
cout << "Fundamentals First" << endl;
cout << "Problem Driven" << endl;
return 0;
} Run
WelcomeWithThreeMessages

19
Computing with Numbers
Further, you can perform mathematical computations and
displays the result to the console. Listing 1.3 gives such an
example.
#include <iostream>
using namespace std;
int main()
{
cout << "(10.5 + 2 * 3) / (45 - 3.5) = ";
cout << (10.5 + 2 * 3) / (45 - 3.5) << endl;

return 0;
} ComputeExpression Run

20
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

21
The C++ Toolchain
• From the moment you write your C++ program to the moment it
executes; your program passes through many steps.
• At each step, a certain tool does something towards the process
of converting your program from a high-level language to an
executable program.
• This sequence of tools is called a toolchain.
• Most of the time, the toolchain comes with your IDE (e.g.,
Microsoft Visual Studio), or needs to be downloaded separately
and configured to work with your IDE.
• Expert Programmers can configure the fine details of the tool
chain (e.g., choose each tool, which version, other options).
• In any C++ IDE, pressing the build button starts the toolchain.
22
The C++ Toolchain Main Programs
A program written in a high-level language is called a source program. It
has #include statements and may contain comments.
• The preprocessor strips out user comments and does replacements for
the include statements.
• A program called a compiler is used to translate the preprocessed
source program into the assembly code of the target machine.
• A program called the assembler then translates the assembly code to a
machine language program called an object program.
• The object program is often then linked using a linker with other
supporting library code before the object can be executed on the
machine.

23
Detailed Tool Chain Steps (1)

Assembly Code 24
Detailed Toolchain Steps (2)
Assembly Code

Assembler

25
C++ IDE Tutorial
You can develop a C++ program from a command window or from an
IDE. An IDE is software that provides an integrated development
environment (IDE) for rapidly developing C++ programs. Editing,
compiling, building, debugging, and online help are integrated in one
graphical user interface. Just enter source code or open an existing
file in a window, then click a button, menu item, or function key to
compile and run the program. Examples of popular IDEs are
Microsoft Visual Studio, Dev-C++, Eclipse, CodeBlocks, CLion, and
NetBeans. All these IDEs can be downloaded for free or with a free
student license.
IDEs might use different toolchains. For example, Microsoft Visual
Studio has their own compiler. CodeBlocks/CLion/Our server use
GCC. GCC/G++ is one of the world’s most famous compilers.
26
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

27
Programming Style and Documentation

• Appropriate Comments
• Proper Indentation and Spacing Lines
• Block Styles
#include <iostream>
using namespace std;
int main()
{
cout << "(10.5 + 2 * 3) / (45 - 3.5) = ";
cout << (10.5 + 2 * 3) / (45 - 3.5) << endl;

return 0;
} 28
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

29
Programming Errors

1. Syntax Errors
2. Runtime Errors
3. Logic Errors

30
Syntax Errors

ShowSyntaxErrors

31
Runtime Errors

ShowRuntimeErrors Run

32
Logic Errors

ShowLogicErrors Run

33
Common Errors

1. Missing Braces
2. Missing Semicolons
3. Missing Quotation Marks
4. Misspelling Names

34
Outline

• Introduction and Computers (§§1.1–1.2)


• Programming languages (§§1.3)
• A simple C++ program for console output (§1.6)
• C++ program-development cycle (§1.7)
• Programming style and documentation (§1.8)
• Programming errors (§1.9)

35

You might also like