lab 3: introduction to c programming

advertisement
DKT 121 – Basic Computer Programming
Laboratory Module
LAB 5
REPETITION STRUCTURES (LOOPS)
School of Computer and Communication Engineering
Universiti Malaysia Perlis
1
DKT 121 – Basic Computer Programming
Laboratory Module
1. OBJECTIVES:
1.1 To introduce the repetition structures; while, do-while, for and nested
loops statements.
1.2 To apply repetition structures as program control in writing C program.
1.3 To understand the counter control, sentinel control and flag control conditions.
1.4 To modify program according to user requirements.
2. INTRODUCTION:
In C language, there are several repetition (looping) statements or methods which can
be used:
• while loop
• do-while loop
• for loop
• Nested loop
2.1 while Loop
The syntax and flowchart for while loop are as below:
while (condition)
{
statement;
statement;
}
start
counter <=10
T
Display
counter
F
Counter = counter + 1
start
2.2 do-while Loop
The syntax and flowchart for do-while loop are as below:
A
do
{
statement;
statement;
}while (condition);
Instruction 1
T
Instruction 2
condition
F
2
B
DKT 121 – Basic Computer Programming
Laboratory Module
2.3 for Loop
The syntax for for loop is as below:
for (counter init; end condition; increment statement)
{
statement;
statement;
}
2.4 Nested Loop
The nested loop is when a loop is placed within a loop statement. It can be a while
loop within a while loop, a for loop within a for loop or a for loop within a
while loop as shown in the example below:
Nested Loop
char choice = ‘y’;
while (condition)
{
statement;
for (i =1; i< 10; i++)
statement;
printf(“continue? y or n: ”);
scanf(“%c”,&choice);
}
3
DKT 121 – Basic Computer Programming
Laboratory Module
3. TASKS:
3.1 Compile and run the loop program below:
#include <stdio.h>
int main ( )
{
int counter = 1; //declare and initialize variable
while (counter <= 10)
{
printf("%d\n",counter);
++counter;
}
//condition checking
//increment counter
return 0;
}
a. Write the output of the program.
b. What type of control is used in the above program? (counter control, sentinel
control or flag control)
c. Change the above program using a for loop statement.
4
DKT 121 – Basic Computer Programming
Laboratory Module
3.2 Based on the following grade chart, write a program to get the user to enter student
matrix number, test1, test2 and final marks and then total up the scores and determine
the student grade. The program should continue running until the user wants to stop. i.e
the program should prompt the user whether to continue or to stop checking for other
students’ grades.
Marks
>= 80
>= 60
>= 50
>= 40
< 40
a.
Grade
A
B
C
D
F
Write down your complete code here.
5
DKT 121 – Basic Computer Programming
Laboratory Module
3.3 Write a program to find the equivalent series and parallel resistance for a collection
of resistor values. Your program should scan first the number of resistors and then the
resistor values. Then compute the equivalent series resistance for all resistors in the
collection and also the equivalent parallel resistance. For example, if the 3 resistor
values are r1=100, r2=200 and r3=300 ohms, respectively, their equivalent series
resistance is r1 + r2 + r3 = 100 + 200 + 300 = 600 ohms and their equivalent parallel
resistance = 1 / (1/r1 + 1/r2 + 1/r3) = 1/(0.01+0.005+0.0033) = 54.55 ohms. (In previous
lab, you calculated the resistance for only three (3) resistors, now, the number of resistor is not fix
but is based on the user input)
6
DKT 121 – Basic Computer Programming
Laboratory Module
3.4 Write a program to find the largest number of a series of numbers entered (use these
numbers as your test data):
33, 10, 40, 30, 22, 29, 50, 69, 40, 67
The output of the program is as below:
Program to find the largest number
Please enter a number (type -999 to stop) : 33
Please enter a number (type -999 to stop) : 10
Please enter a number (type -999 to stop) : 40
Please enter a number (type -999 to stop) : 30
Please enter a number (type -999 to stop) : 22
Please enter a number (type -999 to stop) : 29
Please enter a number (type -999 to stop) : 50
Please enter a number (type -999 to stop) : 69
Please enter a number (type -999 to stop) : 40
Please enter a number (type -999 to stop) : 67
Please enter a number (type -999 to stop) : -999
Answer the largest number is 69
Thank you
7
DKT 121 – Basic Computer Programming
Laboratory Module
4. ADDITIONAL TASK:
4.1 Write a for loop which will produce the following output. (hint: use two nested
for loops).
(i) 1
22
333
4444
55555
(ii) 1.
1.2.
1.2.3.
1.2.3.4.
1.2.3.4.5.
8
Download