Download presentation
Presentation is loading. Please wait.
1
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
2
getchar, putchar and Buffers
3
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); ====
4
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); ====
5
Buffers Computers have Buffers – places for temporary storage
When we enter input to the program, it is stored in a buffer When our program gets a scanf or putchar command, it doesn’t really wait for our input – it looks in the buffer. Only if the buffer is empty it waits for input!!!
6
Example printf(“Please enter your age: ”); scanf(“%d”,&age);
Buffer printf(“Please enter your age: ”); scanf(“%d”,&age); printf(“Please enter you gender (M/F): ”); scanf(“%c”,&gender); If (gender==‘M’) printf(“You are a %d year old man!\n”); else if (gender==‘F’) printf(“You are a %d year old woman!\n”); else printf(“No such gender on earth!\n”); \n F 25\n \nF age 25 gender \n
7
Solution To tell scanf we expect an enter or space before the next character, we use: scanf(“ %c”,&gender); or: scanf(“\n%c”,&gender);
8
Loops – The Power of Repetition
C Programming Loops – The Power of Repetition
9
Loops Used to repeat the same instruction(s) over and over again.
Block of code Some changing state Loop while some condition holds
10
Loops C provides flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops.
11
while loops while (expr) { statement(s); }
true false while (expr) { statement(s); } The statements are executed as long as the condition is true. When the condition is no longer true, the loop is stopped.
12
Example - factorial Initialize Test Increment 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 <= n) fact *= i; ++i; } printf("the factorial is %d\n", fact); return 0; } Initialize Test Increment
13
Example - Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … Notice that we only need two elements in order to calculate the next one. 1 1 1 1 2 1 1 2 + + + +
14
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen
15
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1
16
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1
17
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1 1
18
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 8 Screen 1 1
19
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 8 Screen 1 1
20
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1
21
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1
22
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1 2
23
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 3 8 Screen 1 1 2
24
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 2 3 8 Screen 1 1 2
25
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen 1 1 2
26
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen 1 1 2
27
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen
28
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 5 8 Screen
29
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 3 5 8 Screen
30
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen
31
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen
32
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen
33
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 8 8 Screen
34
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 5 8 8 Screen
35
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen
36
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen
37
Fibonacci – step by step
int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen
38
Example – lower-case to upper case.
low2up.c
39
Low2up – step by step Buffer c upper_c Screen yeS\n ‘#’ ‘@’
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character. */ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer yeS\n c upper_c ‘#’ Screen
40
Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘@’
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ Screen
41
Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘@’
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ Screen
42
Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘Y’
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘Y’ Screen
43
Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘Y’ Y
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘Y’ Screen Y
44
Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y
45
Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y
46
Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y
47
Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘E’ Y
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘E’ Screen Y
48
Low2up – step by step c upper_c Screen S\n ‘e’ ‘E’ YE c = getchar();
while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); S\n c upper_c ‘e’ ‘E’ Screen YE
49
Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE
50
Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE
51
Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE
52
Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘S’ YE
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘S’ Screen YE
53
Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘S’ YES
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘S’ Screen YES
54
Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES
55
Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES
56
Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES
c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES
57
Exercise Input: Output: Note: Two integers – A and B
How many times A contains B This is the result of the integer division A/B Note: Do not use the division operator!
58
Solution int main() { int a = 0, b = 0, res = 0;
printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ((res + 1) * b <= a) ++res; printf("%d / %d = %d", a, b, res); return 0; }
59
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 loop. If called within a nested loop, break breaks out of the inner loop only.
60
Example – what does this do?
int main() { int i = 1; char c; printf("Enter a line of text:\n"); c = getchar(); while (c != '\n' && c >= 0) { if (c == ' ') break; ++i; } …
61
Example – Counting Letters
int main() { int i = 0; char c; printf("Enter a line of text:\n"); c = getchar(); while (c != '\n' && c >= 0) { if (c == ' ') /* found the first space - exit the loop */ break; i; } if (c== ' ') /* we found a space */ printf("There are %d letters before the first space.\n", i); else /* The loop ended without finding spaces */ printf("There are no spaces in the input line.\n"); return 0;
62
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.
63
What does it do? int main() { int i = 0; while (i < 10) { i++;
if (i % 2 == 0) break; printf("%d\n", i); } printf("The End\n"); return 0; } 1 The End
64
and this? 1 3 5 7 9 The End int main() { int i = 0;
while (i < 10) { i++; if (i % 2 == 0) continue; printf("%d\n", i); } printf("The End\n"); return 0; } The End
65
for loops for (<initialization>; <condition>; <increment>) { statement(s) } initialization condition statement(s) increment true false
66
The factorial example again, this time using for
#include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0;
67
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; --- 3 1
68
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 1 3 1
69
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 1 3 1
70
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 2 3 1
71
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 2 3 2
72
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 3 3 2
73
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 3 3 6
74
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 4 3 6
75
Factorial with for – step by step
n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 4 3 6
76
for loops (cont.) Equivalent to while. Any for loop can be converted to while loop and vice versa Some applications are more natural to for, and others to while. If we want to perform something for a predefined number of times, better use for. If we just wait for something to happen (not after a certain number or iterations), better use while.
77
Example – Fahrenheit-Celsius conversion table
int main ( ) { int fahr = 0; double celsius = 0.0; int lower = 0, upper = 300; int step = 20; for (fahr = lower; fahr <= upper; fahr += step) celsius = 5.0 * (fahr ) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;
78
Another way to do it… int main ( ) { int fahr = 0;
double celsius = 0.0; int lower = 0, upper = 300; int step = 20; for (fahr = lower; fahr <= upper; fahr ++) if (fahr%step != 0) continue; celsius = 5.0 * (fahr ) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;
79
Nested Loops The loop body can contain other loops
For each iteration of the outer loop we iterate the inner loop a number of times. while (...) { for(...) { } }
80
Nested Loops - example int main() { int row, col;
for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0;
81
Nested Loops - example row col --- --- int main() { int row, col;
for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; --- ---
82
Nested Loops - example row col 1 --- int main() { int row, col;
for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 ---
83
Nested Loops - example row col 1 1 int main() { int row, col;
for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 1
84
Nested Loops - example row col 1 1 2 3 4 int main() { int row, col;
for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 1 2 3 4
85
Nested Loops - example row col 1 5 output 1 2 3 4 5 int main() {
int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 5 output
86
Nested Loops - example row col 2 5 output 1 2 3 4 5 int main() {
int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 5 output
87
Nested Loops - example row col 2 1 4 2 3 output 1 2 3 4 5 int main() {
int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 1 4 2 3 output
88
Nested Loops - example row col 2 5 output 1 2 3 4 5 2 4 6 8 10
int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 5 output
89
do-while loops do { statement(s) } while (condition);
Similar to while loops Except the condition is evaluated after the loop The loop body is executed at least once, even if the expression is never true condition true false
90
Example – waiting for legal input
int main() { int i = 0; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) printf("Try again.\n"); } while (i <= 0); /* The program continues.... */ return 0; }
91
Exercise Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** **** *** ** *
92
Solution int main() { int i = 0, j = 0, size = 0;
printf("Please enter a size:\n"); scanf("%d",&size); for (i = 0; i < size; ++i) { for (j = 0; j < (size – i); ++j) printf("*"); printf("\n"); } return 0;
93
Exercise Write a program that accepts a number from the user, and prints out all of the prime numbers up to that number.
94
Solution int main() { int i = 0, j = 0, last = 0;
printf("enter a number\n"); scanf("%d", &last); for (i = 2; i <= last; ++i) for (j = 2 ; j < i; ++j) if (i % j == 0) break; } if (j == i) printf("the number %d is prime\n", i); return 0;
95
Exercise Change the former program, so that is displays only the largest prime number which is smaller than or equal to the user’s input.
96
Solution int main() { int i, j, num; printf("enter a number\n");
scanf("%d", &num); for (i = num; i > 1; --i) for (j = 2 ; j < i; ++j) if (i % j == 0) break; if (j == i) /* i is prime. We found our number */ } printf("The largest prime not larger than %d is %d.\n", num, i); return 0;
97
C Programming Arrays
98
Arrays A block of many variables of the same type
Array can be declared for any type E.g. int arr[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices
99
In Memory: The array variable holds the address in memory of the array beginning The n-th element of array arr is specified by arr[n-1] 1 2 3 4 5 6 7 8 9 int arr[10]; 5 21 arr arr[0]=5; arr[7]=21; arr[10]=3; Error!!!
100
Example - minimum #include <stdio.h> int main(void) {
int i, min, array[10]; printf("please enter 10 numbers:\n"); for(i = 0; i < 10; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < 10; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0;
101
Define Magic Numbers (like 10 in the last example) in the program convey little information to the reader Hard to change in a systematic way #define defines a symbolic name During preprocessing phase, symbolic names are replaced by the replacement text
102
minimum with #define Use capital letters #include <stdio.h>
#define ARRAY_SIZE 10 int main(void) { int i, min, array[ARRAY_SIZE]; printf("please enter %d numbers:\n", ARRAY_SIZE); for(i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < ARRAY_SIZE; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0; Use capital letters
103
Initialization Like in the case of regular variables, we can initialize the array during declaration. the number of initializers cannot be more than the number of elements in the array
104
Initialization The array size can be inferred from the number of initializers by leaving the square brackets empty These are identical declarations : int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
105
Exercise Write a program that gets 10 numbers from the user.
It then accepts another number and checks to see if that number was one of the previous ones. Example 1: Please enter 10 numbers: Please enter a number to search for: 8 I found it! Example 2: Please enter a number to search for: 30 Sorry, it’s not there.
106
Solution (simple_search.c)
#include <stdio.h> #define ARRAY_SIZE 10 int main(void) { int array[ARRAY_SIZE], i, num; printf("Please enter %d numbers:\n", ARRAY_SIZE); for (i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); printf("Please enter a number to search for:\n"); scanf("%d", &num); for (i = 0; i < ARRAY_SIZE; ++i) { if (array[i] == num) { printf("I found it!\n"); return 0; } printf("Sorry, it's not there\n");
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.