Morteza Noferesti - SHARIF UNIVERSITY OF...
Transcript of Morteza Noferesti - SHARIF UNIVERSITY OF...
![Page 1: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/1.jpg)
Morteza Noferesti
![Page 2: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/2.jpg)
Concept of algorithms
Understand and use three tools to represent algorithms:
◦ Flowchart
◦ Pseudocode
◦ Programs
![Page 3: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/3.jpg)
We want to solve a real problem by computers
◦ Take average, Sort, Painting, Web, Multimedia,..
We need a solution that
◦ Specifies how the real (complex) problem should be solved step-
by-step using the basic operations
The solution is the Algorithm of the problem
Characteristics
◦ Specific
◦ Unambiguous
◦ Language independent
![Page 4: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/4.jpg)
Algorithms are the problem solving steps/strategy in our
mind!!!
How can we document it (don’t forget it)?
How can explain/teach it to others peoples?
How can explain it to computers?
We need some methods to describe algorithms!
◦ Flow chart
◦ Pseudo-codes
◦ Codes/Programs
![Page 5: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/5.jpg)
A typical programming task can be divided into two
phases:
Problem solving phase◦ produce an ordered sequence of steps that describe solution
of problem
◦ this sequence of steps is called an algorithm
Implementation phase◦ implement the program in some programming language
![Page 6: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/6.jpg)
First produce a general algorithm (one can use pseudo code)
Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language.
![Page 7: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/7.jpg)
Pseudo code is an artificial and informal language
that helps programmers develop algorithms. Pseudo
code is very similar to everyday English.
Employs 'programming-like' statements to depict the
algorithm
No standard format (language independent)
![Page 8: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/8.jpg)
Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing.
The final grade is calculated as the average of four
marks.
![Page 9: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/9.jpg)
Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50
Print “FAIL”else
Print “PASS”
![Page 10: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/10.jpg)
Step 1: Input M1,M2,M3,M4
Step 2: GRADE M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then
Print “FAIL”else
Print “PASS”endif
![Page 11: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/11.jpg)
A graphical representation of the sequence of operations in an information system or program.
A Flowchart
◦ shows logic of an algorithm
◦ emphasizes individual steps and their interconnections
◦ e.g. control flow from one action to the next
![Page 12: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/12.jpg)
Oval
Parallelogram
Rectangle
Diamond
Hybrid
Name Symbol Use in Flowchart
Denotes the beginning or end of the program
Denotes an input operation
Denotes an output operation
Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Flow line Denotes the direction of logic flow in the program
![Page 13: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/13.jpg)
PRINT“PASS”
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
else
Print “PASS”
endif
START
InputM1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
ISGRADE<50
PRINT“FAIL”
STOP
YN
![Page 14: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/14.jpg)
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Pseudo code:
Input the length in feet (Lft)Calculate the length in cm (Lcm) by multiplying LFT with 30Print length in cm (LCM)
![Page 15: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/15.jpg)
Algorithm
Step 1: Input Lft
Step 2: Lcm Lft x 30
Step 3: Print Lcm
START
Input Lft
Lcm Lft x 30
Print Lcm
STOP
Flowchart
![Page 16: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/16.jpg)
A flowchart for the
pay-calculating program
START
Display message “How
many hours did you work?”
Read Hours
Display message “How
much do you get paid per
hour?”
Read Pay Rate
Multiply Hours by Pay
Rate. Store result in
Gross Pay.
Display Gross Pay
END
![Page 17: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/17.jpg)
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.
Pseudo code
Input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
Print A
![Page 18: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/18.jpg)
Algorithm
Step 1: Input W,L
Step 2: A L * W
Step 3: Print A
START
Input W, L
A L * W
Print A
STOP
![Page 19: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/19.jpg)
Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
Hint: d = sqrt ( ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
2 0ax bx c
2 4b ac
![Page 20: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/20.jpg)
Pseudo code:
Input the coefficients (a, b, c) of the quadratic equationCalculate dCalculate x1
Calculate x2
Print x1 and x2
![Page 21: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/21.jpg)
Algorithm: Step 1: Input a, b, c
Step 2: d sqrt ( )
Step 3: x1 (–b + d) / (2 x a)
Step 4: x2 (–b – d) / (2 x a)
Step 5: Print x1, x2
START
Input
a, b, c
d sqrt(b x b – 4 x a x c)
x1 ,x2
STOP
x1 (–b + d) / (2 x a)
X2 (–b – d) / (2 x a)
4b b a c
![Page 22: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/22.jpg)
The structure is as follows
If condition then
true alternative//S1
else
false alternative//S2
endifcondition
S2S1
Y N
![Page 23: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/23.jpg)
The expression A>B is a logical expression
if A>B is true (if A is greater than B) we take the action on left
◦ print the value of A
if A>B is false (if A is not greater than B) we take the action on right
◦ print the value of Bis
A>B
Print BPrint A
Y N
![Page 24: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/24.jpg)
Conditions by comparisons; e.g.,
If a is greater then b
If c equals to d
Comparing numbers: Relational Operators
![Page 25: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/25.jpg)
Relations produce a boolean value
int a, b;
bool bl; // #include <stdbool.h>
bl = a == b;
bl = a <= b;
C Boolean operators
and &&
or ||
not !
![Page 26: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/26.jpg)
p q p && q p || q !p
False False False False True
False True False True True
True False False True False
True True True True False
![Page 27: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/27.jpg)
Examples
bool a = true, b=false, c;
c = !a; //c=false
c = a && b; //c=false
c = a || b; //c=true
c = !a || b; //c=false
![Page 28: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/28.jpg)
Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.
ALGORITHMStep 1: Input VALUE1, VALUE2Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1else
MAX VALUE2endif
Step 3: Print “The largest value is”, MAX
![Page 29: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/29.jpg)
MAX VALUE1
“The largest value is”, MAX
STOP
Y N
START
Input
VALUE1,VALUE2
MAX VALUE2
is
VALUE1>VALUE2
![Page 30: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/30.jpg)
Display “x is
within limits.”
Display “x is outside
the limits.”
YESNOx > min?
x < max?
YESNO
Display “x is outside
the limits.”
![Page 31: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/31.jpg)
if (x > min){
if (x < max)printf("x is within the limits");
elseprintf("x is outside the limits");
}else
printf("x is outside the limits");
![Page 32: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/32.jpg)
Write and algorithm to
a) read an employee name (NAME), overtime hours
worked (OVERTIME), hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
>40 hours
>30 but 40 hours
>20 but 30 hours
>10 but 20 hours
10 hours
$50
$40
$30
$20
$10
![Page 33: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/33.jpg)
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) thenPAYMENT 50
else if (OVERTIME–(2/3)*ABSENT > 30) thenPAYMENT 40
else if (OVERTIME–(2/3)*ABSENT > 20) thenPAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) thenPAYMENT 20
else PAYMENT 10
endifStep 3: Print “Bonus for”, NAME “is $”, PAYMENT
![Page 34: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/34.jpg)
Algorithm: calculate 12 + 22 +⋯+ 𝑛2
Input: nOutput: sum
sum ← 0i ← 1Repeat the following three steps while i ≤ n:
sq ← i * isum ← sum + sqi ← i + 1
![Page 35: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/35.jpg)
![Page 36: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/36.jpg)
![Page 37: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/37.jpg)
The flowchart reads three numbers and prints the value
of the largest number.
![Page 38: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/38.jpg)
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) thenif (N1>N3) then
MAX N1 [N1>N2, N1>N3]
elseMAX N3 [N3>N1>N2]
endifelse
if (N2>N3) then MAX N2 [N2>N1, N2>N3]
elseMAX N3 [N3>N2>N1]
endifendif
Step 3: Print “The largest number is”, MAX
![Page 39: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/39.jpg)
#include <stdio.h>
int main(void){
int number_to_test, remainder;
printf("Enter your number to be tested: ");
scanf("%d", &number_to_test);
remainder = number_to_test % 2;
if(remainder == 0)
printf ("The number is even.\n”)
else
printf ("The number is odd.\n”)
return 0;
}
![Page 40: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/40.jpg)
Assign value according to conditions
A ternary operator
int i, j, k;
bool b;
...
i = b ? j : k; /*if(b)
* i=j
* else
* i=k;
*/
![Page 41: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/41.jpg)
y = abs(x)
y = (x > 0) ? x : -x;
signum = (x < 0) ? -1 : (x > 0 ? 1 : 0)
![Page 42: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/42.jpg)
int d = numg / 25;
charg = (d == 0) ? ‘D’ :
((d == 1) ? ‘C’ :
(d == 2) ? ‘B’ : ‘A’);
![Page 43: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/43.jpg)
Danger of assignment (=) and equality (==)
int a = 10;
int b = 20;
if(a=b) // Logical Error
Danger of similarity between C and mathematic
if(a < b < c)
if(a && b > 0)
// Logical
// Logical
Error
Error
![Page 44: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/44.jpg)
; is a null statement!
#include <stdio.h>int main() {
int num;printf("enter a number>>");scanf("%d",&num);if (num>100);
printf("This statement always is displayed!");
return 0;}
![Page 45: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/45.jpg)
Multiple conditions◦ If-else if-else if-….
Select from alternative values of a variable◦ switch-case
◦ Values should be constant not expression: i,
◦ Values & Variables should be int or char
![Page 46: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/46.jpg)
Each switch-case can be rewritten by If-else◦ if-else version of switch-case in the previous slide
if(variable == value1)}
<statements 1>
<statements 2>
}
else if(variable == value2){
<statements 2>
}
![Page 47: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/47.jpg)
If(variable == value1)
{
<statements 1>
}
else if(variable == value2)
{
<statements 2>
}
else{
<statements 3>
}
switch(variable){
case value1:
<statements 1>
break;
case value2:
<statements 2>
break;
default:
<statements 3>
}
![Page 48: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/48.jpg)
Get the operation (contains +,-,*,/,\) as a character
(variable name: op)!
Get two input numbers (variable names: num1, num2 ).
Compute and display the expression of:
num1 (op) num2
+-*
/or\
![Page 49: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/49.jpg)
Header file: <ctype.h>
Function Work Of Function
isalnum Tests whether a character is alphanumeric or not
isalpha Tests whether a character is aplhabetic or not
iscntrl Tests whether a character is control or not
isdigit Tests whether a character is digit or not
isgraph Tests whether a character is grahic or not
islower Tests whether a character is lowercase or not
isprint Tests whether a character is printable or not
ispunct Tests whether a character is punctuation or not
isspace Tests whether a character is white space or not
isupper Tests whether a character is uppercase or not
isxdigit Tests whether a character is hexadecimal or not
tolower Converts to lowercase if the character is in uppercase
toupper Converts to uppercase if the character is in lowercase
![Page 50: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/50.jpg)
switch(opr){
case '+':
res = opd1 + opd2;
break;
case '-':
res = opd1 - opd2;
break;
case '/':
res = opd1 / opd2;
break;
#include <stdio.h>
#include <stdlib.h>
int main(void){
int res, opd1, opd2;
char opr;
printf("Operand1 : ");
scanf("%d", &opd1);
printf("Operand2 : ");
scanf("%d", &opd2);
printf("Operator : ");
scanf(" %c", &opr);
![Page 51: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/51.jpg)
case '*':
res = opd1 * opd2;
break;
default:
printf("Invalid operator \n");
return -1;
}
printf("%d %c %d = %d\n", opd1, opr, opd2, res);
return 0;
}
![Page 52: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/52.jpg)
switch(variable){
case
case
value1:
value2:
<statements 1>
break;
case value3:
<statements 2>
}
If(
(variable == value1) ||
(variable == value2)
){
<statements 1>
== value3)
}
else if
(variable
{
<statements 2>
}
![Page 53: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/53.jpg)
if-else is more powerful than switch-case
switch-case is only for checking the values of a variable and the values must be constant
Some if-else cannot be rewritten by switch-case
double var1, var2;
if(var1 <= 1.1)
<statements 1>
if(var1 == var2)
<statements 2>
![Page 54: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/54.jpg)
bool b; //b = x && y
switch (x){
case 0:
b = 0;
break;
case 1:
switch(y){
case
case
0:
b = 0;
break;
1:
b = 1;
break;
}
break;
}
![Page 55: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/55.jpg)
All values used in case should be different
switch(i){ //Error
case 1:
…
case 2:
…
case 1:
…
![Page 56: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/56.jpg)
All values must be value, not expression of variables
switch(i){ //Error
case j:
…
case 2:
…
case k+10:
…
![Page 57: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/57.jpg)
While
Do while
For
![Page 58: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/58.jpg)
Example: Write a program that read 3 integer and compute average It is easy. 3 scanf, an addition, a division and, a printf
Example: Write a program that read 3000 integer and compute average ?? 3000 scanf !!!
Example: Write a program that read n integer and compute average N??? scanf
Repetition in algorithms
![Page 59: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/59.jpg)
A loop is a group of instructions the computer executes repeatedly while some loop-continuation condition remains true.
We have discussed two means of repetition:Counter-controlled repetition
Sentinel-controlled repetition
Counter-controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed.
Sentinel-controlled repetition is sometimes called indefinite repetition because it’s not known in advance how many times the loop will be executed.
![Page 60: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/60.jpg)
In counter-controlled repetition, a control variable is used
to count the number of repetitions.
Counter-controlled repetition requires:
The initial value of the control variable.
The increment (or decrement) by which the control variable is
modified each time through the loop.
The condition that tests for the final value of the control variable
(i.e., whether looping should continue).
![Page 61: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/61.jpg)
When we know the number of iteration
Average of 10 number
Initialize counter = 0
Initialize other variables
While (counter < number of loop repetition)
do something (e.g. read input, take sum)
counter = counter + 1
![Page 62: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/62.jpg)
Consider the following problem statement:A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.
![Page 63: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/63.jpg)
Sentinel values are used to control repetition when:
The precise number of repetitions is not known in advance, and
The loop includes statements that obtain data each time the loop
is performed.
The sentinel is entered after all regular data items have
been supplied to the program.
Sentinels must be distinct from regular data items.
![Page 64: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/64.jpg)
When we do NOT know the number of iteration
But we know, when loop terminates E.g. Average of arbitrary positive numbers ending with <0
Get first input as n
While (n is not sentinel)
do something (sum, …)
get the next input as n
if (there is not any valid input) then S1
else S2
![Page 65: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/65.jpg)
Consider the following problem:Develop a class averaging program that will process
an arbitrary number of grades each time the program is run.
One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value, or a flag value) to indicate “end of data entry.”
![Page 66: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/66.jpg)
The user types in grades until all legitimate grades have been entered.
The user then types the sentinel value to indicate that the last grade has been entered.
Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value.
![Page 67: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/67.jpg)
![Page 68: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/68.jpg)
Repetition is performed by loops
Put all statements to repeat in a loop
Don’t loop to infinity
Stop the repetition
Based on some conditions (counter, sentinel)
C has 3 statements for loops
while statement
do-while statement
for statement
![Page 69: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/69.jpg)
A loop tests a condition, and if the condition
exists, it performs an action. Then it tests the
condition again. If the condition still exists, the
action is repeated. This continues until the
condition no longer exists
x < y? Process A
YES
![Page 70: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/70.jpg)
while ( <expression> )
<statements>
![Page 71: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/71.jpg)
را از کاربر nبرنامه ای بنویسید که عدد .را چاپ کندnتا 0بگیرد و اعداد
<stdio.h>
int main(void){
int n, number;
number = 0;
printf("Enter n: ");
scanf("%d", &n);
while(number <= n){
number);printf("%d \n",
number++;
}
return 0;
}
![Page 72: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/72.jpg)
#include <stdio.h>
int main(void){
int negative_num, positive_num;
int number;
negative_num = positive_num = 0;
printf("Enter Zero to stop \n");
printf("Enter next number: ");
scanf("%d", &number);
while(number != 0){
if(number > 0)
positive_num++;
else
negative_num++;
printf("Enter next number: ");
scanf("%d", &number);
= %d\n", positive_num);
= %d\n", negative_num);
}
printf("The number of positive numbers
printf("The number of negative numbers
return 0;
}
![Page 73: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/73.jpg)
Consider a program segment designed to find the first power of 3 larger than 100.
When the following while repetition statement finishes executing, product will contain the desired answer:
product = 3;
while ( product <= 100 ) {product = 3 * product;
} /* end while */
![Page 74: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/74.jpg)
do
<statements>
while (<expression>);
![Page 75: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/75.jpg)
#include <stdio.h>
int main(void){
int n;
double number, sum;
printf("Enter n > 0: ");
scanf("%d", &n);
-1;}if(n < 1){printf("wrong input"); return
sum = 0;
number = 0.0;
do{
number++;
sum += number / (number + 1.0);
}while(number < n);
printf("sum = %f\n", sum);
return 0;
}
![Page 76: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/76.jpg)
#include <stdio.h>
int main(void){
int negative_num=0, positive_num=0;
int number;
printf("Enter Zero to stop \n");
do{
printf("Enter next number: ");
scanf("%d", &number);
if(number > 0)
positive_num++;
else if(number < 0)
negative_num++;
}while(number != 0);
numbers
numbers
= %d\n", positive_num);
= %d\n", negative_num);
printf("The number of positive
printf("The number of negative
return 0;
}
![Page 77: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/77.jpg)
for(<expression1>;<expression2>; <expression3>)
<statements>
![Page 78: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/78.jpg)
int x, sum, i;
sum = 0;
for (i = 1; i < 6; i++) {
scanf(“%d”,&x);
sum = sum + x;
}printf(“%d”,sum);
counter ← 1, sum ← 0
counter < 6
sum ← sum + n
false
true
counter++
output sum
input n
![Page 79: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/79.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
_
![Page 80: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/80.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
1
_
![Page 81: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/81.jpg)
num
1
_
Example:for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);printf(“have come to exit\n”);
![Page 82: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/82.jpg)
num
1
1 _
Example:for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);printf(“have come to exit\n”);
![Page 83: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/83.jpg)
Example:for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);printf(“have come to exit\n”);
num
2
1 _
![Page 84: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/84.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
2
1 _
![Page 85: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/85.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
2
1 2 _
![Page 86: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/86.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
3
1 2 _
![Page 87: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/87.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
3
1 2 _
![Page 88: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/88.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
3
1 2 3 _
![Page 89: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/89.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
4
1 2 3 _
![Page 90: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/90.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
num
4
1 2 3 _
![Page 91: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/91.jpg)
Example:
for (num = 1; num <= 3; num++ )
printf(“%d\t”, num);
printf(“have come to exit\n”);
4
1 2 3 have come to exit_
![Page 92: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/92.jpg)
int grade, count, i;
double average, sum;
sum = 0;
", (i + 1));
printf("Enter the number of students: ");
scanf("%d", &count);
for(i = 0; i < count; i++){
printf("Enter the grade of %d-th student:
scanf("%d", &grade);
sum += grade;
}
average = sum / count;
average);printf("The average of your class is %0.3f\n",
return 0;
}
![Page 93: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/93.jpg)
int n, number;
printf("Enter n: ");
scanf("%d", &n);
for(number = 1; number
if((number % 2) ==
<= n; number++)
0)
printf("%d \n", number);
return 0;
}
![Page 94: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/94.jpg)
int n, number;
printf("Enter n: ");
scanf("%d", &n);
for(number = 2; number <= n; number += 2)
printf("%d \n", number);
return 0;
}
![Page 95: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/95.jpg)
The following examples show methods of varying the control variable in a for statement.
Vary the control variable from 1 to 100 in increments of 1.
for ( i = 1; i <= 100; i++ )
Vary the control variable from 100 to 1 in increments of -1 (decrements of 1).
for ( i = 100; i >= 1; i-- )
Vary the control variable from 7 to 77 in steps of 7.
for ( i = 7; i <= 77; i += 7 )
Vary the control variable from 20 to 2 in steps of -2.
for ( i = 20; i >= 2; i -= 2 )
Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17.
for ( j = 2; j <= 17; j += 3 )
Vary the control variable over the following sequence of values: 44, 33, 22, 11, 0. for ( j = 44; j >= 0; j -= 11 )
![Page 96: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/96.jpg)
Expression1 and Expression3 can be any number of expressions
for(i = 0, j = 0; i < 10; i++, j--)
Expression2 at most should be a single expression
for(i = 0, j = 0; i < 10, j > -100; i++, j--)
//ERROR
Any expression can be empty expression
for(;i<10;t++)
for(;;)//infinite loop
![Page 97: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/97.jpg)
The three expressions in the for statement are optional.
for(;;)
One may omit expression1 if the control variable is initialized elsewhere in the program.
If expression2 is omitted, C assumes that the condition is true, thus creating an infinite loop.
expression3 may be omitted if the increment is calculated by statements in the body of the for statement or if no increment is needed.
![Page 98: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/98.jpg)
<statement> in loops can be empty
while(<expression>) ;
E.g.,
while(i++ <= n) ;
for(<expression1>; <expression2>;<expression3>);
E.g.,
for(i = 0; i < 10; printf("%d\n",i), i++) ;
![Page 99: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/99.jpg)
<statement> in loops can be loop itself
while(<expression0>)
for(<expression1>; <expression2>;<expression3>)
<statements>
for(<expression1>; <expression2>;<expression3>)
do
<statements>
while(<expression>);
![Page 100: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/100.jpg)
A program that takes n and m and prints
*** ….* (m * in each line)
*** ….*
…
*** ….* (n lines)
![Page 101: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/101.jpg)
int main(void){
int i, j, n, m;
printf("Enter n &
scanf("%d%d", &n,
m: ");
&m);
for(i = 0; i < n; i++){
for(j = 0; j < m; j++)
printf("*");
printf("\n");
}
return 0;
}
![Page 102: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/102.jpg)
A program that takes n and prints
*
**
*** (i * in i-th line)
*** ….* (n lines)
![Page 103: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/103.jpg)
#include <stdio.h>
int main(void){
int i, j, n;
printf("Enter n: ");
scanf("%d", &n);
i = 1;
while(i <= n){
for(j = 0; j < i; j++)
printf("*");
printf("\n");
i++;
}
return 0;
}
![Page 104: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/104.jpg)
A program that takes a number and generates the following
patternInput = 5
*
**
***
****
*****
****
***
**
*
for(i= 1; i <= n; i++){
for(j= 0; j < i-1;j++)
printf(" ");
for(j= 1; j <= i; j++)
printf("*");
printf("\n");
}
for(i=n-1; i >= 1; i--){
for(j= 1; j < i; j++)
printf(" ");
for(j = 1; j <= i; j++)
printf("*");
printf("\n");
}
![Page 105: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/105.jpg)
The break and continue statements are used to alter the flow
of control.
The break statement, when executed in a while, for,
do…while or switch statement, causes an immediate exit from
that statement.
Program execution continues with the next statement.
![Page 106: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/106.jpg)
Exit from loop based on some conditions
do{
scanf("%d", &a);
scanf("%d", &b);
if(b == 0)
break;
res = a / b;
printf("a /= %d\n", res);
}while(b > 0);
![Page 107: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/107.jpg)
![Page 108: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/108.jpg)
int i,j;
for(i=1; i<6;i++){
for(j =1; j<6;j++) {
printf("%d %d\n" , i,j);
if(j==3)
break;
}
}
![Page 109: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/109.jpg)
![Page 110: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/110.jpg)
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=sqrt(n); ++i)
{
if(!n%i)
{
flag=1;
break;
}
}
if (!flag)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
![Page 111: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/111.jpg)
Jump to end of loop and continue repetition
The continue statement, when executed in a while, for or do…whilestatement, skips the remaining statements in the body of that control statement and performs the next iteration of the loop.
do{
scanf("%f", &a);
scanf("%f", &b);
if(b == 0)
continue;
res = a / b;
printf("a / b= %f\n", res);
}while(a> 0);
![Page 112: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/112.jpg)
![Page 113: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/113.jpg)
When you know the number of repetition
Counter-controlled loops
Usually, for statements
When you don’t know the number of repetitions (sentinel loop)
Some condition should be check before startingloop
Usually, while statement
The loop should be executed at least one time
Usually, do-while statement
![Page 114: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/114.jpg)
Loop should terminate E.g., in for loops, after each iteration, we should approach to the
stop condition
for(i = 0; i < 10; i++) //OK
for(i = 0; i < 10; i--) //Bug
Initialize loop control variables
int i;
for( ; i < 10; i++) //Bug
![Page 115: Morteza Noferesti - SHARIF UNIVERSITY OF TECHNOLOGYce.sharif.edu/courses/96-97/1/ce153-6/resources/root/Slides... · Write an algorithm and draw a flowchart that will ... Write and](https://reader030.fdocuments.in/reader030/viewer/2022020204/5aeec0537f8b9a8b4c8b638b/html5/thumbnails/115.jpg)
Don’t modify forloop controller in loop body
for(i =
...
i--;
0; i < 10; i++){
//Bug}
Take care about wrong control conditions < vs. <=
= vs. ==
int b = 10;
while(a = b){//it means while(true)
scanf("%d",&a)
…
{