Title: Programming and Data Structure
1Programming and Data Structure
Lecture 7
2- main ()
-
- int sum0
- int input, inner, outer
- printf(Input an integer )
- scanf (d, input)
- for (outer1 outer lt input outer)
- for (inner0 inner lt outer inner)
- sum inner
- printf (The result is d\n, sum)
3Some Loop Pitfalls
- while (sum lt NUM)
- sum sum2
for (i0 iltNUM i) sum sumi
for (i1 i!10 ii2) sum sumi
double x for (x0.0 xlt10.0 xx0.2)
printf(.18f, x)
4Doubles 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.200000000
000000000 0.400000000000000000 .. ..
.. 8.999999999999999999 9.199999999999999999 9.399
999999999999999 9.59999999999999999 9.799999999999
999999
5Use ints as loop counters
- int i
- double x
- for (i0 ilt50 ii1)
-
- x (double)i/5.0
- printf (.18f, x)
6Iteration 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.
7Event 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.
8Simple 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
9Command 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
10Command Interpreter program
switch (command) case A
case a Add()
break case S case s
Sub() break
case Q case q doneTRUE
- define FALSE 0
- define TRUE 1
- int main (void)
-
- char command
- int done FALSE
- while (!done)
- printf (Input command)
- scanf(c,command)
11Exercise 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 2
- 3 3 3
- 4 4 4 4
12Exercise 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
13Test if a number is prime
- prime 1
- for (i2 iltnum i)
-
- if (numi 0)
- prime0
-
- if (prime 1)
- printf (d is a prime number\n)
14Test if a number is prime
- prime 1
- limit sqrt ((double)num)
- for (i2 iltlimit i)
-
- if (numi 0)
- prime0
- break
-
-
- if (prime 1)
- printf (d is a prime number\n)
15Break 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- do
- scanf (f, x)
- if (xlt0)
- printf(Error, neg x)
- break
-
- . . .
- /process non-neg x /
- while (xlt100)
for (count0countltncount) . . .
while ((cgetchar()) ! \n)
if (c) break . . .
17- do
- scanf (f, x)
- if (xlt0)
- printf(Neg value forx)
- continue
-
- . . .
- /process non-neg x /
- while (xlt100)
18Ex Write a loop that will calculate the sum of
an AP series upto n terms
- Sum a (ad) (a2d) . . . (a (n-1)d)
sum a for (i1 iltn i) sum sum
a id printf (d, sum)
sum a term a for (i1 iltn i)
term term d sum sum
term printf (d, sum)
19Exercise c
- Write a C program that takes as input a positive
integer n, and prints all prime numbers between 2
and n.
20Exercise d
- Write a C program that calculates the sum of the
first n odd numbers - 1 3 5 . . . 2n-1
21The 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
22scanf (f, x) x xPI/180.0 sineval x
term x for (i1 iltn i) term
(-1)termxx/(2i(2i1)) sineval
sineval term
23scanf (f, x) x xPI/180.0 sineval x
term x for (i1 termlt0.00001 i)
term (-1)termxx/(2i(2i1))
sineval sineval term printf (The value of
sine is f to d terms\n,sineval,
i)