Presentation is loading. Please wait.

Presentation is loading. Please wait.

17.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7.

Similar presentations


Presentation on theme: "17.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7."— Presentation transcript:

1 17.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7

2 17.1.2001Sudeshna Sarkar, IIT Kharagpur 2 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) ; }

3 17.1.2001Sudeshna Sarkar, IIT Kharagpur 3 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);

4 17.1.2001Sudeshna Sarkar, IIT Kharagpur 4 Doubles and floats What you expect: 0.000000000000000000 0.200000000000000000 0.400000000000000000...... 9.000000000000000000 9.200000000000000000 9.400000000000000000 9.600000000000000000 9.800000000000000000 What you may get: 0.000000000000000000 0.200000000000000000 0.400000000000000000...... 8.999999999999999999 9.199999999999999999 9.399999999999999999 9.59999999999999999 9.799999999999999999

5 17.1.2001Sudeshna Sarkar, IIT Kharagpur 5 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); }

6 17.1.2001Sudeshna Sarkar, IIT Kharagpur 6 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.

7 17.1.2001Sudeshna Sarkar, IIT Kharagpur 7 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.

8 17.1.2001Sudeshna Sarkar, IIT Kharagpur 8 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

9 17.1.2001Sudeshna Sarkar, IIT Kharagpur 9 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

10 17.1.2001Sudeshna Sarkar, IIT Kharagpur 10 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; }

11 17.1.2001Sudeshna Sarkar, IIT Kharagpur 11 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 2 3 3 3 4 4

12 17.1.2001Sudeshna Sarkar, IIT Kharagpur 12 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 4 5 5 5 5 5 5 5 5 5

13 17.1.2001Sudeshna Sarkar, IIT Kharagpur 13 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”);

14 17.1.2001Sudeshna Sarkar, IIT Kharagpur 14 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”);

15 17.1.2001Sudeshna Sarkar, IIT Kharagpur 15 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) used to bypass the remainder of the current pass through a loop

16 17.1.2001Sudeshna Sarkar, IIT Kharagpur 16 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 17.1.2001Sudeshna Sarkar, IIT Kharagpur 17 do { scanf (“%f”, &x); if (x<0) { printf(“Neg value forx”); continue; }... /*process non-neg x */ } while (x<=100);

18 17.1.2001Sudeshna Sarkar, IIT Kharagpur 18 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);

19 17.1.2001Sudeshna Sarkar, IIT Kharagpur 19 Exercise c Write a C program that takes as input a positive integer n, and prints all prime numbers between 2 and n.

20 17.1.2001Sudeshna Sarkar, IIT Kharagpur 20 Exercise d Write a C program that calculates the sum of the first n odd numbers: 1 + 3 + 5 +... + 2*n-1

21 17.1.2001Sudeshna Sarkar, IIT Kharagpur 21 The sine of x can be calculated approximately by summin the first n terms of the infinite series: sin x = x - x 3 /3! + x 5 /5! – x 7 /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

22 17.1.2001Sudeshna Sarkar, IIT Kharagpur 22 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; }

23 17.1.2001Sudeshna Sarkar, IIT Kharagpur 23 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);


Download ppt "17.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7."

Similar presentations


Ads by Google