Sie sind auf Seite 1von 12

Birla Institute of Technology & Science, Pilani

Second Semester 2015-2016, Computer Programming [CS F111]


Week #4
Tutorial Sheet #1 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab Sec:______/_____

Section 1 Concept of Loops:


Loops causes program to execute the certain block of code repeatedly until some conditions are
satisfied, i.e., loops are used in performing repetitive work in programming.
Suppose you want to execute some code/s 10 times. Without loops you will end up writing the
same code repeatedly. Lets understand the meaning of it.
Example 1: Write a program to print integers starting from -3 to 3.
#include <stdio.h>
/* Program to print integers between -3 and 3 */
int main(){
int i = -3;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
i = i + 1;
printf("%d ", i);
return 0;
}
Now imagine if you have to print integers from -1000 to 1000. Is it possible to code in the above
fashion? Obviously no!
Hence loops are control structures using which you can perform the same
computation by writing that code/s only one time and repeat the execution 7 times.

In C there are three different kinds of loops. They are


1. While loop
2. For loop
3. Do-while loop

Section 2 While Loop


The flowchart of while loop will look as follows:

The while loop checks whether the test expression (often called condition of the loop) is true
or not. If it is true, code/s inside the body of while loop is executed, that is, code/s inside the
braces { } are executed. Then again the test expression is checked whether test expression is
true or not. This process continues until the test expression becomes false.
The corresponding syntax in C language is as follows
while(test expression){

statement1;
statement2;

So lets rewrite the code of the above program and appreciate that how it can help us saving the
effort to type same computation repeatedly.

Example 2: Write a program to print integers starting from -3 to 3.


#include <stdio.h>
/* Program to print integers between -3 and 3 */
int main(){
int i = -3;
while(i != 4){
printf("%d ", i);
i = i + 1;
}

return 0;
}
Lets try few more examples:
Example 3: Design the flowchart to print all the positive integers starting from 0 to a
user provided positive integer N.

Exercise 1: Write the C code for the example 3.


#include<stdio.h>
main()
{
int n,i=0;
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i=i+1;
}
}
Example 4 Write the flow chart to print the following pattern
*******
*******
*******
*******
*******

Example 5: Write the code for the example 4.


#include <stdio.h>
/* Program to print patterns*/
int main(){
int i = 0;
while(i < 5){
printf("******\n");
i = i + 1;
}
return 0;
}
Lets look into another example, where you have to compute the following expression
1 + 3 + 5 + 7 + . +(2n-1)
Now note that you can solve it mathematically and reduce it to a simpler expression of n. For a
fixed value of n we can get our desired answer. But we want to actually compute this summation
without using the mathematically computed expression.
Example 6. Write the flowchart to find the following summation.
S = 1 + 3 + 5 + 7 + + (2n-1)

Example 7 Write a program to compute the summation


S = 1 + 3 + 5 + 7 + + (2n-1)
#include <stdio.h>
/* Program to compute 1 + 3 + 5 +..+(2n-1)*/
int main(){
int i = 1, sum = 0, N;
printf( Please enter the value of N );
scanf(%d,&N);
while(i <= (2*n-1)){
sum = sum + i;
i = i + 2;
}
printf( Sum = %d, sum);
return 0;
}
So we have completed few examples and it is exercise time.
Exercise 2: Write a program to play the following two players game
Player 1 will provide a number to the program which Player 2 will not see. Make sure
that the provided number is between 1-10 else quit the program.
Next Player 2 will keep on guessing the numbers till there is a match with the number
provided by Player1.
Program will output the number of attempts made by Player2.
A) Draw the flowchart for this program
B) Write C code for the above problem
#include<stdio.h>
main()
{

int n,guess=1;
printf("Player1 please enter a number between 1-10");
scanf("%d",&n1);
if((n>=1) && (n<=10))
{
while(1)
{
printf("Player2 please enter a number between 1-10");
scanf("%d",&n2);
if(n1==n2)
break;
else
guess++;
}
}
else
{
printf("The number entered is out of range");
exit(0);
}
printf("Player2 took %d attempts", guess);
}
Exercise 3a: Write a flowchart to print the multiplication table of a given number

Exercise 3b: Write the C code for the above stated problem.
#include<stdio.h>
main()
{
int n,i=1;
printf("enter a number\n");
scanf("%d",&n);
while(i<=10)
{
printf("\n%d X %d = %d\n",n,i,n*i);
i++;
}
}

Birla Institute of Technology & Science, Pilani


Second Semester 2015-2016, Computer Programming [CS F111]
Week #4
Tutorial Sheet #2 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab Sec:______/_____

Section 2 Tracing a loop


An important aspect of programming is to be able to trace your program. By trace we mean that
the state (or value) of each variable should be tracked after each C statement. A correct tracing
is a great tool because it helps you to check the correctness of your program. Tracing while
statement can be tricky because the details of while statements can be quite complicated. Great
care is needed to make sure that unexpected results during tracing are due to a mistake in the
while-statement design and not due to erroneous tracing! A systematic, though tedious way to
trace while statements is to use the cross em out technique. For each iteration of the body of
the while, the values of the variables from the previous iteration are crossed out and new values
recorded beside the old.

Example 8 Trace the following piece of code where variables x, y, and m are of type
int.
Statements

Variable values
x = 21
y = 15
m = 2917

while (y != 0) {
x = 21 15 6
y = 15 6 3
m = 2917 6 3
m = x % y;
Statements

x = y;
while (i <= n) {
y = m;

c = a + b;

Variable
x = 21 15values
6
i=1;
y = 15 6 3
a=0;
m = 6 3 0
b=1;
c=5672;
x = 15 6 3
n=4;
y = 15 6 3
m =
i=
a=
x =
b=
y =
c=
m =
n=
x =
i=
y =
a=
m =
b=
c=
n=

a = b;
i=
a=
b=
c=
n=
b = c;
i=
a=
b=
c=
n=
i++;
i=
a=
b=
c=
n=
}
i=
a=
b=
c=
n=

6 3 0
15 6 3
6 3 0
6 3 0
3
0
0

Exercise 4
Complete
the tracing
table for the
given piece
of code

Section 2
Some
common
Pitfalls

If you
have
only
one

statement after the while then you may skip putting the curly braces just like in if
statements. If the loop condition is satisfied then that single statement will be executed.
For example
int x=0;
while( x < 20)
x = x + 5;
If you think later that some more statements needs to be added then you may commit
silly mistakes such as
int x=0, y=1;
while( x < 20)
x = x + 5;
y++;
whereas your intention was to write
int x=0, y=1;
while( x < 20){
x = x + 5;
y++;
}
Perhaps as a beginner programmer it is a better idea to always put curly braces even if
there is one single statement.

Note that there is no semicolon right after the while statement. If by mistake you
put a semicolon then the compiler will understand that there is only one single statement
after the while statement even though that statement is empty.
For example
int count = 0;
while(count != 10);
--(1)
{
count = count +1;
}
The semicolon in statement (1) will mean that there are no statements after the while
condition is satisfied and hence the loop will run forever.

One of the most common pitfalls is getting into an infinite loop. In the above given
example, since the value of count (also called loop variable) is not going to change
because of that semicolon in statement (1) the condition of the loop will always remain
true and hence the loop will not terminate. Such non terminating loops are called infinite
loops. Thus you should be always careful that the loop variable is changed inside the
body of the loop such that the loop terminates.

Exercise 5: Find errors in the following code snippets.


a)

int count = 0;
while(count != 10){
count = count + 3;
}
b)
int a = 2;
while( a = 2){
printf( Give a new value of a );
scan(%d , &a);
}
c)

int m = 100, n = 0;
while(n = 0);{
if (m < 10){
n= 2;
}
m = m 10;
}

Exercise time!
Exercise 6a Write a flowchart to find if a given number is prime.

Exercise 6b Write C code for the above stated problem

Exercise 7 Do the following by taking N as input. First attempt each question by using the pow
function provided in <math.h> library i.e. pow(2,a) will return the value of 2 a. Please note that
you have to use the flag lm while compiling your program if you have used math.h library.
a) WAP to find the sum of following series.
N

21a
a=0

b) Modify the above program (part a) to get the sum of following series:
N

a
3+2
a
2
a=0

c) Modify the above program (part b) to get the sum of following series:
N

(1)a 3+22a
a
a=0

Exercise 8: Write a program to print the following pattern.


*******
*
*
*
*
*
*******

*
*
*
*
*