Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Programming and Data Structure
Sudeshna Sarkar
Lecture 7
1
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
main ()
{
int sum=0;
int input, inner, outer;
printf(“Input an integer : “);
scanf (“%d”, &input) ;
for (outer=1; outer <= input; outer++)
for (inner=0; inner < outer; inner++)
sum += inner;
printf (“The result is %d\n”, sum) ;
}
2
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Some Loop Pitfalls
while (sum <= NUM) ;
sum = sum+2;
for (i=0; i<=NUM; i++);
sum = sum+i;
for (i=1; i!=10; i=i+2)
sum = sum+i;
double x;
for (x=0.0; x<10.0; x=x+0.2)
printf(“%.18f”, x);
3
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Doubles and floats
What you expect:
0.000000000000000000
0.200000000000000000
0.400000000000000000
.. .. ..
9.000000000000000000
9.200000000000000000
9.400000000000000000
9.600000000000000000
9.800000000000000000
4
17.1.2001
What you may get:
0.000000000000000000
0.200000000000000000
0.400000000000000000
.. .. ..
8.999999999999999999
9.199999999999999999
9.399999999999999999
9.59999999999999999
9.799999999999999999
Sudeshna Sarkar, IIT Kharagpur
Use ints as loop counters
int i;
double x;
for (i=0; i<50; i=i+1)
{
x = (double)i/5.0;
printf (“%.18f”, x);
}
5
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Iteration Summary
General Pattern :
6
initialize
test
do stuff
update
go back to re-test, re-do stuff, re-update, ...
while and for are equally general in C
use for when initialize/test/update are simple,
especially when counting.
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Event Driven Programming
General Pattern :
Program starts, sets itself up.
Waits for some event or command to happen
7
mouse click, key click, timer, menu selection etc.
Program performs operation (“handles” the
command)
Program goes back to waiting.
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Simple Command Interpreter
Read in “commands” and execute them.
Input - single characters
Pseudocode for main loop:
8
a - execute command Add by calling Add()
s - execute command Sub by calling Sub()
q - quit
get next command
if a, execute command Add()
if b, execute command Sub()
if q, signal quit
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Command Interpreter
Loop Control
repeat until quit signal
use variable “done” to indicate when done
set done to false
while not done
body statements
if quit command, set done to true
9
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Command Interpreter program
switch (command) {
case ‘A’:
case ‘a’: Add();
break;
case ‘S’:
case ‘s’: Sub();
break;
case ‘Q’:
case ‘q’: done=TRUE;
}
#define FALSE 0
#define TRUE 1
int main (void)
{
char command;
int done = FALSE;
while (!done) {
printf (“Input command:”);
scanf(“%c”,&command);
}
}
10
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Exercise a
Write a C program which accepts as input a single
integer k, then writes a pattern consisting of a
single 1 on the first line, two 2s on the 2nd line,
three 3s on the 3rd line, until it writes k
occurrences of k on the last line.
For example, if the input is 4, the output should be:
1
22
333
4444
11
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Exercise b
Write a C program which accepts as input a single
integer k, then generates the following pattern of k
lines:
For example, if the input is 5, the output should be:
1
222
33333
4444444
555555555
12
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Test if a number is prime
prime = 1;
for (i=2; i<num; i++)
{
if (num%i == 0)
prime=0;
}
if (prime == 1)
printf (“%d” is a prime number\n”);
13
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Test if a number is prime
prime = 1;
limit = sqrt ((double)num);
for (i=2; i<limit; i++)
{
if (num%i == 0) {
prime=0;
break;
}
}
if (prime == 1)
printf (“%d” is a prime number\n”);
14
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Break and continue
These two statements are used in loop
control
“break” exits the innermost current loop (for,
while, do-while) and to exit from a switch
Control will be transferred out of the loop
“continue” starts the next iteration of the
loop (for, while, do-while)
15
used to bypass the remainder of the current pass
through a loop
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Error, neg x”);
break;
}
...
/*process non-neg x */
} while (x<=100);
16
for (count=0;count<n;count++)
{
...
while ((c=getchar()) != ‘\n’)
{
if (c==‘*’) break;
...
}
}
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Neg value forx”);
continue;
}
...
/*process non-neg x */
} while (x<=100);
17
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Ex: Write a loop that will calculate the sum of
an AP series upto n terms
Sum= a + (a+d) +(a+2d) + . . . + (a+ (n-1)d)
sum = a;
for (i=1; i<n; i++)
{
sum = sum +a+ i*d;
}
printf (‘%d”, sum);
18
sum = a;
term = a;
for (i=1; i<n; i++)
{
term = term + d;
sum = sum + term;
}
printf (‘%d”, sum);
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Exercise c
19
Write a C program that takes as input a
positive integer n, and prints all prime
numbers between 2 and n.
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
Exercise d
Write a C program that calculates the sum
of the first n odd numbers:
1 + 3 + 5 + . . . + 2*n-1
20
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
The sine of x can be calculated approximately
by summin the first n terms of the infinite series:
sin x = x - x3 /3! + x5 /5! – x7 /7! + . . .
where x is expressed in radians (p radians = 180
degrees).
Write a C program that will read in a value for x and will
calculate its sine.
(i) sum the first n terms
(ii)continue adding successive terms till the value of
the next term becomes smaller (in magnitude) than
10-5
21
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
scanf (“%f”, &x);
x = x*PI/180.0;
sineval = x;
term = x;
for (i=1; i<n; i++)
{
term = (-1)*term*x*x/(2*i*(2*i+1));
sineval = sineval + term;
}
22
17.1.2001
Sudeshna Sarkar, IIT Kharagpur
scanf (“%f”, &x);
x = x*PI/180.0;
sineval = x;
term = x;
for (i=1; term<0.00001; i++)
{
term = (-1)*term*x*x/(2*i*(2*i+1));
sineval = sineval + term;
}
printf (“The value of sine is %f to %d
terms\n”,sineval, i);
23
17.1.2001
Sudeshna Sarkar, IIT Kharagpur