Script started on Wed Oct 9 12:28:50 2024 mweeks@air:C_ch8$ echo 1989 1989 mweeks@air:C_ch8$ ls Oct9_2024.log maybe_infinite.c void_main.c checkTSize.c repdigit.c ident.c reverse.c mweeks@air:C_ch8$ cat ident.c #include #define N 10 int main() { double ident[N][N]; int row, col; for (row = 0; row < N; row++) for (col = 0; col < N; col++) if (row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0; return 0; } mweeks@air:C_ch8$ vi ident.c mweeks@air:C_ch8$ gcc ident.c ident.c:16:19: warning: invalid conversion specifier ' ' [-Wformat-invalid-specifier] printf("%5.2 ", ident[row][col]); ~~~~^ 1 warning generated. mweeks@air:C_ch8$ vi ident.c mweeks@air:C_ch8$ gcc ident.c mweeks@air:C_ch8$ cat ident.c #include #define N 10 int main() { double ident[N][N]; int row, col; for (row = 0; row < N; row++) for (col = 0; col < N; col++) if (row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0; for (row = 0; row < N; row++) { for (col = 0; col < N; col++) printf("%5.2f ", ident[row][col]); printf("\n"); } return 0; } mweeks@air:C_ch8$ ./a.out 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 mweeks@air:C_ch8$ cat repdigit.c /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* repdigit.c (Chapter 8, page 166) */ /* Checks numbers for repeated digits */ #include /* C99 only */ #include int main(void) { bool digit_seen[10] = {false}; int digit; long n; printf("Enter a number: "); scanf("%ld", &n); while (n > 0) { digit = n % 10; if (digit_seen[digit]) break; digit_seen[digit] = true; n /= 10; } if (n > 0) printf("Repeated digit\n"); else printf("No repeated digit\n"); return 0; } mweeks@air:C_ch8$ mweeks@air:C_ch8$ vi repdigit.c mweeks@air:C_ch8$ gcc repdigit.c mweeks@air:C_ch8$ cat repdigit.c /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* repdigit.c (Chapter 8, page 166) */ /* Checks numbers for repeated digits */ #include /* C99 only */ #include int main(void) { bool digit_seen[10] = {false}; int digit; long n; printf("Enter a number: "); scanf("%ld", &n); while (n > 0) { digit = n % 10; if (digit_seen[digit]) break; digit_seen[digit] = true; n /= 10; } if (n > 0) printf("Repeated digit\n"); else printf("No repeated digit\n"); // print y if a digit was seen printf("digits seen are:\n"); for (int i=0; i<10; i++) { if (digit_seen[i]) printf("y"); else printf("n"); } printf("\n"); return 0; } mweeks@air:C_ch8$ ./a.out Enter a number: 123 No repeated digit digits seen are: nyyynnnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 1232 Repeated digit digits seen are: nnyynnnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 4443 Repeated digit digits seen are: nnnyynnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 3221 Repeated digit digits seen are: nyynnnnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 321 No repeated digit digits seen are: nyyynnnnnn mweeks@air:C_ch8$ mweeks@air:C_ch8$ ls Oct9_2024.log ident.c reverse.c a.out maybe_infinite.c void_main.c checkTSize.c repdigit.c mweeks@air:C_ch8$ cp repdigit.c repdigit2.c mweeks@air:C_ch8$ vi repdigit2.c mweeks@air:C_ch8$ gcc repdigit2.c mweeks@air:C_ch8$ cat repdigit2.c /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* repdigit.c (Chapter 8, page 166) */ /* Checks numbers for repeated digits */ #include /* C99 only */ #include int main(void) { bool digit_seen[10] = {false}; int digit; long n; printf("Enter a number: "); scanf("%ld", &n); //long m = n; bool found_repeat = false; while (n > 0) { digit = n % 10; if (digit_seen[digit]) { found_repeat = true; } digit_seen[digit] = true; n /= 10; } if (found_repeat) printf("Repeated digit\n"); else printf("No repeated digit\n"); // print y if a digit was seen printf("digits seen are:\n"); for (int i=0; i<10; i++) { if (digit_seen[i]) printf("y"); else printf("n"); } printf("\n"); return 0; } mweeks@air:C_ch8$ ./a.out Enter a number: 123 No repeated digit digits seen are: nyyynnnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 1232 Repeated digit digits seen are: nyyynnnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 144442 Repeated digit digits seen are: nyynynnnnn mweeks@air:C_ch8$ ./a.out Enter a number: 142 No repeated digit digits seen are: nyynynnnnn mweeks@air:C_ch8$ cat reverse.c /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* reverse.c (Chapter 8, page 164) */ /* Reverses a series of numbers */ #include #define N 10 int main(void) { int a[N], i; printf("Enter %d numbers: ", N); for (i = 0; i < N; i++) scanf("%d", &a[i]); printf("In reverse order:"); for (i = N - 1; i >= 0; i--) printf(" %d", a[i]); printf("\n"); return 0; } mweeks@air:C_ch8$ gcc reverse.c mweeks@air:C_ch8$ ./a.out Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 In reverse order: 10 9 8 7 6 5 4 3 2 1 mweeks@air:C_ch8$ cd ../C_ch9 mweeks@air:C_ch9$ cat average_declaration.c /* Example Function Declaration 1: fn must be defined before the call */ #include double average(double a, double b) { return (a + b)/2; } int main() { // ... double x=1.0, y=3.0; average(x, y); return 0; } mweeks@air:C_ch9$ vi average_declaration.c mweeks@air:C_ch9$ gcc average_declaration.c mweeks@air:C_ch9$ cat average_declaration.c /* Example Function Declaration 1: fn must be defined before the call */ #include double average(double a, double b) { return (a + b)/2; } int main() { // ... double x=1.0, y=3.0; double avg; //average(x, y); avg = average(x, y); printf("x is %f and y is %f and the avg is %f\n", x, y, avg); return 0; } mweeks@air:C_ch9$ ./a.out x is 1.000000 and y is 3.000000 and the avg is 2.000000 mweeks@air:C_ch9$ cp average_declaration.c average_declaration_v2.c mweeks@air:C_ch9$ vi average_declaration_v2.c mweeks@air:C_ch9$ gcc average_declaration_v2.c mweeks@air:C_ch9$ cat average_declaration_v2.c /* Example Function Declaration 1: fn must be defined before the call */ #include double average(double a, double b) { double f = a; a = 5.0; // changing a does not change x return (f + b)/2; } int main() { // ... double x=1.0, y=3.0; double avg; //average(x, y); avg = average(x, y); printf("x is %f and y is %f and the avg is %f\n", x, y, avg); return 0; } mweeks@air:C_ch9$ ./a.out x is 1.000000 and y is 3.000000 and the avg is 2.000000 mweeks@air:C_ch9$ cp average_declaration_v2.c average_declaration_v3.c mweeks@air:C_ch9$ vi average_declaration_v3.c mweeks@air:C_ch9$ gcc average_declaration_v3.c mweeks@air:C_ch9$ cat average_declaration_v3.c /* Example Function Declaration 1: fn must be defined before the call */ #include double average(double *a, double b) { double f = *a; *a = 5.0; // changing a does change x return (f + b)/2; } int main() { // ... double x=1.0, y=3.0; double avg; //average(x, y); avg = average(&x, y); printf("x is %f and y is %f and the avg is %f\n", x, y, avg); return 0; } mweeks@air:C_ch9$ ./a.out x is 5.000000 and y is 3.000000 and the avg is 2.000000 mweeks@air:C_ch9$ vi memory.txt mweeks@air:C_ch9$ cat memory.txt memory byte values (hex) (dec) address value 0000 23 0001 34 0002 17 0003 9 ... 0201 0 x is here 0202 125 extends to next byte 0203 125 extends to next byte 0204 101 extends to next byte 0205 1 extends to next byte 0206 101 extends to next byte 0207 1 extends to next byte 0208 125 extends to next byte 0209 101 y is here 020A 1 extends to next byte 020B 4 ... FFFF 54 mweeks@air:C_ch9$ ls av* average_declaration.c average_declaration_v2.c average_declaration2.c average_declaration_v3.c mweeks@air:C_ch9$ cat average_declaration2.c /* Example Function Declaration 2: fn declared; can now go after the call */ #include double average(double x, double y); int main() { // ... average(x, y); return 0; } double average(double x, double y) { // ... } mweeks@air:C_ch9$ ls a.out memory.txt average_declaration.c myexit.c average_declaration2.c power.c average_declaration_v2.c power2.c average_declaration_v3.c prime.c fact.c qsort.c mweeks@air:C_ch9$ cat myexit.c #include #include /* needed for exit */ int main() { printf("I am going to exit with status code 42\n"); exit(42); } mweeks@air:C_ch9$ gcc myexit.c mweeks@air:C_ch9$ ./a.out I am going to exit with status code 42 mweeks@air:C_ch9$ cat power.c /* Args vs Params See Dr. King's book, chapter 9 */ #include int power(int x, int n) { int i, result = 1; for (i = 1; i <= n; i++) result = result * x; return result; } int main (int argc, char *argv[]) { printf("power(2,3) returns %d.\n", power(2,3)); return 0; } mweeks@air:C_ch9$ gcc power.c mweeks@air:C_ch9$ ./a.out power(2,3) returns 8. mweeks@air:C_ch9$ ls a.out memory.txt average_declaration.c myexit.c average_declaration2.c power.c average_declaration_v2.c power2.c average_declaration_v3.c prime.c fact.c qsort.c mweeks@air:C_ch9$ more power2.c /* Args vs Params See Dr. King's book, chapter 9 */ #include // Alternate form int power(int x, int n) { int result = 1; // n is a *copy* while (n-- > 0) result = result * x; return result; } int main (int argc, char *argv[]) { printf("power(2,3) returns %d.\n", power(2,3)); return 0; } mweeks@air:C_ch9$ gcc power2.c mweeks@air:C_ch9$ ./a.out power(2,3) returns 8. mweeks@air:C_ch9$ vi short_circuit.txt mweeks@air:C_ch9$ cat short_circuit.txt || or && these are done in a short-circuit way bool raining = true; if (raining || (have_umbrella)) printf("it is wet outside\n"); if (!raining && (walking)) printf("take umbrella\n"); mweeks@air:C_ch9$ exit exit Script done on Wed Oct 9 13:44:53 2024