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
getchar, putchar and Buffers
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); ====
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); ====
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!!!
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
Solution To tell scanf we expect an enter or space before the next character, we use: scanf(“ %c”,&gender); or: scanf(“\n%c”,&gender);
Loops – The Power of Repetition C Programming Loops – The Power of Repetition
Loops Used to repeat the same instruction(s) over and over again. Block of code Some changing state Loop while some condition holds
Loops C provides flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops.
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.
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
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 2 3 5 8 1 1 2 3 5 8 1 1 2 3 5 1 1 2 3 5 8 13 1 1 2 3 5 8 13 21 1 1 2 3 5 8 13 1 1 2 3 5 1 1 2 3 1 1 1 1 2 1 1 2 1 1 2 3 + + + +
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
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
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
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
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
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
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
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
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
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
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
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
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
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 3
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 1 1 2 3
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 1 1 2 3
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 1 1 2 3
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 1 1 2 3
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 1 1 2 3 5
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 1 1 2 3 5
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 1 1 2 3 5
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 1 1 2 3 5
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 1 1 2 3 5
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 1 1 2 3 5
Example – lower-case to upper case. low2up.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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; }
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.
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; } …
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;
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.
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
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; } 1 3 5 7 9 The End
for loops for (<initialization>; <condition>; <increment>) { statement(s) } initialization condition statement(s) increment true false
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;
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
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
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
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
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
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
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
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
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
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.
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 - 32.0) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;
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 - 32.0) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;
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(...) { . . . } . . . }
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;
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; --- ---
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 ---
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
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
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 1 2 3 4 5
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 1 2 3 4 5
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 1 2 3 4 5
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 1 2 3 4 5 2 4 6 8 10
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
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; }
Exercise Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** **** *** ** *
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;
Exercise Write a program that accepts a number from the user, and prints out all of the prime numbers up to that number.
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;
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.
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;
C Programming Arrays
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
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!!!
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;
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
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
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
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};
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: 1 2 3 4 5 6 7 8 9 10 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.
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");