0% found this document useful (0 votes)
2 views53 pages

C++_Lecture_Three

This document covers the fundamentals of programming control structures, focusing on sequencing, selection, and repetition. It provides detailed explanations of input, output, and assignment statements in both pseudo code and C++, along with examples and flow charts for algorithms. Additionally, it includes practical examples of algorithms for summation, cost calculation, and average computation.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

C++_Lecture_Three

This document covers the fundamentals of programming control structures, focusing on sequencing, selection, and repetition. It provides detailed explanations of input, output, and assignment statements in both pseudo code and C++, along with examples and flow charts for algorithms. Additionally, it includes practical examples of algorithms for summation, cost calculation, and average computation.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

dfdfdfdfdfdfddfdfdf

Programming Fundamentals

Control Structures
Dr. Ahmed Alnasheri 11/13/2020

Programming Fundamentals (Lecture Three) 1


Content

Topics to cover here:

◼ Introduction to Control Structures in the


algorithmic language
◼ Sequencing
◼ Sequencing in C++ language

Programming Fundamentals (Lecture Three) 2


Control Structures

◼ In your algorithm (or program), you can use


different types of statements.

◼ Thereare 3 categories of control structures:


1- Sequencing
2- Selection
3- Repetition

Programming Fundamentals (Lecture Three) 3


1- Sequencing

◼ A compound statement (or a block) is a


sequence of statements ordered in a way
to give a solution: e.g.
Statement 1
Statement 2
Statement 3
is a sequence of 3 statements

Programming Fundamentals (Lecture Three) 4


1- Sequencing

◼ The statements that have a sequential control:


1- INPUT statements
2- OUPUT statements
3- Assignment statement

INPUT statement

Use Input statement to input data into variables


from the standard input device (e.g. a keyboard).

Programming Fundamentals (Lecture Three) 5


INPUT Statement
Syntax:

In pseudo code In C++


INPUT List of variables cin >> identifier >> identifier ;

where, List of variables contains one or more variables

Example:
In pseudo code In C++
INPUT x cin>>x;

INPUT a, b cin>>a>>b;

The semantics (execution) of this statement:


You can enter the values you want for the variables in the statement from
the keyboard and the computer will assign these values into the variables
(stores them in memory).

Programming Fundamentals (Lecture Three) 6


OUPUT Statement

◼ The OUTPUT statement has many uses.

◼ Use OUTPUT statement to output the following


in the standard output device (EX: a screen).
1. The values of variables stored in memory
2. message (i.e. a string of characters).
3. The value of an expression.
4. Causes a new line ( Use this for output clarity)

Programming Fundamentals (Lecture Three) 7


OUPUT Statement
1- OUTPUT the values of variables stored in memory
Syntax:
In pseudo code In C++
OUTPUT List of variables cout<< identifier << identifier ;

where, List of variables contains one or more variables


Example:
In pseudo code In C++
OUTPUT x cout<<x;

OUTPUT a, b cout<<a<<b;

The semantics (execution) of this statement:


This statement allows the computer to access the locations of the variables
mentioned in the statement and displays their contents on an output device
(e.g. a screen).

Programming Fundamentals (Lecture Three) 8


OUPUT Statement
2- OUTPUT message
where message may by any string of characters enclosed with double quotas.
Syntax:

In pseudo code In C++


OUTPUT message cout >> “message” ;

Example:

In pseudo code In C++


OUTPUT “Enter 3 values” cout<<“Enter 3 values”;

The semantics (execution) of this statement:


This statement will display the message on the screen.

Programming Fundamentals (Lecture Three) 9


OUPUT Statement
3- OUTPUT expression
where expression is any arithmetic expression
Syntax:
In pseudo code In C++
OUTPUT expression cout >> expression;

Example:
In pseudo code In C++
OUTPUT 3+6 cout<< 3+6;
OUTPUT x–y cout<< x-y;

The semantics (execution) of this statement:


First, the expression is evaluated, then the result will be displayed on the
screen. For the first example, it will display 9.

Programming Fundamentals (Lecture Three) 10


OUPUT Statement

4- OUTPUT a new line

Syntax:

In pseudo code In C++

OUTPUT endl cout << endl;

Programming Fundamentals (Lecture Three) 11


OUPUT Statement
NOTE
You can mix between the different types of the OUTPUT statements.

Example:

In pseudo code In C++


OUTPUT “Length = “ , length cout<< “length=”<<length;

The semantics (execution) of this statement:


This statement will display the message
Length = on the screen and on the same line it will display the value of
the variable length.

Programming Fundamentals (Lecture Three) 12


Assignment Statement
Storing a new value in a memory location is called assignment.

Syntax:

In pseudo code In C++


Variable  Expression Variable = Expression

Example:

In pseudo code In C++


X  10 X = 10
Y X+5 Y= X+5
Z  y; Z=y;
The semantics (execution) of this statement:
1- The Expression on the RHS is evaluated
2- The result of the expression is assigned to the variable on the LHS

Programming Fundamentals (Lecture Three) 13


Assignment Statement
NOTE:
The right hand side (RHS) of the assignment
statement should be of the same data type of the left
hand side (LHS).
e.g.
1- T  true
This will be correct if T is of Boolean type.

2- A  x + y * 2
This will be correct if A has a numeric data type (e.g.
integer, or real) and the value of the expression on
(RHS) has the same numeric data type.

Programming Fundamentals (Lecture Three) 14


Assignment Operator

L.H.S = R.H.S.

X+ 3 = y + 4 Wrong
Z = x +4 True
x +4 = Z Wrong

Programming Fundamentals (Lecture Three) 15


Assignment Statement
◼How to execute a statement like X  X + 1 ?
Suppose we have:
X5
Then to execute X  X + 1, we proceed as follows:

X X5
5 6

XX+1

Programming Fundamentals (Lecture Three) 16


Assignment Statement
◼ Dereferencing:
If we want to copy a value from one memory location (say,
X) into another location (say, Y), we say that we
dereference a variable.
e.g.
X5
Y  10
XY // now X has the value 10

X 5 10 Y 10

Programming Fundamentals (Lecture Three) 17


C++ Language Elements
• The general form of a C++ program
// File: filename
// Program description
# include compiler directives
void main ( )
{
declarations section
executable statement section
}

Programming Fundamentals (Lecture Three) 18


1- Comments in Programs
• In C++, the two symbols // are used to
denote a program comment.

• If comments need more than one line, then


you can use the symbols /* to begin
comment and */ to end it.

Programming Fundamentals (Lecture Three) 19


2- The include compiler directive
• The line begins with #include represents a
compiler directive.
• A compiler directive is processed at
compilation time.
• C++ syntax:
# include <filename>

Programming Fundamentals (Lecture Three) 20


2- The include compiler directive
• e.g.
#include <iostream>
using namespace std;
• iostream is the name of a C++ library header file
whose contents are inserted in place of the #include
line during compilation.
• iostream is used to manipulate input/output
operations
• The standard I/O stream objects, cin and cout are
already defined in iostream

Programming Fundamentals (Lecture Three) 21


3- Declaration Section
• In the algorithmic language, we will use identifiers
without declaring them
• In C++ the declaration section tells the compiler
what data are needed in the program.
• Declarations are based on the problem data
requirements identified during the problem
analysis.
• All identifiers must be declared before they are
used.
• Every identifier associated with a problem data
element must be declared only once in the
declaration section.
Programming Fundamentals (Lecture Three) 22
3- Declaration Section
Syntax: <type> List-of-identifiers
where, type is any predefined type in C++, and
List-of-identifiers is a list that contains one or more identifiers.
1- Declaring identifiers of integer type :
int x ;
int x, y;
2- Declaring identifiers of character type:
char c1, c2 ;
3- Declaring identifiers to hold real numbers:
float sum ;
double total ;

Programming Fundamentals (Lecture Three) 23


Declaration and initialization

• int c=5;
• int n1=3, n2=7;
• float c1=3.5, c2;
• int z;
z=10;
In a program a variable has:
Name,Type,Size,Value

Programming Fundamentals (Lecture Three) 24


Examples on Simple Algorithms
Example 1

Write an algorithm to Compute and print the


summation of two numbers.
First, we have to analyze the problem to
understand what is the input, output of the
problem, and which formula to use to solve the
problem (if any).

Programming Fundamentals (Lecture Three) 25


Example1

1- Analysis stage:
◼ Problem Input:
- num1
- num2
◼ Problem Output:
- summation of two numbers
◼ Formula:
sum=num1+num2

Programming Fundamentals (Lecture Three) 26


Example1

2- Algorithm Design
We write the algorithm by using the pseudo
code
ALGORITHM Summation
INPUT num1, num2
sum num1+ num2
OUTPUT “sum=“ ,sum
END Summation

Programming Fundamentals (Lecture Three) 27


Example1
3- Testing the algorithm
We give a sample data to see whether the algorithm solves
the problem correctly or not.
To give a sample data, proceed as follows:
1- Prepare a table to contain all variables of the algorithm.
2- Give any data you like as input.
3- Calculate any formula in the algorithm using these data.
4- Show the output
e.g.
num1 num2 sum
13 50 ---
63
The output:
sum= 63

Programming Fundamentals (Lecture Three) 28


Flow Chart

Input num1,num2

Sum = num1+ num2

Output sum

Programming Fundamentals (Lecture Three) 29


Example1 on C++ Programs
//This program Compute and print the summation of two numbers
#include <iostream>
using namespace std;
void main() {
int num1, num2, sum;
cout<<"Please Enter two numbers:";
cin>>num1>>num2;
sum = num1 + num2;
cout<<"sum="<<sum<<endl;
}

Programming Fundamentals (Lecture Three) 30


Examples on Simple Algorithms
Example 2
Write an algorithm to determine the total cost of
apples given the number of kilos of apples
purchased and the cost per kilo of apples.

First, we have to analyze the problem to


understand what is the input, output of the
problem, and which formula to use to solve the
problem (if any).

Programming Fundamentals (Lecture Three) 31


Example2
1- Analysis stage:
◼ Problem Input:
- Quantity of apples purchased (in kilos)
- Cost per kilo of apples (in Ryals/fils per kilo)

◼ Problem Output:
- Total cost of apples (in Ryals/fils)

◼ Formula:
Total cost = Number of kilos of apples × Cost per kilo

Programming Fundamentals (Lecture Three) 32


Example2
2- Algorithm Design
We write the algorithm by using the pseudo
code
ALGORITHM apples
INPUT quantity, cost
total_cost  quantity * cost
OUTPUT “Total cost = “ , total_cost
END apples

Programming Fundamentals (Lecture Three) 33


Example2
3- Testing the algorithm
We give a sample data to see whether the algorithm solves
the problem correctly or not.
To give a sample data, proceed as follows:
1- Prepare a table to contain all variables of the algorithm.
2- Give any data you like as input.
3- Calculate any formula in the algorithm using these data.
4- Show the output
e.g.
quantity cost total_cost
3 0.9 ---
2.7
The output:
Total cost = 2.7

Programming Fundamentals (Lecture Three) 34


Flow Chart

INPUT quantity, cost

total_cost  quantity * cost

OUTPUT “Total cost

Programming Fundamentals (Lecture Three) 35


Example2 on C++ Programs
// File: apples.cpp
/*This program calculates the price of purchasing some kilos
of apples */
#include <iostream>
using namespace std;
void main ( )
{ int quantity ;
float cost , total_cost ;
cin >> quantity >> cost ;
total_cost = quantity * cost ;
cout << “Total cost = “ << total_cost << endl ;
}
Programming Fundamentals (Lecture Three) 36
Example 3
◼The problem statement:
Write an algorithm that Compute and print the
average of three numbers
1- Analysis stage:
◼ Problem Input:
- n1,n2,n3
◼ Problem Output:
- average
◼ Formula:
sum= n1 + n2 + n3
average = sum / 3

Programming Fundamentals (Lecture Three) 37


Assignment Statement
NOTE:
The right hand side (RHS) of the assignment
statement should be of the same data type of the left
hand side (LHS).
e.g.
1- T  true
This will be correct if T is of Boolean type.

2- A  x + y * 2
This will be correct if A has a numeric data type (e.g.
integer, or real) and the value of the expression on
(RHS) has the same numeric data type.

Programming Fundamentals (Lecture Three) 38


Example 3

2- Algorithm Design
ALGORITHM Avg
INPUT n1, n2, n3
sum n1 + n2 + n3
average  sum / 3
OUTPUT average
END Avg

Programming Fundamentals (Lecture Three) 39


Example 3

3- Testing the algorithm


n1 n2 n3 sum average
2 6 1
9
3
The output:
average= 3
-----------------------------------------------------------

Programming Fundamentals (Lecture Three) 40


Flow Chart

Input n1,n2,n3

sum = n1+n2+n3
Average = sum / 3

Output average

Programming Fundamentals (Lecture Three) 41


Example3 on C++ Programs
/*This program Compute and print the average of three numbers */

#include <iostream>
using namespace std;
void main() {
int n1, n2, n3;
float s, average;
cout<<"Please Enter three integers";
cin>>n1>>n2>>n3;
s = n1 + n2 + n3;
average = s / 3;
cout<<"\n Average = \t"<<average<<endl;
}

Programming Fundamentals (Lecture Three) 42


Programming Fundamentals (Lecture Three) 43
Discriminant

2b - 2a
4c
= 2*b - 2*a/4*c Incorrect answer

Solution

= (2*b - 2*a)/4*c Correct answer

Programming Fundamentals (Lecture Three) 44


Examples
Q1: Given the following declarations
int x,y,z,w;
int i = 10;
int j = 15;
float k = 15.0;
Indicate which C++ statements below are valid and find the
value of each valid statement. Also Indicate which are
invalid and why.
1. x=j / i ;
2. y=j % i ;
3. z=k / i ;
4. w=k % i ;

Programming Fundamentals (Lecture Three) 45


Examples
Q2: Find the output of the following program
#include <iostream>
using namespace std;
main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z=x+y;
cout << " x = " ;
cout << x ;
cout << " y = " ;
cout << y ;
cout << " z =x + y = " ;
cout << z ;
}

Programming Fundamentals (Lecture Three) 46


Examples
Q3) Writ algorithm and c++ program to find the Area of the Ring

Programming Fundamentals (Lecture Three) 47


Problem

Given a four-digit integer, separate


and print the digits on the screen

Programming Fundamentals (Lecture Three) 48


Analysis
Input: Number = 1234
Process:
◼ Take the remainder of the above number after dividing by 10
Eg 1234 / 10 gives remainder 4
1234 % 10 = 4
◼ Remove last digit
 1234/10 = 123.4
 123 (Truncation due to Integer Division)
◼ 123 %10 gives 3
◼ Remove last digit
 123/10 = 12.3
 12 (Truncation due to Integer Division)
◼ 12 % 10 gives remainder 2
◼ Remove last digit
 12/10 = 1.2
 1 (Truncation due to Integer Division)
◼ Final digit remains
Output: print the digits on the screen one digit in each line

Programming Fundamentals (Lecture Three) 49


Code
#include <iostream.h>
main ( )
{
int number;
int digit;
cout << “Please enter a 4 digit integer : ”;
cin >> number;
digit = number %10;
cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit << ‘\n’;
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit << ‘\n’;
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit;
}

Programming Fundamentals (Lecture Three) 50


Assignment Statement
NOTE:
The right hand side (RHS) of the assignment
statement should be of the same data type of the left
hand side (LHS).
e.g.
1- T  true
This will be correct if T is of Boolean type.

2- A  x + y * 2
This will be correct if A has a numeric data type (e.g.
integer, or real) and the value of the expression on
(RHS) has the same numeric data type.

Programming Fundamentals (Lecture Three) 51


Assignment Statement
NOTE:
The right hand side (RHS) of the assignment
statement should be of the same data type of the left
hand side (LHS).
e.g.
1- T  true
This will be correct if T is of Boolean type.

2- A  x + y * 2
This will be correct if A has a numeric data type (e.g.
integer, or real) and the value of the expression on
(RHS) has the same numeric data type.

Programming Fundamentals (Lecture Three) 52


The End

Programming Fundamentals (Lecture Three) 53

You might also like