Programming - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Programming

Description:

int i = 0, n = 0, fact = 1; printf('Enter a numbern'); scanf('%d', &n) ... putchar(char c) prints out the character inside the brackets. Requires including stdio.h ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 65
Provided by: csta3
Category:
Tags: fact | programming

less

Transcript and Presenter's Notes

Title: Programming


1
Programming
  • Loops

2
Incremental Operators
  • Used as a short-hand

i a ?? i i a i - a ?? i i -
a i a ?? i i a i / a ?? i i /
a i or i ?? i i 1 ?? i 1 i-- or
--i ?? i i 1 ?? i - 1
3
Loops
  • Used to repeat the same code segmaent over and
    over again.

Block of code Some changing state
Loop while some condition holds
4
Loops
  • C provides some flexible ways of deciding how
    many times to loop, or when to exit a loop.
  • for, while, do-while loops.

5
while loops
  • while (expression)
  • statement
  • The statement is executed as long as condition
    is true.
  • The loop terminates when the condition is no
    longer true.

6
Example - Factorial
  • int main()
  • int i 0, n 0, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • i 1 / this is the counter /
  • while (i lt n)
  • fact i
  • i
  • printf("the factorial is d\n", fact)
  • return 0

7
Example Fibonacci series
0 1 1 2 3 5 8 13 21 34
0 1
0 1 1 2 3 5 8 13 21
0 1 1 2
0 1 1
0 1 1 2 3 5 8 13 21 34



8
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
0
1
---
Screen
0
9
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib1 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
0
1
---
Screen
0
10
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
0
1
---
Screen
0 1
11
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
0
1
1
Screen
0 1
12
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
1
Screen
0 1
13
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
1
Screen
0 1
14
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
1
Screen
0 1
15
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
1
Screen
0 1 1
16
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
2
Screen
0 1 1
17
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
1
2
Screen
0 1 1
18
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
2
2
Screen
0 1 1
19
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
2
2
Screen
0 1 1
20
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
2
2
Screen
0 1 1 2
21
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
1
2
3
Screen
0 1 1 2
22
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
2
2
3
Screen
0 1 1 2
23
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
2
3
3
Screen
0 1 1 2
24
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
2
3
3
Screen
0 1 1 2
25
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
2
3
3
Screen
0 1 1 2 3
26
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
2
3
5
Screen
0 1 1 2 3
27
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
3
3
5
Screen
0 1 1 2 3
28
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
3
5
5
Screen
0 1 1 2 3
29
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
3
5
5
Screen
0 1 1 2 3
30
Fibonacci step by step
lim
fib1
fib2
fib_next
  • int fib1 0, fib2 1, fib_next 0,
    lim
  • ...
  • printf("d ", fib1)
  • while (fib2 lt lim)
  • printf("d ", fib2)
  • fib_next fib1 fib2
  • fib1 fib2
  • fib2 fib_next
  • printf("\n")

5
3
5
5
Screen
0 1 1 2 3
31
getchar
  • getchar() gets a single character from the user.
  • Requires including stdio.h
  • Returns a non-positive number on failure.
  • Similar to scanf.

char c c getchar()
char c scanf("c", c)
??
32
putchar
  • putchar(char c) prints out the character inside
    the brackets.
  • Requires including stdio.h
  • Similar to printf.

char c putchar(c)
char c printf("c", c)
??
33
Example - Copy
  • int main()
  • int c
  • c getchar()
  • while (c ! '\n')
  • putchar(c)
  • c getchar()
  • return 0

34
Exercise Low to Up
  • Read input from the user until end-of-line (\n)
    encountered.
  • Convert lowercase letters to uppercase ones.

35
Solution Low to Up
  • int main()
  • int c 0
  • printf ("Please enter a string ")
  • c getchar()
  • while (c ! '\n')
  • if (c gt 'a' c lt 'z')
  • putchar(c - 'a' 'A')
  • else
  • putchar(c)
  • c getchar()
  • putchar('\n')
  • return 0

36
break in a loop
  • When break is encountered, the loop is stopped
    regardless of whether the condition is still
    true.
  • The program then continues to run from the first
    line after the while loop.
  • If called within a nested loop, break breaks out
    of the inner loop only.

37
Example what does this do?
  • int main()
  • int i 0, c
  • printf("Enter a line of text\n")
  • c getchar()
  • while (c ! '\n')
  • if (c ' ')
  • break
  • i
  • c getchar()

38
Example Counting Letters
  • int main()
  • int counter 0, c
  • printf("Enter a line of text\n")
  • c getchar()
  • while (c ! '\n')
  • if (c ' ') / found the first space - exit
    the loop /
  • break
  • counter
  • c getchar()
  • if (c ' ') / we found a space /
  • printf("There are d letters before the
    first space.\n", counter)
  • else / The loop ended without
    finding spaces /
  • printf("There are no spaces in the input
    line.\n")
  • return 0

39
continue
  • When continue is encountered, the rest of the
    loop body is ignored.
  • The program then continues to run from the
    beginning of the loop.

40
What does it do?
  • int main()
  • int i
  • i 1
  • while (i lt 10)
  • if (i 2 0)
  • break
  • printf("d\n", i)
  • i
  • printf("The End\n")
  • return 0

1The End
41
and this?
  • int main()
  • int i
  • i 1
  • while (i lt 10)
  • if (i 2 0)
  • continue
  • printf("d\n", i)
  • i
  • printf("The End\n")
  • return 0

Infinite Loop!
42
for loops
  • for (ltinitializationgt ltconditiongt ltincrementgt)
        statement(s)

43
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

---
---
1
44
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

---
3
1
45
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

1
---
3
1
46
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

1
3
1
47
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

2
3
1
48
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

2
---
3
1
49
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

2
3
2
50
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

3
3
2
51
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

3
3
6
52
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

4
3
6
53
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

4
---
3
6
54
Example - Factorial Using for
i
n
fact
  • int main()
  • int i, n, fact 1
  • printf("Enter a number\n")
  • scanf("d", n)
  • for (i 1 i lt n i)
  • fact i
  • printf("the factorial is d\n", fact)
  • return 0

4
---
3
6
55
What does it do?
  • int main()
  • int i
  • for (i 1 i lt 10 i)
  • if (i 2 0)
  • break
  • printf("d\n", i)
  • printf("The End\n")
  • return 0

1The End
56
and this?
  • int main()
  • int i
  • for (i 1 i lt 10 i)
  • if (i 2 0)
  • continue
  • printf("d\n", i)
  • printf("The End\n")
  • return 0

13579The End
57
Nested Loops
  • Loops can be nested
  • For every time in the external loop we make a
    complete iteration of the internal one.

58
Example - Multiplication Table
  • int main()
  • int row, col
  • for (row 1 row lt 10 row)
  • for (col 1 col lt 10 col)
  • printf("3d", row col)
  • printf("\n")
  • return 0

59
Output
  • 1 2 3 4 5 6 7 8 9 10
  • 2 4 6 8 10 12 14 16 18 20
  • 3 6 9 12 15 18 21 24 27 30
  • 4 8 12 16 20 24 28 32 36 40
  • 5 10 15 20 25 30 35 40 45 50
  • 6 12 18 24 30 36 42 48 54 60
  • 7 14 21 28 35 42 49 56 63 70
  • 8 16 24 32 40 48 56 64 72 80
  • 9 18 27 36 45 54 63 72 81 90
  • 10 20 30 40 50 60 70 80 90 100

60
Example Reverse Triangle
  • int main()
  • int i 0, j 0
  • int size 0
  • printf("Please enter a size\n")
  • scanf("d", size)
  • for (i 0 i lt size i)
  • for (j i j lt size j)
  • printf("")
  • printf("\n")
  • return 0

Please enter a size 5
61
Exercise Prime Numbers
  • Write a program that prints all the prime numbers
    smaller than or equal to N.
  • N is given by the user (input)
  • How do you go over all the numbers lt N?
  • How do you know if a number is prime?

62
Solution Prime Numbers
  • int main()
  • int candidate 0, divisor 0, last 0
  • printf("Enter a number\n")
  • scanf("d", last)
  • for (candidate 2 candidate lt last
    candidate)
  • for (divisor 2 divisor lt candidate
    divisor)
  • if (candidate divisor 0)
  • break
  • if (divisor candidate)
  • printf("the number d is prime\n",
    candidate)
  • return 0

63
do-while loops
Statement
  • do
  • statement(s)
  • while (condition)
  • Similar to while loops
  • Except the condition is evaluated after the loop
    body
  • The loop body is always executed at least once,
    even if the expression is never true

condition
true
false
64
Example Waiting for Legal Input
  • int main()
  • int i 0
  • printf("Please enter a positive number.\n")
  • do
  • scanf("d", i)
  • if (i lt 0)
  • printf("Try again.\n")
  • while (i lt 0)
  • / The program continues.... /
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com