Computer Merge Lectures
Computer Merge Lectures
Lecture No. 1
Program
“A precise sequence
of steps to
solve a particular
problem”
Alan Perlis – Yale University:
Outer Circle
Area of Outer Circle ____ Area of Inner Circle = Area of the Ring
• Think Reuse
• Think User Interface
• Comments liberally
What is the probability that she gets
exactly three letter right i.e. three
Letters into their correct envelopes.
Logical Error
Lewis Carol: “Through the Looking Glass”
Lecture 2
Today’s Lecture
Software Categories
System Software
Application Software
Evolution
Justification
System software
Application Software
TWAIN
Editor
Interpreter and Compilers
Debuggers
Integrated Development Environment
(IDE)
It contains
Editor
Compilers
Debugger
Linkers
Loaders
Program is created in the
Editor Disk editor and stored on disk.
Preprocessor program
Preprocessor Disk processes the code.
Compiler creates
Compiler Disk object code and stores
it on disk.
Linker Disk Linker links the object
code with the libraries
Primary Memory
Loader
Loader puts program
in memory.
Disk ..
..
..
Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
..
values as the program
..
executes.
Introduction to Programming
Lesson 3
#include <iostream.h>
main ( )
{
cout << “ Welcome to Punjab University
“;
}
Variable
Variable X
Variable
Pic of the memory
25
name
of the
10323
variable
Variable
Variable starts with
1. Character
2. Underscore _ (Not Recommended)
Variable
Small post box
X
Variable
Variable is the name of a location in
the memory
e.g. x= 2;
Variable
In a program a variable has:
1. Name
2. Type
3. Size
4. Value
Assignment Operator
=
x=2
X
2
Assignment Operator
L.H.S = R.H.S.
X+ 3 = y + 4 Wrong
Z = x +4
x +4 = Z Wrong
X = 10 ; X 10
X = 30 ;
X 30
X = X + 1;
X
10 + 1
= 11
X
Data type
int i ; ->
Declaration line
i
#include <iostream.h>
main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z=x+y;
Divide /
Modulus %
Arithmetic operators
i+j
x*y
a/b
a%b
% = Remainder
5%2=1
2%2=0
4/2=2
5/2=?
Precedence
Highest: ()
Next: *,/,%
Lowest: +,-
Introduction to Programming
Lecture 4
Key Words of C
main
if
else
while
do
for
Memory
x=2+4;
=6;
Memory
a b
x=a+b;
x
#include <iostream.h>
main ( )
{
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;
int TotalAge ;
int AverageAge ;
In C
y = a*x*x + b*x + c
a*b%c +d
a*(b%c) = a*b%c
?
Discriminant
b2 - 2a
4c
= b*b - 4*a*c /2 *a Incorrect
answer
Solution
}
Special Character Newline
\n
Introduction to Programming
Lecture 5
In the Previous Lecture
Basic structure of C program
Variables and Data types
Operators
‘cout’ and ‘cin’ for output and input
Braces
Decision
If Statement
If condition is true
statements
If (condition)
statement ;
If Statement in C
If ( condition )
{
statement1 ;
statement2 ;
:
}
If statement in C
if (age1 > age2)
cout<<“Student 1 is older
than student 2” ;
Relational Operators
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
!= not equal to
Relational Operators
a != b;
X = 0;
X == 0;
Example
#include <iostream.h>
main ( )
{
int AmirAge, AmaraAge;
AmirAge = 0;
AmaraAge = 0;
Process
Flow line
Continuation mark
Decision
Flow Chart for if statement
Entry point for IF block
IF
Condition
Then
Process
AND &&
OR ||
Logical Operators
If a is greater than b
AND c is greater than d
In C
if(a > b && c> d)
if(age > 18 || height > 5)
if-else
if (condition)
{
statement ;
-
-
}
else
{
statement ;
-
-
}
if-else Entry point for IF-Else block
IF
Condition
Then
Process 1
Else
Process 2
!true = false
!false = true
If (!(AmirAge > AmaraAge))
?
Example
?
Nested if
If (age > 18)
{
If(height > 5)
{
:
}
}
Make a flowchart of this nested if
structure…
In Today’s Lecture
Decision
– If
– Else
Flowcharts
Nested if
In the last lecture
Conditional Construct
if
if-else
Loop - Repetition
structure
Example
int sum ;
sum = 1+2+3+4+5+……..+10 ;
cout << sum ;
Find the Sum of the first 100
Integer starting from 1
?
while
while ( Logical Expression )
{
statements; :
}
int sum ;
sum = 0 ;
int sum = 0; ( Optional )
Example
int sum , number ;
sum = 0 ;
number = 1 ;
while ( number <= 1000 )
{
sum = sum + number ;
number = number + 1 ;
}
cout << “ The sum of the first 1000 integer starting from 1 is ” << sum ;
while (number <= UpperLimit)
Example
int sum, number , UpperLimit ;
sum = 0 ;
number = 1 ;
cout << “ Please enter the upper limit for which you want the sum ” ;
cin >> UpperLimi t;
while (number <= UpperLimit)
{
sum = sum + number ;
number = number +1 ;
}
cout << “ The sum of the first ” << UpperLimit << “ integer is ” << sum ;
if ( number % 2 == 0 )
{
sum = sum + number ;
number = number + 1 ;
}
Example
sum = 0;
number = 1;
cout << “ Please enter the upper limit for which you want the sum ”;
cin >> UpperLimit;
while (number <= UpperLimit)
{
if (number % 2 == 0)
{
sum = sum + number;
number = number + 1;
}
}
cout << “ The sum of all even integer between 1 and ” << UpperLimit << “ is” <<
sum;
2 * ( number / 2 ) ;
?
int Junk ;
Junk = 1 ;
while ( Junk <= UpperLimit ) ( infinite loop ) X
{
sum = sum + number ;
number = number + 1 ;
}
Flow Chart for While Construct
WHILE Statement
Condition is No
While Exit
true?
Process
n! = n*(n-1)*(n-2)*(n-3)…………*3*2*1
Example: Factorial
#include <iostream.h>
main ( )
{
int number ;
int factorial ;
factorial = 1 ;
cout << “Enter the number of Factorial” ;
cin >> number ;
while ( number >= 1 )
{
factorial = factorial * number ;
number = number – 1 ;
}
cout << “Factorial is” << factorial ;
}
Property of While
Statement
Lecture 7
while loop
while (condition)
{
statements; :
}
statements;
While loop executes zero
or more times. What if we
want the loop to execute
at least one time?
do-while
Do while loop execute on
or more times
Syntax of do-while loop
do
{
statements ;
}
while ( condition ) ;
Example-Guessing game
char c ;
int tryNum = 1 ;
do
{
cout << "Please enter your guess by pressing a character key from a to z “ ;
cin >> c ;
if ( c == 'z‘ )
{
cout << "Congratulations! you guessed the right answer“ ;
tryNum = 6 ;
}
else
tryNum = tryNum + 1 ;
} while ( tryNum <= 5 ) ;
Flow chart for do-while loop
Do-while
Process
false
condition Exit
true
Relational Operators
char c ;
int tryNum , maxTries ;
tryNum = 1 ;
maxTries = 5 ;
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) )
{
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
tryNum = tryNum + 1 ;
}
for Loop
For loop
Output
0123456789
Table for 2
2x1=2
2x2=4
2x3=6
:
:
2 x 10 = 20
Example - Calculate Table for 2
#include <iostream.h>
main ( )
{
int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}
}
Output
2 x1 = 2
2 x2=4
2 x3=6
:
:
2 x 10 = 20
Flow chart for the ‘Table’ example
Start
counter=1
While
No Exit
counter <=10?
yes
Print 2*counter
Counter =
counter + 1
Stop
Example: Calculate Table- Enhanced
#include <iostream.h>
main ( )
{
int number ;
int maxMultiplier ;
int counter ;
maxMultiplier = 10 ;
cout << " Please enter the number for which you wish to construct the table “ ;
cin >> number ;
for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )
{
cout << number <<" x " << counter<< " = " << number * counter << "\n“ ;
}
}
Always think re-use
Don’t use explicit constants
Increment operator
++
counter ++ ;
same as
counter = counter + 1;
Decrement operator
--
counter -- ;
same as
counter = counter - 1
+=
counter += 3 ;
same as
counter = counter + 3 ;
-=
counter -= 5 ;
same as
counter = counter – 5 ;
*=
x*=2
x=x*2
/=
x /= 2
x=x/2
Compound Assignment
Operators
operator=
%=
x %= 2 ;
same as
x=x%2;
Comments
Write comment at the top
program to show what it does
Write comments that mean some
thing
In today’s lecture
Do - while
– Executes the code at least ones
For loop
– Executes at least zero times
Short hand operators
– Incrementing
– Decrementing
Compound assignment operator