Chapter 1 - C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

Computer Programing

Chapter One: Introduction to programming

Marta G. (M.Sc.)

St. Mary University


Faculty of Informatics
Department of Computer Science

March 31, 2022

Chapter One: Introduction to programming 1 / 41


Introduction to programming

Definition
Computer is an electronic device that accepts data, performs
computations, and makes logical decisions according to
instructions that have been given to it;
Then produces meaningful information in a form that is useful to the
user.
Computer Programs, Software Programs, or just Programs
is instructions that tells the computer what to do.
Computer programming is the process that professionals use
to write code that instructs how a computer, application or
software program performs.

Chapter One: Introduction to programming 2 / 41


Introduction to programming

Computer programs (also know as source code) is often written by


professionals known as Computer Programmers (simply
programmers).
Source code is written in one of programming languages.
A programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer.
Programming languages, like natural language (such as Amharic), are
defined by Syntax and semantic rules
Syntax: is the set of predefined rules, processes, and protocols that
everyone should follow, if they want an error-free code.
Semantic:is the meaning of the expressions, statements, and
program units.

Chapter One: Introduction to programming 3 / 41


Introduction to programming

The main purpose of programming languages is to provide


instructions to a computer.
Computers do exactly what they are told to do, and cannot
understand the code the programmer ”intended” to write.
Programming languages can be divided in to two major categories:
low-level and high-level languages.

Chapter One: Introduction to programming 4 / 41


Programming Languages Type

Low-level Languages
Computers only understand one language and that is binary language or
the language of 1s and 0s.
All the instructions were given in binary form.
Although the computer easily understood these programs,
It proved too difficult for a normal human being to remember all the
instructions in the form of 0s and 1s.
Low-level Languages is a type of language, which is machine-
friendly.
Two common types of low-level programming languages are
assembly language and machine language.

Chapter One: Introduction to programming 5 / 41


Programming Languages Type

Low-level Languages Type


Machine-level language
The Machine-level language is a language that consists of a set of
instructions that are in the binary form 0 or 1.
⋆ So,the instructions given to the computer can be only in binary codes.
Creating a program in a machine-level language is a very difficult task
⋆ As,it is not easy for the programmers to write the program in machine
instructions.
If we write a program in one computer will no longer be valid in
another computer.
It does not require any translator as the machine code is directly
executed by the computer.
It is a first-generation programming language.

Chapter One: Introduction to programming 6 / 41


Programming Languages Type

Low-level Languages Type


Assembly language
Assembly language correspondences symbolic instructions and
executable machine codes
Since assembly language instructions are written in English words like
mov, add, sub, so it is easier to write and understand.
As we know that computers can only understand the machine-level
instructions
⋆ So we require a translator that converts the assembly code into
machine code.
The translator used for translating the code is known as an assembler.

Chapter One: Introduction to programming 7 / 41


Programming Languages Type

High-level languages
A high-level language is any programming language that enables
development of a program in a much more user-friendly
programming context
High-level language is generally independent of the
computer’s hardware architecture.
High-level language does not require addressing hardware
constraints when developing a program.
High-level languages also require translation to machine language
before execution.

Chapter One: Introduction to programming 8 / 41


Programming Languages Type

This translation is accomplished by either a compiler or an


interpreter.
Compiler
⋆ Compilers translate the entire source code program before execution.
Interpreters
⋆ Interpreters translate source code programs one line at a time.
FORTRAN (FORmula TRANslator), BASIC (Bingers All Purpose
Symbolic Instruction Code), PASCAL, C, C++, Java, python,
JavaScript, PHP,etc.. are some examples of high-level languages.

DBU Chapter One: Introduction to programming March 31, 2022 9 / 41


Problem Solving Techniques

Computer solves varieties of problems that can be expressed in a


finite number of steps leading to a precisely defined goal by writing
different programs.
A program is not needed only to solve a problem but also it
should be reliable, (maintainable) portable and efficient.
In computer programming two facts are given more weight:
The first part focuses on defining the problem and logical
procedures to follow in solving it.
The second introduces the means by which programmers
communicate those procedures to the computer system so that it
can be executed.

Chapter One: Introduction to programming 10 / 41


Problem Solving Techniques

Before a program is written, the programmer must clearly


understand
What data are to be used
The desired result
The procedure to be used to produce the result.
The procedure, or solution, selected is referred to as an algorithm.
Algorithm is defined as a step-by-step sequence of instructions
that must terminate and describe how the data is to be processed to
produce the desired outputs.
Algorithm is a sequence of instructions

Chapter One: Introduction to programming 11 / 41


Problem Solving Techniques

There are three commonly used tools to help to document program


logic (the algorithm).
These are
⋆ Flowcharts
⋆ Structured chart
⋆ Pseudocode

Chapter One: Introduction to programming 12 / 41


Problem Solving Techniques

Pseudocode
Pseudocode is a simple way of writing programming code in
English.
Pseudocode is a methodology that allows the programmer to
represent the implementation of an algorithm.
No standard for pseudocode syntax exists.
Program in pseudocode is not an executable program.
Pseudocode is used for larger problems.

Chapter One: Introduction to programming 13 / 41


Problem Solving Techniques

Examples of Pseudocode
Let’s review an example of pseudocode to create a program to add 2
numbers together and then display the result.
Start Program
Enter two numbers, A, B
Add the numbers together
Print Sum
End Program

DBU Chapter One: Introduction to programming 14 / 41


Problem Solving Techniques

Class Work
Write the Pseudocode that obtains three numbers from the user. It will
print out the sum of those numbers.
Write the Pseudocode that calculate the area of a rectangle

DBU Chapter One: Introduction to programming 15 / 41


Problem Solving Techniques

Structured Charts
Structure Chart represent hierarchical structure of modules.
It breaks down the entire system into lowest functional modules
Describe functions and sub-functions of each module of a system to a
greater detail.
Practice with example

DBU Chapter One: Introduction to programming 16 / 41


Problem Solving Techniques

Flowchart
Flowchart is a type of diagram that represents an
algorithm, workflow or process.
Flowchart can be helpful for both writing programs
and explaining the program to others.
The advantage of Flowchart is it doesn’t depend on
any particular programming language
So that it can used, to translate an algorithm to more than
one programming language.

Chapter One: Introduction to programming 17 / 41


Problem Solving Techniques

Flowchart
Common flowchart symbols

DBU Chapter One: Introduction to programming March 31, 2022 18 / 41


Problem Solving Techniques

Flowchart
Example 1: Flowcharts can be used to plan out programs. Planning a
program that asks people what the best subject they take is, would look
like this as a flowchart:

DBU Chapter One: Introduction to programming March 31, 2022 19 / 41


Problem Solving Techniques

Flowchart
Example 2: Draw flow chart of an algorithm to add two numbers and
display their result.

DBU Chapter One: Introduction to programming March 31, 2022 20 / 41


Problem Solving Techniques

Flowchart
Example 3: Write an algorithm description and draw a flow chart to
check a number is negative or not.

DBU Chapter One: Introduction to programming March 31, 2022 21 / 41


Problem Solving Techniques

Home work
Draw a flowchart to input two numbers from user and display the
largest of two numbers

DBU Chapter One: Introduction to programming March 31, 2022 22 / 41


Problem Solving Techniques

Flowchart
Some times there are conditions in which it is necessary to execute a
group of statements repeatedly.
Until some condition is satisfied
This condition is called a loop.
Loop is a sequence of instructions, which is repeated until some
specific condition occurs.

DBU Chapter One: Introduction to programming March 31, 2022 23 / 41


Problem Solving Techniques

A loop normally consists of four parts. These are:


Initialization
Setting of variables of the computation to their initial values and
setting the counter for determining to exit from the loop.
Computation
Processing
Test
Every loop must have some way of exiting from it or else the program
would endlessly remain in a loop.
Increment
Re-initialization of the loop for the next loop.

DBU Chapter One: Introduction to programming March 31, 2022 24 / 41


Problem Solving Techniques

A loop normally consists of four parts. These are:

DBU Chapter One: Introduction to programming March 31, 2022 25 / 41


Problem Solving Techniques

Example 1: - Write the algorithmic description and draw a flow chart to


find the following sum: Sum = 1+2+3+. . . . + 50

DBU Chapter One: Introduction to programming March 31, 2022 26 / 41


Structure of C++ program
Documentation section(Optional): used to write for purpose of the
program.
• Eg: //this is a c++ code to add two numbers
Linking section: consists of
• Header files like built-in functions, classes, keywords etc.
• Every C++ code consists: #include<iostream>
▪ This enables the programmer to use standard input,
output, and error facilities that are provided only through
the standard streams defined in <iostream>.
▪ Namespaces
▪ A namespace permits grouping of various entities like
classes, objects, functions, and various C++ tokens, etc.
▪ namespace std contains declarations for cout, cin, endl,
etc. statements.
Definition section
▪ It is used to declare some constants and assign them
some value.
Global Declaration Section
•Here, the variables and the class definitions which are
going to be used in the program are declared to make them
global.
Compiling
To turn your source code into a program, you use a compiler.
After your source code is compiled, an object file is produced.
This file is often named with the extension .OBJ.
This is still not an executable program
However. To turn this into an executable program, you must run
your linker.
Linking
The linker takes the object files produced by the compiler
and produces either a library or an executable file.
The object code is combined with required supporting code
to make an executable program.
Basic elements of C++ language

Keywords (Reserved Words)


Reserved/Key words have a unique meaning within a C++
program.
These symbols, the reserved words, must not be used for any
other purposes.
All reserved words are in lower-case letters.
Identifiers
Identifiers are the unique names given to variables, classes,
functions, or other entities by the programmer
For example
• int money;
• double accountBalance;
Rules for naming identifiers
• Rule 1: Identifiers can be composed of letters, digits, and
the underscore character.
• Rule 2: It has no limit on name length.
• Rule 3: Start with a letter or underscore
• Rule 4: A keyword cannot be used as an identifier.
• Rule 5: These are case sensitive
Which one of the following correct and incorrect identifiers?
A. int sum
B. float Test1
C. float 2data1
D. int Total points
F. int my age
E. int else
F. int my age
G. int DataSet1
H. int first 1
J. int first one
K. int first one
L. int Int
Literals
Literals are constant values which can be a number, a
character of a string.
For example
• The number 129.005
• The character ‘A’
• The string “hello world”
Comments
• Comment is a piece of descriptive text which explains some
aspect of a program.
• Program comments are totally ignored by the compiler and are
only intended for human readers.
• We have two types of comment in Programming :
▪ Single Line Comment
⋆ Single Line comment starts with “//” symbol.
⋆ Remaining line after “//” symbol is ignored by browser.
⋆ cout¡¡”Hello”; //Print Hello Word
• Multiple Line Comments
⋆ Multi Line comment starts with “/*” symbol.
⋆ Multi Line comment ends with “*/” symbol
Data types, Variables and Constants

Variables
• Variable is a symbolic name for a memory location in which data
can be stored and subsequently recalled.
• Variables are used for holding data values so that they can be
utilized in various computations in a program.
• All variables have two important attributes:
• Type, which is, established when the variable is defined (e.g.,
integer, float, character).
⋆ Once defined, the type of a C++ variable cannot be changed.
• Value, which can be changed by assigning a new value to the
variable.
⋆ The kind of values a variable can assume depends on its
⋆ For example, an integer variable can only take integer
values e.g., 2, 100, -12 not real numbers like 0.123
Variable Declaration
• Declaring a variable means defining (creating) a variable.
• You create or define a variable by stating its type, followed by
one or more spaces, followed by the variable name and a
semicolon.
• Variables must be declared before used!
• C++ is case-sensitive.
• You can create more than one variable of the same type in one
statement by writing the type and then the variable names,
separated by commas.
• int myAge, myWeight;
• int area, width, length;
Assigning Values to Your Variables
You assign a value to a variable by using the assignment
operator (=).
for example:
• int Width;
• Width = 5;
You can combine these steps and initialize Width when
you define it
by writing
• int Width=5;
Basic Data Types
• When you define a variable in C++, you must tell the compiler
what kind of variable it is:
• an integer, a character, and so forth
• This information tells the compiler how much room to set aside
and what kind of value you want to store in your variable.
• The data types supported by C++ can be classified as
• Basic (fundamental) data types
• User defined data types
• Derived data types
• Empty data types
However, the discussion here will focus only on the basic data
types.
Basic (fundamental) data types
• Basic (fundamental) data types in C++ can be conveniently
divided into numeric and character types
• Integer:
⋆ Keyword used for integer data types is int.
⋆ Integers typically requires 4 bytes of memory space
⋆ For example,
⋆ int salary = 85000;
Character:
⋆ Character data type is used for storing characters.
⋆ Keyword used for character data type is char.
⋆ Characters typically requires 1 byte of memory space
⋆ for example:
⋆ char ch = ’A’;
Boolean:
Boolean data type is used for storing boolean or logical values.
⋆ A boolean variable can store either true or false.

⋆ Keyword used for boolean data type is bool.

⋆ For example:

⋆ bool c = false;

Floating Point:
⋆ Floating Point data type is used for storing single precision floating
point values or decimal values.
⋆ Keyword used for floating point data type is float.

⋆ For example:

⋆ float num = 123.78987;


Signed and Unsigned
• Integer types come in two varieties: signed and
unsigned.
• Integers (short and long) without the word ”unsigned”
are assumed to be signed.
• Signed integers are either negative or positive.
• Unsigned integers are always positive.
Thank you

You might also like