Lecture 5
Essentials of Repetition
Most C programs
involve in repetition or looping. A loop is a group of instructions the
computer executes while some loop continuation condition remains true. There are
two types of repetition.
1.
Counter-controlled repetition
2.
Sentinel-controlled repetition
Counter-controlled
repetition
Also called
definite repetition. We know in advance exactly how many times the loop will be
executed. A control variable is used to count the number of repetitions. The
control variable is incremented/decremented each time the group of instructions
is performed. When the value of the control variable indicates that correct
number of repetitions has been performed, the loop terminates and the computer
continues executing the statement after the repetition structure.
Sentinel-controlled
repetition
Also called
indefinite repetition. It is not known in advance how many times the loop will
be executed. It is particularly useful when-
a. The precise
number of repetition is not known in advance and
b. The loop
includes statements that obtain data each time the loop is performed.
The sentinel
value indicates “End of Data”. The sentinel is entered after all regular data
items have been supplied to the program. Sentinels must be distinct from
regular items.
The while Loop
The general form of while loop is as
follows-
initialize
loop counter;
while
(test loop counter using a condition){
do this;
and do that;
and this;
increment or decrement loop counter;
}
The following program prints number 1 to 10
using a while loop.
#include
<stdio.h>
#include
<conio.h>
void
main(){
int count=1; //initializing loop counter
while (count<=10){
printf (“I am in loop number %d”, count);
count++;//incrementing loop counter
}
}
Case Study 1
The lecturer of
CSE department has been assigned a very tough (!) task. He will have to count
total number of passes and fails for his class after taking the final exam. The
program will tell how many students passed and how many failed. Moreover, if
his class has number of passes over 80%, then we will call his class BEST.
Otherwise, we will ask him to try better next time.
Now, see, what
you will have to take care of:
1. Input: The
input of the program will be pass/fail only for each student. You will have to
collect the number of students from the user. This number of students will be
your loop control variable.
2. In Loop : Inside the loop, you will have to keep track of
number of fail and number of pass based on the input.
3. Output: You
will have to show number of passed students and number of failed students.
Also, you will have to see if the number of passed students is 80% or higher.
So, let’s get
started. See, what you will have to create:
1. Variables,
for holding number of students (will be used in loop condition), number of
pass, number of fail, loop counter, and the user input (pass/fail)
2. A loop.
Inside of the loop, you will have to have an if/else structure (if passed then
increase pass, else increase fail).
3. At the end of
the loop, you will have to have another if/else structure (if pass is above or
equal to 80% it is the BEST class, else the teacher will be asked for better
luck next time).
Simple! Now, we
will develop the pseudocode.
Header
files
main(){
initialize pass and fail with 0, result,
number of students, loop counter with 1;
ask the user for putting number of
students, take it into number of students variable;
while (loop counter <=number of
students){
ask the user to put result
(pass/fail), take it into result variable;
if result is pass
increment pass by one;
else
increment fail by one;
increment loop counter by
one;
}
print number of fail and pass;
if
((pass/number of students) >= 0.8)
print BEST;
else
print BETTER LUCK NEXT TIME;
}
Case Study 2
It will be a
similar program. The problem statement is as follows.
The user is
teacher once again. The user will give the program- number of students. Then he
will provide their marks. In the end, the program will respond with the average
marks. The program can really help a teacher to understand the merit of his
class.
Can you
write the pseudocode by yourself?
0 comments:
Post a Comment