01_introduction_to-computers_programs_and_cpp
01_introduction_to-computers_programs_and_cpp
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
2
What is a Computer?
A computer consists of a CPU, memory, hard disk,
monitor, and communication devices.
Bus
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
7
Outline
8
Programs
Computer programs, known as software, are instructions
to the computer.
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:
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
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
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
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
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
35