C++_Lecture_Three
C++_Lecture_Three
Programming Fundamentals
Control Structures
Dr. Ahmed Alnasheri 11/13/2020
INPUT statement
Example:
In pseudo code In C++
INPUT x cin>>x;
INPUT a, b cin>>a>>b;
OUTPUT a, b cout<<a<<b;
Example:
Example:
In pseudo code In C++
OUTPUT 3+6 cout<< 3+6;
OUTPUT x–y cout<< x-y;
Syntax:
Example:
Syntax:
Example:
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.
L.H.S = R.H.S.
X+ 3 = y + 4 Wrong
Z = x +4 True
x +4 = Z Wrong
X X5
5 6
XX+1
X 5 10 Y 10
• 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
1- Analysis stage:
◼ Problem Input:
- num1
- num2
◼ Problem Output:
- summation of two numbers
◼ Formula:
sum=num1+num2
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
Input num1,num2
Output sum
◼ Problem Output:
- Total cost of apples (in Ryals/fils)
◼ Formula:
Total cost = Number of kilos of apples × Cost per kilo
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.
2- Algorithm Design
ALGORITHM Avg
INPUT n1, n2, n3
sum n1 + n2 + n3
average sum / 3
OUTPUT average
END Avg
Input n1,n2,n3
sum = n1+n2+n3
Average = sum / 3
Output average
#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;
}
2b - 2a
4c
= 2*b - 2*a/4*c Incorrect answer
Solution
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.
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.