Upload
shona-hunt
View
219
Download
0
Embed Size (px)
Citation preview
17.1.2001 Sudeshna Sarkar, IIT Kharagpur1
Programming and Data Structure
Sudeshna Sarkar
Lecture 7
17.1.2001 Sudeshna Sarkar, IIT Kharagpur2
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) ;
}
17.1.2001 Sudeshna Sarkar, IIT Kharagpur3
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);
17.1.2001 Sudeshna Sarkar, IIT Kharagpur4
Doubles and floatsWhat you expect:0.0000000000000000000.2000000000000000000.400000000000000000.. .. ..9.0000000000000000009.2000000000000000009.4000000000000000009.6000000000000000009.800000000000000000
What you may get:
0.000000000000000000
0.200000000000000000
0.400000000000000000
.. .. ..
8.999999999999999999
9.199999999999999999
9.399999999999999999
9.59999999999999999
9.799999999999999999
17.1.2001 Sudeshna Sarkar, IIT Kharagpur5
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);
}
17.1.2001 Sudeshna Sarkar, IIT Kharagpur6
Iteration Summary
General Pattern : 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 Kharagpur7
Event Driven Programming
General Pattern : Program starts, sets itself up. Waits for some event or command to happen
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 Kharagpur8
Simple Command Interpreter Read in “commands” and execute them. Input - single characters
a - execute command Add by calling Add() s - execute command Sub by calling Sub() q - quit
Pseudocode for main loop: get next command if a, execute command Add() if b, execute command Sub() if q, signal quit
17.1.2001 Sudeshna Sarkar, IIT Kharagpur9
Command Interpreter Loop Control
repeat until quit signal
use variable “done” to indicate when done
set done to falsewhile not done body statements if quit command, set done to true
17.1.2001 Sudeshna Sarkar, IIT Kharagpur10
Command Interpreter program
#define FALSE 0
#define TRUE 1
int main (void)
{
char command;
int done = FALSE;
while (!done) {
printf (“Input command:”);
scanf(“%c”,&command);
switch (command) {
case ‘A’:
case ‘a’: Add();
break;
case ‘S’:
case ‘s’: Sub();
break;
case ‘Q’:
case ‘q’: done=TRUE;
}
}
}
17.1.2001 Sudeshna Sarkar, IIT Kharagpur11
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:12 23 3 34 4 4 4
17.1.2001 Sudeshna Sarkar, IIT Kharagpur12
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 2 2 2 3 3 3 3 3 4 4 4 4 4 4 45 5 5 5 5 5 5 5 5
17.1.2001 Sudeshna Sarkar, IIT Kharagpur13
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”);
17.1.2001 Sudeshna Sarkar, IIT Kharagpur14
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”);
17.1.2001 Sudeshna Sarkar, IIT Kharagpur15
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 switchControl will be transferred out of the loop
“continue” starts the next iteration of the loop (for, while, do-while)
used to bypass the remainder of the current pass through a loop
17.1.2001 Sudeshna Sarkar, IIT Kharagpur16
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Error, neg x”);
break;
}
. . .
/*process non-neg x */
} while (x<=100);
for (count=0;count<n;count++)
{
. . .
while ((c=getchar()) != ‘\n’)
{
if (c==‘*’) break;
. . .
}
}
17.1.2001 Sudeshna Sarkar, IIT Kharagpur17
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Neg value forx”);
continue;
}
. . .
/*process non-neg x */
} while (x<=100);
17.1.2001 Sudeshna Sarkar, IIT Kharagpur18
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);
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 Kharagpur19
Exercise c
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 Kharagpur20
Exercise d
Write a C program that calculates the sum of the first n odd numbers:1 + 3 + 5 + . . . + 2*n-1
17.1.2001 Sudeshna Sarkar, IIT Kharagpur21
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 ( 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
17.1.2001 Sudeshna Sarkar, IIT Kharagpur22
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;}
17.1.2001 Sudeshna Sarkar, IIT Kharagpur23
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);