Chapter 1 - C++
Chapter 1 - C++
Chapter 1 - C++
Marta G. (M.Sc.)
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.
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.
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.
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.
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
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
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
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.
Flowchart
Common flowchart symbols
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:
Flowchart
Example 2: Draw flow chart of an algorithm to add two numbers and
display their result.
Flowchart
Example 3: Write an algorithm description and draw a flow chart to
check a number is negative or not.
Home work
Draw a flowchart to input two numbers from user and display the
largest of two numbers
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.
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.
⋆ 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: