├── .gitignore ├── Chapter02 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ └── ex10.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ ├── pp07.c │ └── pp08.c ├── Chapter03 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ └── ex06.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ └── pp06.c ├── Chapter04 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ └── ex07.c └── programming-projects │ └── pp01.c ├── Chapter05 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ └── ex11.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ ├── pp07.c │ └── pp08.c ├── Chapter06 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ └── ex14.c └── programming-projects │ ├── a.out │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ ├── pp07.c │ ├── pp08.c │ ├── pp09.c │ └── pp10.c ├── Chapter07 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex04.txt │ ├── ex05.txt │ ├── ex06.txt │ ├── ex07.txt │ ├── ex08.txt │ ├── ex09.txt │ ├── ex10.txt │ ├── ex11.txt │ ├── ex12.txt │ ├── ex13.txt │ ├── ex14.txt │ └── ex15.c └── programming-projects │ ├── .pp0.swp │ ├── .pp03.c.swp │ ├── .pp15.c.swp │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ ├── pp07.c │ ├── pp08.c │ ├── pp09.c │ ├── pp10.c │ ├── pp11.c │ ├── pp12.c │ ├── pp13.c │ ├── pp14.c │ └── pp15.c ├── Chapter08 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex04.txt │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ └── ex11.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ ├── pp07.c │ ├── pp08.c │ ├── pp09.c │ ├── pp10.c │ ├── pp11.c │ ├── pp12.c │ ├── pp13.c │ ├── pp14.c │ ├── pp15.c │ ├── pp16.c │ └── pp17.c ├── Chapter09 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.txt │ ├── ex08.txt │ ├── ex09.txt │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.c │ ├── ex15.c │ ├── ex16.c │ ├── ex17.c │ ├── ex18.c │ └── ex19.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ └── pp07.c ├── Chapter10 ├── exercises │ ├── ex01.c │ ├── ex01.txt │ ├── ex02.txt │ └── ex03.txt └── programming-projects │ └── pp01.c ├── Chapter11 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ └── ex08.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ └── pp04.c ├── Chapter12 ├── exercises │ ├── ex01.txt │ ├── ex03.txt │ ├── ex04.c │ ├── ex05.txt │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.c │ ├── ex15.c │ ├── ex16.c │ ├── ex17.c │ └── ex18.c └── programming-projects │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ └── pp05.c ├── Chapter13 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.txt │ ├── ex08.txt │ ├── ex09.txt │ ├── ex10.txt │ ├── ex11.c │ ├── ex12.c │ ├── ex13.c │ ├── ex14.txt │ ├── ex15.txt │ ├── ex16.c │ ├── ex17.c │ └── ex18.c └── programming-projects │ └── pp01.c ├── Chapter14 └── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.txt │ ├── ex04.txt │ ├── ex05.txt │ ├── ex06.c │ ├── ex07.txt │ ├── ex08.c │ └── ex09.c ├── Chapter15 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex04.txt │ ├── ex05.txt │ └── ex06.txt └── programming-projects │ ├── 01 │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── makefile │ ├── quote │ ├── word.c │ └── word.h │ ├── 02 │ ├── README.md │ ├── justify.c │ ├── line.c │ ├── line.h │ ├── makefile │ ├── quote │ ├── word.c │ └── word.h │ ├── 03 │ ├── README.md │ ├── makefile │ ├── qsort.c │ ├── quicksort.c │ └── quicksort.h │ └── 04 │ ├── README.md │ ├── makefile │ ├── readline.c │ ├── readline.h │ └── remind.c ├── Chapter16 ├── exercises │ ├── ex01.txt │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.c │ ├── ex09.c │ ├── ex10.c │ ├── ex11.txt │ ├── ex12.txt │ ├── ex13.txt │ ├── ex14.c │ ├── ex15.c │ ├── ex16.txt │ ├── ex17.txt │ └── ex18.c └── programming-projects │ └── pp01.c ├── Chapter17 ├── exercises │ ├── ex01.c │ ├── ex02.c │ ├── ex03.c │ ├── ex04.c │ ├── ex07.txt │ ├── ex08.c │ ├── ex09.txt │ ├── ex10.c │ ├── ex11.c │ ├── ex12.c │ ├── ex14.c │ ├── ex15.txt │ └── ex16.c └── programming-projects │ ├── 01 │ ├── inventory │ ├── inventory.c │ ├── makefile │ ├── readline.c │ └── readline.h │ ├── 02 │ ├── README.md │ ├── inventory │ ├── inventory.c │ ├── makefile │ ├── readline.c │ └── readline.h │ ├── 03 │ ├── README.md │ ├── inventory │ ├── inventory.c │ ├── makefile │ ├── readline.c │ └── readline.h │ └── 05 │ ├── README.md │ └── pp05.c ├── Chapter18 └── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex04.txt │ ├── ex05.txt │ ├── ex06.txt │ ├── ex07.txt │ └── ex08.txt ├── Chapter19 ├── exercises │ ├── ex01.h │ ├── ex02 │ │ ├── README.md │ │ ├── stack.h │ │ └── stack2.c │ ├── ex03 │ │ ├── a │ │ │ ├── README.md │ │ │ ├── client.c │ │ │ ├── makefile │ │ │ ├── queue.c │ │ │ └── queue.h │ │ └── b │ │ │ ├── README.md │ │ │ ├── client.c │ │ │ ├── makefile │ │ │ ├── queue.c │ │ │ └── queue.h │ ├── ex04 │ │ ├── a │ │ │ ├── README.md │ │ │ ├── client.c │ │ │ ├── makefile │ │ │ ├── stack.c │ │ │ └── stack.h │ │ └── b │ │ │ ├── README.md │ │ │ ├── client.c │ │ │ ├── makefile │ │ │ ├── stack.c │ │ │ └── stack.h │ └── ex06 │ │ ├── README.md │ │ ├── a │ │ ├── makefile │ │ ├── stackADT.c │ │ ├── stackADT.h │ │ └── stackclient.c │ │ ├── b │ │ ├── makefile │ │ ├── stackADT2.c │ │ ├── stackADT2.h │ │ └── stackclient.c │ │ └── c │ │ ├── makefile │ │ ├── stackADT.h │ │ ├── stackADT3.c │ │ └── stackclient.c └── programming-projects │ └── pp01 │ ├── client.c │ ├── makefile │ ├── stack.c │ └── stack.h ├── Chapter20 └── exercises │ ├── ex01.txt │ ├── ex02.c │ ├── ex04.c │ ├── ex05.c │ ├── ex06.c │ ├── ex07.c │ ├── ex08.txt │ ├── ex09.c │ ├── ex11.c │ ├── ex12.txt │ └── ex13.txt ├── Chapter21 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex05.txt │ ├── ex06.txt │ └── ex07.txt └── programming-projects │ └── pp01.c ├── Chapter22 ├── exercises │ ├── ex01.txt │ ├── ex02.txt │ ├── ex03.txt │ ├── ex05.txt │ ├── ex06.c │ ├── ex07.txt │ ├── ex09.txt │ ├── ex10.c │ ├── ex11.txt │ ├── ex12.txt │ ├── ex13.c │ ├── ex14.c │ ├── ex15.c │ └── ex16.c └── programming-projects │ ├── 08 │ ├── .pp08.c.swp │ ├── pp08.c │ ├── readline.c │ ├── readline.h │ └── stock.dat │ ├── pp01.c │ ├── pp02.c │ ├── pp03.c │ ├── pp04.c │ ├── pp05.c │ ├── pp06.c │ └── pp07.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.out 3 | *.gch 4 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | //Author: Ashley K. Smith 2 | //Solution to "C Programming: A modern approach" chapter 2, exericse 1 3 | 4 | #include 5 | 6 | int main(){ 7 | printf("Hello, world\n"); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 2. 3 | */ 4 | 5 | 6 | #include /* Preprocessor directive */ 7 | 8 | int main(){ 9 | printf("Parkinson's Law:\nWork expands so as to "); /* Statement */ 10 | printf("fill the time\n"); /* Statement */ 11 | printf("available for its completion.\n"); /* Statement */ 12 | return 0; /* Statement */ 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 2, exercise 3. 3 | * Condense the dwight.c program by (1) replacing the assignments to height, length and width with initializers and (2) removing th weight variable, instead 4 | * calculating (volume + 165) + 166 within the last printf 5 | */ 6 | 7 | #include 8 | 9 | int main(){ 10 | int height = 8, width = 12, length = 10, volume = height * length * width; 11 | printf("Dimesions: %dx%dx%d\n", length, width, height); 12 | printf("Volume (cubic inches): %d\n", volume); 13 | printf("Dimesional weight (pounds): %d\n", (volume + 165) / 166); 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 4. 3 | * Write a program that declares several int and flaot variables (Without initialising them) and then print their values. 4 | */ 5 | 6 | #include 7 | 8 | int main(void){ 9 | int y, o, d, a; 10 | float l, u, k, e; 11 | 12 | printf("%d\n", y); 13 | printf("%d\n", o); 14 | printf("%d\n", d); 15 | printf("%d\n", a); 16 | 17 | printf("%f\n", l); 18 | printf("%f\n", u); 19 | printf("%f\n", k); 20 | printf("%f\n", e); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 2. 3 | * Which of the following are not legal C identifiers: 4 | (a) 100_bottles INVALID 5 | (b) _100_bottles VALID 6 | (c) one__hundred__bottles VALID 7 | (d) bottles_by_the_hundred_ VALID 8 | */ 9 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 6. 3 | * Why is not a good idea for an identifier to contai more than one adjacent underscore (as in current__balance) for example. 4 | * Probably becuase it can be difficult to identify how may underscores are beig used in some editors. 5 | * 6 | */ 7 | 8 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 7. 3 | *Which of the following are keywords in C? 4 | (a) for RESERVED 5 | (b) if RESERVED 6 | (c) main 7 | (d) printf 8 | (e) while RESERVED 9 | */ 10 | 11 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 8. 3 | *How many tokens are there in the following statement? 4 | * answer=(3*q-p*q)/3; 5 | * 3 Identifiers 6 | * 6 Puncuation 7 | */ 8 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex09.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 9 3 | *Insert spaces between the tokens in exercise 8 to make the statemet easier to read 4 | * answer = (3 * q - p * p) / 3; 5 | */ 6 | -------------------------------------------------------------------------------- /Chapter02/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 2, exercise 10 3 | *In the dweight.c program, which spaces are essential. 4 | *The space between #include and 5 | *The space between int and main 6 | *The space between int and height 7 | *The space between return and 0. 8 | */ 9 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 1 3 | * Write a program that uses printf to display the following picture on the screen: 4 | 5 | * 6 | * 7 | * 8 | * * 9 | * * 10 | * 11 | 12 | */ 13 | 14 | #include 15 | 16 | int main(void){ 17 | printf(" *"); 18 | printf(" *"); 19 | printf(" *"); 20 | printf("* *"); 21 | printf(" * *"); 22 | printf(" *"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 2 3 | */ 4 | 5 | #include 6 | #define PI 3.14 7 | 8 | int main(void){ 9 | int radius = 10; 10 | float volume; 11 | 12 | radius = radius * radius * radius; 13 | 14 | volume = (4.0f / 3.0f) * PI * radius; 15 | printf("%0.2f\n", volume); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 3 3 | */ 4 | 5 | #include 6 | #define PI 3.14 7 | 8 | int main(void){ 9 | int radius; 10 | float volume; 11 | scanf("%d", &radius); 12 | radius = radius * radius * radius; 13 | 14 | volume = (4.0f / 3.0f) * PI * radius; 15 | printf("%0.2f\n", volume); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 4 3 | * Write a program that asks the uset to enter a dollar-and-cents amount, the displays the amount with 5% tax added: 4 | */ 5 | 6 | #include 7 | 8 | int main(void){ 9 | float input; 10 | printf("Enter an amount: $"); 11 | scanf("%f", &input); 12 | 13 | printf("With tax added: $%0.2f", input + (input / 100) * 5); 14 | printf("\n"); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp05.c: -------------------------------------------------------------------------------- 1 | /* Solution to "C Programming: A modern approach" chapter 2, programming project 5 2 | * Write a program that asks the user to enter a value for x and then dispays the value of the following polynomial: 3 | 3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 4 | * Test output, if n = 3, answer is 762. 5 | */ 6 | 7 | #include 8 | 9 | int main(void){ 10 | int n; 11 | printf("Please enter a value: "); 12 | scanf("%d", &n); 13 | printf("result is: %d", 3*(n*n*n*n*n)+2*(n*n*n*n)-5*(n*n*n)-(n*n)+7*n-6); 14 | printf("\n"); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 6 3 | * Modify the program of programming project 5 so that the polynomial is evaluated using the following foumula: 4 | ((((3x + 2) x- 5)x-1)x+8)x-6 5 | */ 6 | 7 | #include 8 | 9 | int main(void){ 10 | int n; 11 | printf("Please enter a value: "); 12 | scanf("%d", &n); 13 | printf("result is: %d", (((((3 * n + 2) * n - 5) * n - 1) * n + 7) * n - 6)); 14 | printf("\n"); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/programming-projects/pp07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 2, programming project 7 3 | * Write a program that asks the uset to enter a U.S. dollar amount and then shows how to pay 4 | * that amount using the smallest number of 20, 10, 5 and 1 dollar bills. 5 | */ 6 | 7 | #include 8 | 9 | int main(void){ 10 | int n, twenties, tens, fives, ones; 11 | printf("Please enter an amount: $"); 12 | scanf("%d", &n); 13 | 14 | twenties = n / 20; 15 | n = n % 20; 16 | printf("\n$20 bills: %d", twenties); 17 | 18 | tens = n / 10; 19 | n = n % 10; 20 | printf("\n$10 bills: %d", tens); 21 | 22 | fives = n / 5; 23 | n = n % 5; 24 | printf("\n $5 bills: %d", fives); 25 | 26 | ones = n / 1; 27 | n = n % 1; 28 | printf("\n $1 bills: %d", ones); 29 | 30 | printf("\n"); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter03/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /*Author: Ashley K. Smith 2 | *Solution to "C Programming: A modern approach" chapter 3, exercise 1. 3 | 4 | * What output do the following called of printf produce? 5 | (a) printf("%6d,%4d", 86, 1040); 6 | " 86,1040" a minimum of 6 numbers and a minimum of 4 numbers 7 | (b) printf("%12.5e", 30.253); 8 | "3.02530e+01" 9 | (c) printf("%.4f", 83.162); 10 | "83.1620" 4 points after the decimal point 11 | (d) printf("%-6.2g", .0000009979); 12 | "000000.00" the minus sign forces left justification, 6 before decimal point and 2 after. 13 | 14 | 15 | */ 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter03/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, exercise 2 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | float x = 10; 9 | //(a) Exponential notation: left justified in a field of size 8; one digit after the decimal point 10 | printf("%-8.1g", x); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Chapter03/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, exercise 4 3 | i = 10 4 | x = .3 (0.3) 5 | j = 5 6 | */ 7 | -------------------------------------------------------------------------------- /Chapter03/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, exercise 5 3 | x = 12.3 4 | i = 45 5 | j = .6 (0.6) 6 | */ 7 | -------------------------------------------------------------------------------- /Chapter03/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, exercise 6 3 | */ 4 | 5 | //It's not the same program, but the concept is the same. Adding a space after the first conversion specifier will allow 6 | //a space before and after the / 7 | 8 | 9 | #include 10 | 11 | int main(void){ 12 | int x = 0, j = 0; 13 | scanf("%d /%d", &x, &j); 14 | printf("\n%d %d\n", x, j); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter03/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, programming project 1 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int day, month, year; 9 | printf("Enter a date (mm/dd/yyyy)"); 10 | scanf("%d/%d/%d%", &day, &month, &year); 11 | printf("You entered the date %d%d%d", year, month, day); 12 | printf("\n"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Chapter03/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, programming project 2 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int itemNumber, day, month, year; 9 | float unitPrice; 10 | printf("Enter item number: "); 11 | scanf("%d", &itemNumber); 12 | printf("Enter unit price: "); 13 | scanf("%f", &unitPrice); 14 | printf("Enter purchase date (mm/dd/yyyy): "); 15 | scanf("%d/%d/%d", &month, &day, &year); 16 | printf("\n"); 17 | 18 | printf("Item\t\tUnit\t\tPurchase\n"); 19 | printf("\t\tPrice\t\tDate\n"); 20 | printf("%d", itemNumber); 21 | printf("\t\t$ %6g", unitPrice); 22 | printf("\t\t%-.2d/%-.2d/%-.4d", month, day, year); 23 | printf("\n"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter03/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, programming project 3 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int gsiPrefix, groupID, publisherCode, itemNo, checkNo; 9 | printf("Enter ISBN: "); 10 | scanf("%d-%d-%d-%d-%d", &gsiPrefix, &groupID, &publisherCode, &itemNo, &checkNo); 11 | printf("GSI prefix: %d\nGroup identifier: %d\nPublisher code: %d\nItem number: %d\nCheck digit: %d\n", gsiPrefix, groupID, publisherCode, itemNo, checkNo); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Chapter03/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, programming project 4 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int areaCode, firstHalf, secondHalf; 9 | printf("Enter phone number [(xxx) xxx-xxxx]: "); 10 | scanf("(%3d) %3d-%4d", &areaCode, &firstHalf, &secondHalf); 11 | printf("You entered %d.%d.%d\n", areaCode, firstHalf, secondHalf); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Chapter03/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 3, programming project 6 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int num1, denom1, num2, denom2, result_num, result_denom; 9 | 10 | printf("Enter two fractions seperated by a plus sign: "); 11 | scanf("%d/%d+%d/%d", &num1, &denom1, &num2, &denom2); 12 | 13 | result_num = num1 * denom2 + num2 * denom1; 14 | 15 | result_denom = denom1 * denom2; 16 | 17 | printf("The sum is %d/%d\n", result_num, result_denom); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 1 3 | 4 | (a) i = 5; j = 3; 5 | printf("%d %d", i / j, i % j); 6 | 7 | 1 1 8 | 9 | (b) i = 2; j = 3; 10 | printf("%d", (i + 10) % j); 11 | 12 | 0 13 | 14 | 15 | (c) i = 7; j = 8; k = 9; 16 | printf("%d", (i + 10) % k / j); 17 | 18 | 1 19 | 20 | 21 | (d) i = 1; j= 2; k = 3; 22 | printf("%d", (i + 5) % (j + 2) / k); 23 | 24 | (0.6666) 25 | 0 26 | 27 | */ 28 | 29 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 2 3 | 4 | If i and j are positive integers, does (-i) / j always have the same value as -(i / j)? 5 | justify your answer. 6 | 7 | Let i be 4, j be 2. 8 | -4 / 2 = -2 9 | -(4 / 2) = -2 10 | 11 | 12 | */ 13 | 14 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 3 3 | 4 | 5 | What is the value of each of the following expressions in C89? 6 | 7 | 8 | 9 | (a) 8 / 5 10 | 1 or 2 11 | 12 | (b) -8 / 5 13 | -1 or -2 14 | 15 | (c) 8 / -5 16 | -1 or -2 17 | 18 | (d) -8 / -5 19 | 1 or 2 20 | */ 21 | 22 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 4 3 | 4 | 5 | What is the value of each of the following expressions in C99? 6 | 7 | 8 | 9 | (a) 8 / 5 10 | 1 11 | 12 | (b) -8 / 5 13 | -1 14 | 15 | (c) 8 / -5 16 | -1 17 | 18 | (d) -8 / -5 19 | 1 20 | */ 21 | 22 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 5 3 | 4 | What is the value of each of the following expressions in c89? 5 | 6 | (a) 8 % 5 7 | 8 | 3 9 | 10 | (b) -8 % 5 11 | 12 | -3 13 | 14 | (c) 8 % -5 15 | 16 | 3 17 | 18 | (d) -8 % -5 19 | 20 | -3 21 | 22 | */ 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 6 3 | 4 | What is the value of each of the following expressions in c89? 5 | 6 | (a) 8 % 5 7 | 8 | 3 9 | 10 | (b) -8 % 5 11 | 12 | -3 13 | 14 | (c) 8 % -5 15 | 16 | 3 17 | 18 | (d) -8 % -5 19 | 20 | -3 21 | 22 | */ 23 | 24 | #include 25 | 26 | int main(void){ 27 | printf("\n%d\n", -8 % 5); 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter04/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 4, exercise 7 3 | 4 | */ 5 | -------------------------------------------------------------------------------- /Chapter04/programming-projects/pp01.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter04/programming-projects/pp01.c -------------------------------------------------------------------------------- /Chapter05/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 2 3 | 4 | Q: The following program fragments illustrate the relational and equality operators. Show the output produced by each assuming that i, j and k are int variables. 5 | 6 | (a) i = 10; j = 5; 7 | printf("%d", !i < j); 8 | 9 | (!i) = 0, 0 < j. 10 | The result is 1. 11 | 12 | (b) i = 2; j = 1; 13 | printf("%d", !!i + !j); 14 | 15 | !i = 0, !!i = 1. 16 | !j = 0. 0 + 1 = 1. 17 | The result is 1. 18 | 19 | (c) i = 5; j = 0; k = -5; 20 | printf("%d", i && j || k); 21 | 22 | The expression is the same as (i && j) || k 23 | i is a non-zero value so the expression is short circuited. i or k is a non-zero value. 24 | The result is 1. 25 | 26 | (d) i = 1; j = 2; k = 3; 27 | printf("%d", i < j || k ); 28 | 29 | The expression is the same as (i < j) || k 30 | k is a non zero value, so the result will be 1. 31 | 32 | */ 33 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 2 3 | * 4 | */ 5 | #include 6 | 7 | int main(void){ 8 | printf("wtf"); 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 5. 3 | 4 | Q: Is the following if statement legal? 5 | 6 | if (n >= 1 <=10) 7 | printf("n is between 1 and 10\n"); 8 | 9 | No, the correct statement is : 10 | if (n >= 1 && n <= 10){ 11 | expr1, ... 12 | } 13 | 14 | */ 15 | 16 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 6. 3 | 4 | Q: Is the following if statement legal? If so, what does it do when n is equal to 5. 5 | 6 | if (n == 1-10) 7 | printf("n is between 1 and 10\n"); 8 | 9 | This statement is perfectly legal, but n must be 9 for the condition to be satisfied. When n is equal to 5, nothing will happen. 10 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 7 3 | 4 | 5 | Q: What does the following statement print if i has the value 17?what does it print if i has the value -17? 6 | 7 | int i = 17; 8 | printf("%d\n", i >= 0 ? i : -i); 9 | 10 | This conditional expression is read as : "If i is greater than or equal to 0, print i, else print -i 11 | 12 | In the case that i = 17, the program will print i, which is 17. 13 | In the case that i = -17, the program will print -(-i) which is 17. 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 8 3 | */ 4 | 5 | Q: The following if statement is unneccessarily simplify it as much as possible. 6 | 7 | if (age >= 13) 8 | if(age <= 19) 9 | teenager = true; 10 | else 11 | teenager = false; 12 | else if (age < 13) 13 | teenager = false; 14 | 15 | A: 16 | 17 | if(age >= 13 && age <= 19){ 18 | teenager = true; 19 | } else { 20 | teenager = false; 21 | } 22 | 23 | Personally, I find the above to be simple and readable. The statement can also be simplified to a single line conditional expression below. 24 | 25 | teenager = age >= 13 && age <= 19 ? true : false; 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex09.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 9 3 | */ 4 | 5 | Q: Are the following if statements equivalent? If not, why not? 6 | 7 | if(score >= 90) 8 | printf("A"); 9 | else if(score >= 80) 10 | printf("B"); 11 | else if(score >= 70) 12 | printf("C"); 13 | else if(score >= 60) 14 | printf("D"); 15 | else 16 | print("F"); 17 | 18 | if(score < 60) 19 | printf("F"); 20 | else if (score < 70) 21 | printf("D"); 22 | else if (score < 80) 23 | printf("C"); 24 | else if (score < 90) 25 | printf("B"); 26 | else 27 | print("A); 28 | 29 | A: They are equivalent. 30 | -------------------------------------------------------------------------------- /Chapter05/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, exercise 10 3 | */ 4 | 5 | Q: What output does the following program fragment produce? (Assume that i is an integer variable). 6 | 7 | 8 | i = 1; 9 | switch (i % 3){ 10 | case 0: printf("zero"); 11 | case 1: printf("one"); 12 | case 2: printf("two"); 13 | } 14 | 15 | A: the result of i % 3 is one. Because there is no break points, the result is "onetwo" 16 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 1 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | int n; 8 | printf("Enter a number: "); 9 | scanf("%d", &n); 10 | printf("The number %d has ", n); 11 | if(n >= 0 && n <= 9){ 12 | printf("1 digit\n"); 13 | } else if(n >= 10 && n <= 99){ 14 | printf("2 digits\n"); 15 | }else if(n >= 100 && n <= 999){ 16 | printf("3 digits\n"); 17 | } else { 18 | printf("more than 3 digits\n"); 19 | } 20 | 21 | return 0; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 2 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | int hour, mins; 8 | printf("Enter a 24-hour time: "); 9 | scanf("%d:%d", &hour, &mins); 10 | printf("Equivalent 12-hour time: "); 11 | 12 | if((hour >= 12 && hour <= 23) && (mins >= 0 && mins <= 59)){ 13 | hour = hour - 12; 14 | printf("%d:%d PM\n", hour, mins); 15 | } else if ((hour >= 00 && hour <= 11) && (mins >= 0 && mins <= 59)){ 16 | printf("%d:%d AM\n", hour, mins); 17 | } else { 18 | printf("Invalid time, ensure format is hh:mm\n"); 19 | } 20 | 21 | 22 | 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 4 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | int windSpeed; 8 | printf("Enter a wind speed(knots) :"); 9 | scanf("%d", &windSpeed); 10 | 11 | printf("Speed(knots)\tDescription\n"); 12 | 13 | if (windSpeed < 1) 14 | printf("%d\t\tCalm\n", windSpeed); 15 | else if (windSpeed >= 1 && windSpeed <= 3) 16 | printf("%d\t\tLight Air\n", windSpeed); 17 | else if (windSpeed >= 4 && windSpeed <= 27) 18 | printf("%d\t\tBreeze\n", windSpeed); 19 | else if (windSpeed >= 28 && windSpeed <= 47) 20 | printf("%d\t\tGale\n", windSpeed); 21 | else if (windSpeed >= 48 && windSpeed <= 63) 22 | printf("%d\t\tStorm\n", windSpeed); 23 | else if (windSpeed > 63) 24 | printf("%d\t\tHurricane\n", windSpeed); 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 5 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | float tax; 8 | printf("Enter taxable income: $"); 9 | scanf("%f", &tax); 10 | 11 | if(tax < 750.00f) 12 | printf("Tax due: $%.2f\n", (tax / 100) * 1); 13 | else if (tax >= 750.00f && tax <= 2250.00f) 14 | printf("Tax due: $%.2f\n", ((tax / 100) * 2) + 7.50f); 15 | else if (tax >= 2250.00f && tax <= 3750.00f) 16 | printf("Tax due: $%.2f\n", ((tax / 100) * 3) + 37.50f); 17 | else if (tax >= 3750.00f && tax <= 5250.00f) 18 | printf("Tax due: $%.2f\n", ((tax / 100) * 4) + 82.50f); 19 | else if (tax >= 5250.00f && tax <= 7000.00f) 20 | printf("Tax due: $%.2f\n", ((tax / 100) * 5) + 142.00f); 21 | else 22 | printf("Tax due: $%.2f\n", ((tax / 100) * 6) + 230.00f); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 6 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total, checkDigit; 8 | 9 | printf("Enter the first (single) digit: "); 10 | scanf("%ld", &d); 11 | printf("Enter the first group of five digits: "); 12 | scanf("%ld %ld %ld %ld %ld", &i1, &i2, &i3, &i4, &i5); 13 | printf("Enter second group of five digits: "); 14 | scanf("%ld %ld %ld %ld %ld", &j1, &j2, &j3, &j4, &j5); 15 | printf("Enter the last digit (Single check digit): "); 16 | scanf("%d", &checkDigit); 17 | first_sum = d + i2 + i4 + j1 + j3 + j5; 18 | second_sum = i1 + i3 + i5 + j2 + j4; 19 | total = 3 * first_sum + second_sum; 20 | printf("Check digit: %d\n", 9 - ((total - 1) % 10)); 21 | 22 | if (checkDigit == (9 - ((total - 1) % 10))) 23 | printf("VALID\n"); 24 | else 25 | printf("INVALID\n"); 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 7 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | int yks, kaks, kol, nel, high, low; //Finnish and Estonian numbers, might aswell learn languages at the same time right? 9 | printf("Enter four integers: "); 10 | scanf("%d %d %d %d", &yks, &kaks, &kol, &nel); 11 | high = yks; 12 | low = yks; 13 | 14 | if (kaks > high){ 15 | high = kaks; 16 | } else if (kaks < low){ 17 | low = kaks; 18 | } 19 | 20 | if (kol > high){ 21 | high = kol; 22 | } else if (kol < low){ 23 | low = kol; 24 | } 25 | 26 | if(nel > high){ 27 | high = nel; 28 | } else if (nel < low){ 29 | low = nel; 30 | } 31 | 32 | printf("Largest: %d\n", high); 33 | printf("Smallest: %d\n", low); 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Chapter05/programming-projects/pp08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 8 3 | */ 4 | 5 | #include 6 | 7 | int main(void){ 8 | 9 | 10 | 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" Chapter 6, Exercise 3 | * 4 | * Q: What output does the following program fragment produce? 5 | * 6 | * i = 1; 7 | * while (i <= 128){ 8 | * printf("%d ", i); 9 | * i *= 2; 10 | * } 11 | * 12 | * A: The program fragment will produce the first 8 places of the base 2 number system (Binary). 13 | * "1 2 4 8 16 32 64 128" 14 | */ 15 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" Chapter 6, Exercise 3 | * 4 | * Q: What output does the following program fragment produce? 5 | * 6 | * i = 9384; 7 | * do { 8 | * printf("%d ", i); 9 | * i /= 10; 10 | * } while (i > 0); 11 | * 12 | * A: "9384 938 93 9" 13 | */ 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" Chapter 6, exercise 3 3 | * Q: What output does the following for statmet produce 4 | * for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1) 5 | printf("%d ", i); 6 | 7 | A: This for statement will print "5 4 3 2 1" 8 | */ 9 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 4 3 | 4 | Q: Which one of the following statements is not equivalent to the other two (Assuming that the loop 5 | bodies are the same) ? 6 | 7 | (a) for (i = 0; i < 10; i++) ... 8 | (b) for (i = 0; i < 10; ++i) ... 9 | (c) for (i = 0; i++ < 10; ) ... 10 | 11 | c is different as the increment statement is in the loops conditional statement and the increment statment 12 | is omitted. 13 | */ 14 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 5 3 | 4 | Q: Which one of the following statements is not equivalent to the other two (assuming that the loop 5 | bodies are the same) ? 6 | 7 | (a) while (i < 10) {...} 8 | (b) for (; i < 10; ) {...} 9 | (c) do {...} while (i < 10); 10 | 11 | a and b are the same, as the assignment statement and increment operator are ommited from b. 12 | c while similar, c will check its termination condition always after executing the loop body. 13 | a and b check before. 14 | */ 15 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 5 3 | 4 | Q: Translate the program fragment of exercise 1 into a single for statement 5 | */ 6 | 7 | //A: 8 | #include 9 | int main(){ 10 | double i; 11 | for(i = 1; i <= 128; i *=2) { printf("%d ", i); } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 5 3 | 4 | Q: Translate the program fragment of exercise 2 into a single for statement 5 | */ 6 | 7 | //A: 8 | #include 9 | int main(){ 10 | int i = 9384; 11 | for(; i > 0; i /= 10) { printf("%d ", i); } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 8 3 | 4 | Q: what output does the following for statement produce? 5 | 6 | for(i = 10; i >= 1; i /= 2) 7 | printf("%d ", i++); 8 | 9 | A: The loop will produce "10 5 3 2 1 1 1 1 1 1 1 1 ..." infinite loop. 10 | */ 11 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex09.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 9 3 | 4 | Q: translate the for statement into exercise 8 into an equivalent while statement. You will need 5 | one statement in adition to the while loop itself 6 | */ 7 | 8 | //A 9 | #include 10 | int main(){ 11 | int i = 10; 12 | while (i >= 1){ 13 | printf("%d ", i++); 14 | i /= 2; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 10 3 | 4 | Q: Show how to replace a continue statement by an equivalent goto statement. 5 | 6 | A: The program below will print even numbers starting with 20 and is annotated to show continue equivalence 7 | 8 | */ 9 | #include 10 | int main(){ 11 | int i; 12 | for(i = 20; i > 0; i--){ 13 | if(i % 2 != 0){ 14 | goto eol; 15 | //continue would go here 16 | } 17 | printf("%d \n", i); 18 | eol: ; //continue statement would transfer control here. 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex11.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 10 3 | 4 | Q: Show how to replace a continue statement by an equivalent goto statement. 5 | 6 | A: The program below will print even numbers starting with 20 and is annotated to show continue equivalence 7 | 8 | */ 9 | #include 10 | int main(){ 11 | int i; 12 | for(i = 20; i > 0; i--){ 13 | if(i % 2 != 0){ 14 | goto eol; 15 | //continue would go here 16 | } 17 | printf("%d \n", i); 18 | eol: ; //continue statement would transfer control here. 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex12.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 12 3 | 4 | Q: The following "prime-testing2 loop apeared in section 6.4 as an example: 5 | 6 | for(d = 2; d< n; d++) 7 | if(n % d == 0) 8 | break; 9 | 10 | This loop isn't very efficient. It's not nessessary to divide n by all numbers between 2 and n - 1 11 | to determine whether it's prime. In fact, we need only check divisors up the square root of n 12 | Modify the loop to take advantage of this fact. Hine: Don't try to compute the square root of n; 13 | instead compare d * d with n 14 | 15 | */ 16 | 17 | //A: 18 | #include 19 | 20 | int main(void) 21 | { 22 | int n, d; 23 | n = 29; 24 | for(d = 2; d * d <= n; d++){ 25 | if(d * d <=n) 26 | printf("%d is divisible by %d\n", n, d); 27 | else 28 | printf("%d is prime\n", n); 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex13.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 13 3 | 4 | Q: Rewrite the following loop so that its body is empty: 5 | 6 | for(n = 0; m > 0; n++) 7 | m /= 2; 8 | 9 | */ 10 | 11 | //A: 12 | #include 13 | int main(){ 14 | int m, n; 15 | m = 30; 16 | for(n = 0; m > 0; n++, m /= 2) 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Chapter06/exercises/ex14.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | Solution to "C Programming: A Modern Approach" Chapter 6, exericse 14 3 | 4 | Q: Find the error in the following program fragment and fix it 5 | 6 | for (n % 2 == 0); 7 | printf(n is even \n"); 8 | 9 | */ 10 | 11 | #include 12 | int main(){ 13 | int n = 10; 14 | if(n % 2 == 0){ 15 | printf("n is even\n"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter06/programming-projects/a.out -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 6, programming project 1 3 | 4 | Q: Write a program that finds the largest in a series of numbers entered by the user. The program must 5 | promp the userto enter numbers on by one. When the user enters 0 or a negative number the program must 6 | display the largest nonnegative number entered. 7 | 8 | */ 9 | 10 | //A: 11 | #include 12 | int main(){ 13 | float input = 1, highestVal = 1; 14 | while(input > 0){ 15 | printf("Enter a number: "); 16 | scanf("%f", &input); 17 | if(input > highestVal) 18 | highestVal = input; 19 | } 20 | printf("\nThe largest number entered was %.2f\n\n", highestVal); 21 | return(0); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 6, programming project 2 3 | 4 | Q: Write a program that asks the user to enter two integers, then calculates and displays their greatest 5 | common divisor (GCD). 6 | 7 | */ 8 | 9 | //A: 10 | #include 11 | int main(){ 12 | int m = 0, n = 0, tmp; 13 | 14 | printf("Enter two integers:"); 15 | scanf("%d %d", &m, &n); 16 | 17 | while(n != 0){ 18 | tmp = m % n; 19 | m = n; 20 | n = tmp; 21 | } 22 | printf("Great common divisor: %d\n", m); 23 | return(0); 24 | } 25 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 6, programming project 3 3 | * 4 | * Q: Modify the sum2.c program from section 7.1 to sum a series of double values. 5 | */ 6 | 7 | #include 8 | int main(){ 9 | int n, sum = 0; 10 | 11 | printf("This program sums a series of doubles. \n"); 12 | printf("Enter integers (0 to terminate): "); 13 | 14 | scanf("%d", &n); 15 | 16 | while(n != 0){ 17 | sum += n; 18 | scanf("%d", &n); 19 | } 20 | printf("The sum is: %d\n", sum); 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp05.c: -------------------------------------------------------------------------------- 1 | :/* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 6, programming project 3 3 | 4 | Q: Programmin project in chapter 4 asked you to write a proram that displays a two-figit number with its 5 | digits reversed. Generalize the program so that the number can have one, two, three or more digits. 6 | */ 7 | 8 | 9 | #include 10 | int main(){ 11 | int input; 12 | printf("Enter a number: "); 13 | scanf("%d", &input); 14 | 15 | while(input > 0){ 16 | printf("%d", input % 10); 17 | input = input / 10; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to chapter 6, programming project 6 3 | */ 4 | 5 | #include 6 | int main(){ 7 | int n, counter = 0, tmp, rmndr; 8 | scanf("%d", &n); 9 | printf("\n"); 10 | do{ 11 | counter+=1; 12 | tmp = counter * counter; 13 | 14 | //test even square 15 | rmndr = tmp % 2; 16 | if(rmndr == 0){ 17 | printf("%d\n", tmp); 18 | } 19 | } while (tmp < n); 20 | printf("\n"); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp07.c: -------------------------------------------------------------------------------- 1 | //Coming back to this, can't find the square3.c program 2 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to chapter 6, programming project 8 3 | */ 4 | 5 | #include 6 | 7 | int main(){ 8 | int daysInMonth, startOfWeek, i; 9 | 10 | printf("\nEnter number of days in month: "); 11 | scanf("%d", &daysInMonth); 12 | printf("\nEnter starting day of the week (1=Sun, 7=Sat): "); 13 | scanf("%d", &startOfWeek); 14 | printf("\n"); 15 | 16 | for(i = 1; i < startOfWeek; i++){ 17 | printf("\t"); 18 | } 19 | 20 | for(i = 1; i <= daysInMonth; i++){ 21 | printf("%d\t", i); 22 | if((startOfWeek + i - 1) % 7 == 0){ 23 | printf("\n"); 24 | } 25 | } 26 | printf("\n"); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp09.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A modern approach" chapter 6 programming project 9 3 | * 4 | */ 5 | 6 | #include 7 | 8 | int main(void){ 9 | float loanAmount, interestRate, monthlyPayment, balanceRemaining; 10 | int i, n; 11 | 12 | printf("Enter amount of loan: $"); 13 | scanf("%f", &loanAmount); 14 | printf("Enter interest rate: "); 15 | scanf("%f", &interestRate); 16 | printf("Enter monthly payment :"); 17 | scanf("%f", &monthlyPayment); 18 | printf("Enter the number of payments: "); 19 | scanf("%d", &i); 20 | 21 | interestRate = interestRate / 12; 22 | balanceRemaining = loanAmount; 23 | 24 | for(i = 1; i <= n; i++){ 25 | printf("\nBalance remaining after third payment: $%0.2f", balanceRemaining = balanceRemaining + ((balanceRemaining / 100) * interestRate) - monthlyPayment); 26 | } 27 | printf("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Chapter06/programming-projects/pp10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 5, programming project 5 3 | */ 4 | 5 | #include 6 | int main(void){ 7 | float tax; 8 | printf("Enter taxable income: $"); 9 | scanf("%f", &tax); 10 | 11 | if(tax < 750.00f) 12 | printf("Tax due: $%.2f\n", (tax / 100) * 1); 13 | else if (tax >= 750.00f && tax <= 2250.00f) 14 | printf("Tax due: $%.2f\n", ((tax / 100) * 2) + 7.50f); 15 | else if (tax >= 2250.00f && tax <= 3750.00f) 16 | printf("Tax due: $%.2f\n", ((tax / 100) * 3) + 37.50f); 17 | else if (tax >= 3750.00f && tax <= 5250.00f) 18 | printf("Tax due: $%.2f\n", ((tax / 100) * 4) + 82.50f); 19 | else if (tax >= 5250.00f && tax <= 7000.00f) 20 | printf("Tax due: $%.2f\n", ((tax / 100) * 5) + 142.00f); 21 | else 22 | printf("Tax due: $%.2f\n", ((tax / 100) * 6) + 230.00f); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 1 3 | 4 | Q: Give the decimal value of each of the following integer constants. 5 | 6 | (a) 077 7 | (b) 0x77 8 | (c) 0XABC 9 | 10 | A: 11 | 12 | (a) 077 is equal to 00111111 in base2, which is 63 in base10. 13 | (b) 0x77 is equal to 01110111 in base2, which is 119 in base10. 14 | (c) 0XABC is equal to 101010111100 in base2, which is 2748 in base 10. 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 2 3 | 4 | Q: Which of the following are not legal constants in C? Classify each legal constant 5 | as either integer or floating point. 6 | 7 | (a) 010E2 8 | Could be Octal, but due to the exponent the number must be a legal base10 floating point 9 | 10 | (b) 32.1E+5 11 | Legal floating point 12 | 13 | (c) 0790 14 | An octal value as it starts witha 0, but is illegal because 9 is outside base8 range 15 | 16 | (d) 100_000 17 | Illegal 18 | 19 | (e) 3.98e-2 20 | Legal floating point 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 3 3 | 4 | Q: Which of the following are not legal tpes in C? 5 | 6 | (a) short unsigned int 7 | Legal 8 | 9 | (b) short float 10 | Illegal 11 | 12 | (c) long double 13 | legal 14 | 15 | (d) unsigned long 16 | Legal 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex04.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 4 3 | 4 | Q: If c is a variable of type char , which one of the following statements is illegal 5 | 6 | (a) i += c; 7 | (b) c = 2 * c - 1; 8 | (c) putchar(c); 9 | (d) printf(c); 10 | 11 | A: 12 | d is illegal. A legal usage would be printf("%c", c); to print the character stored in c 13 | 14 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 5 3 | 4 | Q: Which one of the following is not a legal way to write the number 65? 5 | (Assume that the character set is ASCII). 6 | 7 | (a) 'A' 8 | According to asciitable.com a captital 'A' is equal to 65 and is legal. 9 | 10 | (b) 0b1000001 11 | Illegal, cannot represent 65 using binary in C. 12 | 13 | (c) 0101 14 | Legal, 0101 is an base8 number which is 01000001 in base2, or 65 in base10 15 | 16 | (d) 0x41 17 | Legal, 0x41 is equal to 01000001 in base2, which is 65 in base10 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex06.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 6 3 | 4 | Q: For each of the following items of data, specify which one of the types 5 | char, short, int or long is the smallest one guaranteed to be large enough to store the item 6 | 7 | (a) Days in a month 8 | char 9 | 31 base10, 11111 base2 10 | 11 | (b) Days in a year 12 | short 13 | 365 base10, 101101101 base2 14 | 15 | (c) Minutes in a day 16 | short 17 | 1,440 base10, 10110100000 base2 18 | 19 | (d) Seconds in a day 20 | long 21 | 86,400 base10, 10101000110000000 base2 22 | 23 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 7 3 | 4 | Q: For each of the following character escapes, give the equivalent octal escape 5 | (Assume that the character set is ASCII.) 6 | 7 | (a) \b 8 | The escape character for backspace is \010 or \10 9 | 10 | (b) \n 11 | The escape character for new line is \012 or \12 12 | 13 | (c) \r 14 | The escape character for a carriage return is \015 or \15 15 | 16 | (d) \t 17 | The escape character for a horizontal tab is \011 or \11 18 | 19 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex08.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 8 3 | 4 | Q: Repeat exercise 7, but give the equivalent hexadecimal escape 5 | 6 | (a) \b 7 | \x08 8 | 9 | (b) \n 10 | \x0a 11 | 12 | (c) \r 13 | \x0d 14 | 15 | (d) \t 16 | \x09 17 | 18 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex09.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 9 3 | 4 | Q: Suppose that i and j are variables of type int. What is the type of the expression 5 | i / j + 'a'? 6 | 7 | A: Chars are treated as integers, the type of the expression is int. 8 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex10.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 10 3 | 4 | Q: Suppose that i is a variable of type int, j is a variable of type long, and k 5 | is a variable of type unsigned int. What is the expression i + (int)j * k 6 | 7 | K is the largest variable after j has been cast. Usual arithmetic conversion rules 8 | dictate that i and j be promoted to K. The type of the expression is unsigned int. 9 | 10 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex11.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 11 3 | 4 | Q: Suppose that i is a variable of type int. f is variable of type float, and d 5 | is a variable of type double. What is the type of the expression i * f / d? 6 | 7 | The largest type in this example is double, the float and int will be promoted 8 | to doubles to resolve the expression. 9 | 10 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex12.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 12 3 | 4 | Q: Suppose that i is a variable of type int, f is a variable of type float, and d is a 5 | variable of tpe double. Explain what conversions take place during the execution of the 6 | following statement: d = i + f; 7 | 8 | A: Assignment conversion rules dictate that the expression on the right is converted to 9 | the type on the left. In this case the result is a double. 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex13.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 13 3 | 4 | Q: Assume that a program contains the following declarations: 5 | 6 | char c = '\1'; 7 | short s = 2; 8 | int i = -3; 9 | long m = 5; 10 | float f = 6.5f; 11 | double d = 7.5; 12 | 13 | Give the value and the type of each expression listed below: 14 | 15 | (a) c * i (c) f / c (e) f - d 16 | (b) s + m (d) d / s (f) (int)f 17 | 18 | A: 19 | 20 | (a) -3, int (c) 6.5, float (e) -1.0, double 21 | (b) 7, long (d) 3.75, double (f) 6, int 22 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex14.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 7, exercise 14 3 | : 4 | Q: Does the following statement always compute the fractional part of f correctly 5 | (assuming that f and frac_part are float variables)? 6 | 7 | float frac_part, f; 8 | frac_part = f - (int)f; 9 | 10 | if not, what's the problem? 11 | 12 | A: Sort of, the order of presendence (cast, sub, assign) makes the result a float value of 0.6. 13 | (5.6 - 5) 14 | -------------------------------------------------------------------------------- /Chapter07/exercises/ex15.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, exercise 15 3 | * 4 | * Q: Use typedef to create types named Int8, Int16, and Int32. Define the types 5 | * so that they represent 8-bit, 16-bit and 32-bit integers on your machine 6 | * */ 7 | 8 | #include 9 | int main(){ 10 | //Using the standard library, could also include stdint.h and use int8_t etc 11 | typedef char Int8; 12 | typedef short int Int16; 13 | typedef long int Int32; 14 | 15 | printf("Bit length of Int8: %zu", sizeof(Int8) * 8); 16 | printf("\nBit length of Int16: %zu", sizeof(Int16) * 8); 17 | printf("\nBit length of Int32: %zu\n", sizeof(Int32) * 8); 18 | } 19 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/.pp03.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter07/programming-projects/.pp03.c.swp -------------------------------------------------------------------------------- /Chapter07/programming-projects/.pp15.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter07/programming-projects/.pp15.c.swp -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 1 3 | * 4 | * The square2.c program of section 6.3 will fail (usually by printing strange answers) 5 | * if i * i exceeds the maximum int value. Run the program and determine the smallest 6 | * value of n that causes failure. Try changing the type of i to short and running 7 | * the program again. (Don't forget to update the conversion specifications in the call 8 | * of printf!) Then try long. From these experiments, what can you conclude about the number 9 | * of bits used to store integer types on your machine? 10 | * */ 11 | 12 | #include 13 | int main(){ 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 2 3 | * 4 | * Q: The square2.c program of section 6.3 will fail (usually by printing strange answers) 5 | * if i * i exceeds the maximum int value. Run the program and determine the smallest 6 | * value of n that causes failure. Try changing the type of i to short and running 7 | * the program again. (Don't forget to update the conversion specifications in the call 8 | * of printf!) Then try long. From these experiments, what can you conclude about the number 9 | * of bits used to store integer types on your machine? 10 | * */ 11 | 12 | #include 13 | int main(){ 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming: A Modern Approach" chapter 7, programming project 3 3 | * 4 | * Q: Modify the sum2.c program from section 7.1 to sum a series of double values. 5 | */ 6 | 7 | #include 8 | int main(){ 9 | int n, sum = 0; 10 | 11 | printf("This program sums a series of doubles. \n"); 12 | printf("Enter integers (0 to terminate): "); 13 | 14 | scanf("%d", &n); 15 | 16 | while(n != 0){ 17 | sum += n; 18 | scanf("%d", &n); 19 | } 20 | printf("The sum is: %d\n", sum); 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution for "C Programming A Modern Approach" chapter 7, programming project 4 3 | * Write a program that translates an alphabetic phone number inot numeric form. 4 | */ 5 | 6 | #include 7 | #include 8 | int main(){ 9 | char i; 10 | printf("Enter phone number: "); 11 | 12 | do { 13 | i = getchar(); 14 | switch(toupper(i)){ 15 | case 'A': case 'B': case 'C': 16 | printf("2"); 17 | break; 18 | case 'D': case 'E': case 'F': 19 | printf("3"); 20 | break; 21 | case 'G': case 'H': case 'I': 22 | printf("4"); 23 | break; 24 | case 'J': case 'K': case 'L': 25 | printf("5"); 26 | break; 27 | case 'M': case 'N': case 'O': 28 | printf("6"); 29 | break; 30 | case 'P': case 'Q': case 'R': case 'S': 31 | printf("7"); 32 | break; 33 | case 'T': case 'U': case 'V': 34 | printf("8"); 35 | break; 36 | case 'W': case 'X': case 'Y': case 'Z': 37 | printf("9"); 38 | break; 39 | default: 40 | putchar(i); 41 | } 42 | } while (i != '\n'); 43 | } 44 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 6 3 | * 4 | * Write a program that prints the values of sizeof(int), sizeof(short). sizeof(long), sizeof(float), sizeof(double), sizeof(long double). 5 | */ 6 | 7 | #include 8 | int main(){ 9 | printf("The size of int(bytes): %d\n", sizeof(int)); 10 | printf("The size of short int(bytes): %d\n", sizeof(short int)); 11 | printf("The size of long(bytes): %d\n", sizeof(long)); 12 | printf("The size of float(bytes): %d\n", sizeof(float)); 13 | printf("The size of double(bytes): %d\n", sizeof(double)); 14 | printf("The size of long double(bytes): %d\n", sizeof(long double)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 8 3 | * 4 | * Modify programming project 8 from Chapter 5 so tht the user enters a time using the 12-hour clock. the input 5 | * will have the form hours:minutes followed by either a P, AM or PM either lower or upper case. White space 6 | * is allowed but not required between the numerical time and the am/pm indicator. 7 | */ 8 | 9 | #include 10 | int main(){ 11 | //Never completed project 8 from chapter 5. Come back later. 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp09.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 9 3 | * 4 | * Write a program that asks the user for a 12 hour time, then displays the time in 24 hour form. 5 | */ 6 | 7 | #include 8 | int main(){ 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" Chapter 7, programming project 10. 3 | * 4 | * Write a progam that counts the number of vowels in a sentence. 5 | */ 6 | 7 | #include 8 | #include 9 | int main(){ 10 | char input; 11 | int vowels = 0; 12 | 13 | printf("Enter a sentence: "); 14 | do { 15 | input = getchar(); 16 | switch(toupper(input)){ 17 | case 'A': case 'E': case 'I': case 'O': case 'U': 18 | vowels += 1; 19 | break; 20 | 21 | } 22 | 23 | } while (input != '\n'); 24 | 25 | printf("Your sentence contains %d vowels\n", vowels); 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp11.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" Chapter 7, programming project 11. 3 | * 4 | * Write a program that takes a firstname and last name entered by the user 5 | * and displays the last name, a comma and the first initial followed by a period: 6 | * The user's input may contain extra spaces before the first name, between the first 7 | * and last names and after the last name 8 | */ 9 | 10 | #include 11 | #include 12 | int main(){ 13 | char input, firstInitial; 14 | 15 | printf("Enter a first and last name: "); 16 | 17 | while ((input = getchar()) == ' '){ 18 | //do nothing, skip over whitespace (32) 19 | } 20 | 21 | firstInitial = input; 22 | 23 | while ((input = getchar()) != ' '){ 24 | //do nothing 25 | } 26 | 27 | while((input = getchar()) != '\n'){ 28 | if(input != ' ') 29 | putchar(input); 30 | 31 | } 32 | 33 | printf(", %c.\n", firstInitial); 34 | } 35 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp12.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 12. 3 | * 4 | * Write a program that evaluates an expression. The operands in the expression are floating-point 5 | * numbers; the operators are +, -, *, and /. The expression is evaluated from left to right. 6 | * (No operator takes precedence over any other operator. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | char input, sign; 14 | float n1 = 0.0f, n2 = 0.0f, result = 0.0f; 15 | 16 | printf("Enter an expression: "); 17 | scanf("%f%c%f", &n1, &sign, &n2); 18 | 19 | switch(sign){ 20 | case '+': 21 | result = n1 + n2; 22 | break; 23 | case '-': 24 | result = n1 - n2; 25 | break; 26 | case '*': 27 | result = n1 * n2; 28 | break; 29 | case '/': 30 | if(n1 == 0 && n2 == 0){ 31 | printf("Divide by zero"); 32 | break; 33 | } else { 34 | result = n1 / n2; 35 | break; 36 | } 37 | } 38 | 39 | printf("Value of expression: %.1f\n", result); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp13.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 13. 3 | * 4 | * Write a program that calculates the average word length for a sentence. For 5 | * simplicity, your program should consider a punctuation mark to be part of the 6 | * word to which it is attached. Display the average word length to one decimal place.. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | //total number of words is 1 as number of words in a sentence is found 14 | //by number of spaces entered + 1. 15 | float result = 0.0f, wordCount = 1.0f, charCount = 0.0f; 16 | char input; 17 | 18 | while((input = getchar()) != '\n'){ 19 | if(isalpha(input) || ispunct(input)) 20 | charCount += 1; 21 | 22 | if(input == ' ') 23 | wordCount += 1; 24 | } 25 | 26 | printf("\n%.1f\n", charCount / wordCount); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter07/programming-projects/pp14.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 7, programming project 14. 3 | * 4 | * Write a program that uses Newton's method to compute the square of a positive 5 | * floating point number. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(){ 12 | double x = 0.0, y = 1.0, xy = 0.0, avg = 0.0, oldY = 0.0; 13 | 14 | printf("Enter a positive number: "); 15 | scanf("%lf", &x); 16 | 17 | do { 18 | xy = x / y; 19 | avg = (y + xy) / 2; 20 | oldY = y; 21 | y = avg; 22 | } while ((fabs(oldY - y)) > (0.0001 * y)); 23 | 24 | printf("Square root: %f\n", avg); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 8, exercise 1 3 | 4 | Q: We discussed using the expression sizeof(a) / sizeof(a[0]) to calculate the number 5 | of elements in an array. The expression size(a) / sizeof(t), where t is the type 6 | of a's elements, would also work, but it's considered an inferior technique. Why? 7 | 8 | A: If the type of a changes in a large program the source will have to be modified 9 | to support the change of type. 10 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 8, exercise 2 3 | 4 | Q: The Q&A section shows how to use a letter as an array subscript. Describe 5 | how to use a digit (in character form) as a subscript. 6 | 7 | A: Using the subtract operator on two chars will promote both chars to ints, so doing 8 | 9 | int a[] = {1,2,3,4,5}; 10 | printf("%d", a['2' - '0']); 11 | 12 | is the same as a[2 - 0] and so the expression will print the 2nd element (3) 13 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 8, exercise 3 3 | 4 | Q: Write a declaration of an array named weekend containing seven bool values. 5 | include an initializer that makes the first and last values true; all over 6 | values should be false. 7 | 8 | A: 9 | _Bool weekend[6] = {1,0,0,0,0,1}; 10 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex04.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith 2 | Solution to "C Programming A Modern Approach" chapter 8, exercise 4 3 | 4 | Q: (C99) Repeat Exercise 3, but this time use a designated initializer. 5 | Make the intializer as short as possible 6 | 7 | A: _Bool weekend[] = {[0] = 1, [6] = 1}; 8 | 9 | I could include stdbool.h here, but this is "short". 10 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 5 3 | * 4 | * The Fibonacci numbers are 0,1,1,2,3,5,8,13 ...., where each number is the sum of 5 | * two preceeding numbers. Write a program fragment that declares an array named 6 | * 'fib_numbers' of length 40 and fills the array with the first 40 Fibonacci numbers. 7 | */ 8 | 9 | #include 10 | 11 | int main(){ 12 | int fib_numbers[40] = {0, 1}, i = 0; 13 | //i = 2, to skip the first 2 elements 14 | for(i = 2; i < (sizeof(fib_numbers) / sizeof(fib_numbers[0])); i++){ 15 | fib_numbers[i] = fib_numbers[i - 1] + fib_numbers[i - 2]; 16 | printf("%d ", fib_numbers[i]); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 6 3 | */ 4 | 5 | #include 6 | 7 | int main(){ 8 | const int segments[10][7] = {{1, 1, 1, 1, 1, 1, 0}, 9 | {0, 1, 1, 0, 0, 0, 0}, 10 | {1, 1, 0, 1, 1, 0, 1}, 11 | {1, 1, 1, 1, 0, 0, 1}, 12 | {0, 1, 1, 0, 0, 1, 1}, 13 | {1, 0, 1, 1, 0, 1, 1}, 14 | {1, 0, 1, 1, 1, 1, 1}, 15 | {1, 1, 1, 0, 0, 0, 0}, 16 | {1, 1, 1, 1, 1, 1, 1}, 17 | {1, 1, 1, 1, 0, 1, 1}}; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 7 3 | */ 4 | 5 | #include 6 | 7 | int main(){ 8 | const int segments[10][7] = {{1, 1, 1, 1, 1, 1}, 9 | {0, 1, 1}, 10 | {1, 1, 0, 1, 1, 0, 1}, 11 | {1, 1, 1, 1, 0, 0, 1}, 12 | {0, 1, 1, 0, 0, 1, 1}, 13 | {1, 0, 1, 1, 0, 1, 1}, 14 | {1, 0, 1, 1, 1, 1, 1}, 15 | {1, 1, 1}, 16 | {1, 1, 1, 1, 1, 1, 1}, 17 | {1, 1, 1, 1, 0, 1, 1}}; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 8 3 | * 4 | * Write a declaration for a two-dimensional array named temperature_readings 5 | * that stores one month of hourly temperature readings. For simplicity assume 6 | * that a month has 30 days. The rows of the array should represent days of the month 7 | * The columns should represent hours of the day 8 | */ 9 | 10 | #include 11 | 12 | int main(){ 13 | int temperature_readings[30][24] = {0}; 14 | 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 10 3 | * 4 | * Write a declaration for an 8 x 8 char array named chess_board. Include an initializer 5 | * that puts the following data into the array. (One character per array element):# 6 | * 7 | * r n b q k b n r 8 | * p p p p p p p p 9 | * . . . . 10 | * . . . . 11 | * . . . . 12 | * . . . . 13 | * P P P P P P P P 14 | * R N B Q K B N R 15 | * 16 | */ 17 | 18 | #include 19 | 20 | int main(){ 21 | char chess_board[8][8] = {{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'}, 22 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 23 | {' ', '.', ' ', '.', ' ', '.', ' ', '.'}, 24 | {'.', ' ', '.', ' ', '.', ' ', '.', ' '}, 25 | {' ', '.', ' ', '.', ' ', '.', ' ', '.'}, 26 | {'.', ' ', '.', ' ', '.', ' ', '.', ' '}, 27 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 28 | {'R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R'}}; 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter08/exercises/ex11.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, exercise 11 3 | * 4 | * Write a program fragment that delcares an 8 x 8 char array named 5 | * draughts_board (British spelling of checkers) and then uses a loop 6 | * to store the following data into the array (One character per array element); 7 | * 8 | * B R B R B R B R 9 | * R B R B R B R B 10 | * B R B R B R B R 11 | * R B R B R B R B 12 | * B R B R B R B R 13 | * R B R B R B R B 14 | * B R B R B R B R 15 | * R B R B R B R B 16 | */ 17 | 18 | #include 19 | int main(){ 20 | char draughts_board[8][8] = {}; 21 | int i = 0, j = 0; 22 | 23 | for(i = 0; i < 8; i++){ 24 | for(j = 0; j < 8; j++){ 25 | if((i + j) % 2 == 0){ 26 | draughts_board[i][j] = 'B'; 27 | printf("%c ", draughts_board[i][j]); 28 | } else { 29 | draughts_board[i][j] = 'R'; 30 | printf("%c ", draughts_board[i][j]); 31 | } 32 | } 33 | printf("\n"); 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 1 3 | * 4 | * Modify the repdigit.c program of section 8.1 so that it shows which digits (if any) 5 | * were repeated. 6 | */ 7 | 8 | #include 9 | 10 | int main(){ 11 | int input, digit, i, seenDigits[10] = {0}; 12 | 13 | printf("Enter a number: "); 14 | scanf("%d", &input); 15 | 16 | while(input > 0){ 17 | digit = input % 10; 18 | seenDigits[digit] += 1; 19 | input /= 10; 20 | } 21 | 22 | printf("Repeated digits: "); 23 | 24 | for(i = 0; i < 10; i++){ 25 | if(seenDigits[i] > 1) 26 | printf("%d ", i); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 2 3 | * 4 | * Modify the repdigit.c program of section 8.1 so that it prints a table showing 5 | * how many times each digit apears in the number: 6 | * 7 | */ 8 | 9 | #include 10 | 11 | int main(){ 12 | int input, digit, i, seenDigits[10] = {0}; 13 | 14 | printf("Enter a number: "); 15 | scanf("%d", &input); 16 | 17 | while(input > 0){ 18 | digit = input % 10; 19 | seenDigits[digit] += 1; 20 | input /= 10; 21 | } 22 | 23 | printf("Digit: \t\t"); 24 | for(i = 0; i < 10; i++){ 25 | printf("%d ", i); 26 | } 27 | printf("\nOccurrences: "); 28 | for(i = 0; i < 10; i++){ 29 | printf("%d ", seenDigits[i]); 30 | 31 | 32 | } 33 | printf("\n"); 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 3 3 | * 4 | * Modify the redigit.c program of section 8.1 so that the user can enter more 5 | * than one number to be tested for repeated digits. The program should terminate 6 | * when the user enter a number that's less than or equal to 0. 7 | */ 8 | 9 | #include 10 | 11 | int main(){ 12 | int input = 1, digit, i, seenDigits[10] = {0}; 13 | 14 | while(input > 0){ 15 | printf("Enter a number: "); 16 | scanf("%d", &input); 17 | int n = input; 18 | 19 | if(input <= 0){ 20 | printf("\nExiting..."); 21 | break; 22 | } 23 | while(n > 0){ 24 | digit = n % 10; 25 | seenDigits[digit] += 1; 26 | n /= 10; 27 | } 28 | 29 | printf("Digit: \t\t"); 30 | for(i = 0; i < 10; i++){ 31 | printf("%d ", i); 32 | } 33 | 34 | printf("\nOccurrences: "); 35 | for(i = 0; i < 10; i++){ 36 | printf("%d ", seenDigits[i]); 37 | } 38 | printf("\n"); 39 | } 40 | printf("\n"); 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 4 3 | * 4 | * Modify the reverse.c program of section 8.1 to use the expression 5 | * (int) (sizeof(a) / sizeof(a[0])) (or a macro with this value) for the array length 6 | */ 7 | 8 | #include 9 | #define N (int)(sizeof(a) / sizeof(a[0])) 10 | 11 | int main(void){ 12 | int a[10], i; 13 | 14 | printf("Enter %d numbers: ", N); 15 | for(i = 0; i < N; i++) 16 | scanf("%d", &a[i]); 17 | 18 | printf("In reverse order:"); 19 | for(i = N - 1; i >= 0; i--) 20 | printf(" %d", a[i]); 21 | printf("\n"); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 6 3 | * 4 | * Write a filter that converts a normal message into 1337 speak. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main(void){ 11 | char message[255] = {0}, input, i = 0, msgLength = 0; 12 | 13 | printf("Enter a message: "); 14 | while((input = getchar()) != '\n'){ 15 | message[msgLength] = input; 16 | msgLength++; 17 | } 18 | 19 | for(i = 0; i < msgLength; i++){ 20 | switch(toupper(message[i])){ 21 | case 'O': 22 | putchar('0'); 23 | break; 24 | case 'I': 25 | putchar('1'); 26 | break; 27 | case 'E': 28 | putchar('3'); 29 | break; 30 | case 'A': 31 | putchar('4'); 32 | break; 33 | case 'S': 34 | putchar('5'); 35 | break; 36 | case 'B': 37 | putchar('8'); 38 | break; 39 | default: 40 | putchar(message[i]); 41 | break; 42 | } 43 | } 44 | printf("!!!!!!!!!!!!!!\n"); 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 7 3 | * 4 | * Write a program that reads a 5x5 array of integers and then prints the row sums 5 | * and the column sums. 6 | */ 7 | 8 | #include 9 | 10 | int main(void){ 11 | int array[5][5] = {0}, row = 0, col = 0, rowSum = 0, colSum = 0; 12 | 13 | for(row = 0; row < 5; row++){ 14 | printf("Enter row %d: ", row + 1); 15 | scanf("%d %d %d %d %d", &array[row][0], &array[row][1], &array[row][2], 16 | &array[row][3], &array[row][4]); 17 | } 18 | 19 | printf("\nRow totals: "); 20 | for(row = 0; row < 5; row++){ 21 | for(col = 0; col < 5; col++){ 22 | rowSum += array[row][col]; 23 | } 24 | printf("%d ", rowSum); 25 | rowSum = 0; 26 | } 27 | 28 | printf("\nColumn totals: "); 29 | for(col = 0; col < 5; col++){ 30 | for(row = 0; row < 5; row++){ 31 | colSum += array[row][col]; 32 | } 33 | printf("%d ", colSum); 34 | colSum = 0; 35 | } 36 | printf("\n"); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution to "C Programming A Modern Approach" chapter 8, programming project 10 3 | * 4 | * Modify Programming Project 8 from Chapter 5 so that the departure times are stored in 5 | * an array and the arrival times are stored in a section array. (The times are integers representing 6 | * the number of minutes since midnight). The program will use a loop to search the array of 7 | * departure times for the one closest to the time entered by the user user. 8 | */ 9 | 10 | #include 11 | int main(void){ 12 | 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Chapter08/programming-projects/pp17.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Solution for "C Programming A Modern Approach" chapter 8, programming project 17 3 | * 4 | * Write a program that prints an n x n magic square. (A square arrangement of the number 1, 2... n2 in 5 | * which the sums of the rows, columns, and diagonals are all the same. 6 | */ 7 | 8 | #include 9 | 10 | int main(void){ 11 | 12 | printf("This program creates a magic square of a specified size."); 13 | printf("\nThe size must be an odd number between 1 and 99."); 14 | printf("\nEnter size of magic square"); 15 | 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 1 3 | * 4 | * The following function which computers the area of a triangle contains two errors. 5 | * Locate the erros and show how to fix them. 6 | * 7 | * double triangle_areA(double base, height) 8 | * double product; 9 | * { 10 | * product = base * height; 11 | * return product / 2; 12 | * 13 | * } 14 | */ 15 | 16 | //The errors are: 17 | // 1. The function parameters are ilegal due to missing types. 18 | // 2. The local variable, product, is outside the function. 19 | // 20 | // Corrected function 21 | #include 22 | 23 | double triangle_area(double base, double height){ 24 | double product; 25 | product = base * height; 26 | return product / 2; 27 | 28 | } 29 | 30 | int main(void){ 31 | printf("%f", triangle_area(3, 4)); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 2 3 | * 4 | * Write a function check(x, y, n) that returns 1 if both x and y fall between 0 5 | * and n - 1, inclusive. The function should return 0 otherwise. All variables are int. 6 | */ 7 | 8 | #include 9 | int check(int x, int y, int n); //check prototype 10 | 11 | int main(void){ 12 | printf("Test inputs: "); 13 | printf("\n(1, 3, 5) expected 1, actual: %d", check(1, 3, 5)); 14 | printf("\n(9, 1, 5) expected 1, actual: %d", check(3, 1, 5)); 15 | printf("\n(3, 5, 1) expected 0, actual: %d\n", check(3, 5, 1)); 16 | 17 | return 0; 18 | } 19 | 20 | //test if x, y are greater than 0 and less than n - 1. 21 | //Returns 1 if true, 0 otherwise. 22 | int check(int x, int y, int n){ 23 | n = n--; 24 | if((x >= 0 && x <= n) && (y >= 0 && y <= n)) 25 | return 1; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 3 3 | * 4 | * Write a function gcd(m, n) that calculates the greatest common divisor 5 | * of the integers m and n. 6 | */ 7 | 8 | #include 9 | 10 | int gcd(int m, int n); 11 | 12 | int main(void){ 13 | printf("GCD Test (45, 30) expected 15, actual: %d", gcd(45, 30)); 14 | printf("\nGCD Test (100, 5) expected 5, actual: %d", gcd(100, 5)); 15 | printf("\nGCD Test (44, 144) expected 4, actual: %d\n", gcd(44, 144)); 16 | return 0; 17 | } 18 | 19 | int gcd(int m, int n){ 20 | int tmp = 0; 21 | while(n != 0){ 22 | tmp = m % n; 23 | m = n; 24 | n = tmp; 25 | } 26 | return m; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 4 3 | * 4 | * Write a function day_of_year(month, day, year) that returns the day of the 5 | * year specified by three arguments. 6 | */ 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 5 3 | * 4 | * Write a function num_digits(n) that returns the number of digits in a positive 5 | * integer. 6 | */ 7 | 8 | #include 9 | 10 | int num_digits(unsigned int n); 11 | 12 | int main(void){ 13 | printf("num_digits test(100) expected 3, actual: %d", num_digits(100)); 14 | printf("\nnum_digits test(9) expected 1, actual: %d", num_digits(9)); 15 | printf("\nnum_digits test(150050) expected 6, actual: %d\n", num_digits(150050)); 16 | return 0; 17 | } 18 | 19 | /* Takes a positive integer n, and returns the number of digits in that figure. */ 20 | int num_digits(unsigned int n){ 21 | int digit_counter = 0; 22 | while(n > 0){ 23 | n /= 10; 24 | digit_counter++; 25 | } 26 | 27 | return digit_counter; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 6 3 | * 4 | * Write a function digit(n, k) that returns kth digit (from the right) in a 5 | * positive integer. 6 | */ 7 | 8 | #include 9 | 10 | int digit(int n, unsigned int k); 11 | 12 | int main(void){ 13 | printf("digit(1, 135) test expected 5, actual: %d", digit(1, 135)); 14 | printf("\ndigit(2, 401) test expected 0, actual: %d", digit(3, 401)); 15 | printf("\ndigit(4, 6981) test expected 6, actual: %d\n", digit(4, 6981)); 16 | 17 | return 0; 18 | } 19 | 20 | int digit(int n, unsigned int k){ 21 | n--; 22 | while(n > 0){ 23 | n--; 24 | k /= 10; 25 | } 26 | return k % 10; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 9, exerice 7 3 | 4 | Suppose that the function f has the following definition: 5 | 6 | int f(int a, int b) { ... } 7 | 8 | which of the following statements are legal? (Assume that i has type int and x has type double.) 9 | 10 | (a) i = f(83, 12); 11 | Legal. 12 | 13 | (b) x = f(83, 12); 14 | Legal. 15 | 16 | (c) i = f(3.15, 9.28); 17 | Legal, but will lose fractional part as the parameters are converted to int and the value of i is integer by conversion rules. 18 | 19 | (d) x = f(3.15, 9.28); 20 | Legal, same as above but the reuslt will have a fractional part as promoted to double. 21 | 22 | (e) f(83, 12); 23 | Legal, however nothing is done with the result. 24 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex08.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 9, exerice 8 3 | 4 | Which of the following would be valid prototypes for a function that returns nothing 5 | and has one double paramter? 6 | 7 | (a) void f(double x); 8 | valid. 9 | 10 | (b) void f(double); 11 | valid, function parameters may have names omitted. 12 | 13 | (c) void f(x); 14 | invalid, function parameters may have names omitted but not types. 15 | 16 | (d) f(double x); 17 | invalid, functions in c99 may not have types omitted. 18 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex09.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 9, exerice 9 3 | 4 | What will be the output of the following program? 5 | 6 | #include 7 | 8 | void swap(int a, int b); 9 | 10 | int main(void){ 11 | int i = 1, j = 2; 12 | swap(i, j); 13 | printf("i = %d, j = %d\n", i, j); 14 | return 0; 15 | } 16 | 17 | void swap(int a, int b){ 18 | int temp = a; 19 | a = b; 20 | b = temp; 21 | } 22 | 23 | 24 | The result of this program will be "i = 1, j = 2". C is pass by value, variables 25 | a and b are only swapped inside the function with no modification to i and j. 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex12.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 12 3 | * 4 | * Write the following function: 5 | * 6 | * double inner_product(double a[]. double b[], int n); 7 | */ 8 | 9 | #include 10 | 11 | double inner_product(double a[], double b[], int n); 12 | 13 | int main(){ 14 | double array1[] = {15, 10, 5, 5, 20}; 15 | double array2[] = {6, 12, 36, 18, 6}; 16 | 17 | printf("inner_product test. Expected: 600, actual: %.2f\n", inner_product(array1, array2, 5)); 18 | } 19 | 20 | double inner_product(double a[], double b[], int n){ 21 | int i = 0; 22 | double sum = 0; 23 | 24 | for(i = 0; i < n; i++){ 25 | sum += a[i] * b[i]; 26 | } 27 | return sum; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex14.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 14 3 | * 4 | * The following function is supposed to return true is any element of the array a has the value 5 | * 0 and false is the elements are nonzero. Sadly, it contains an error. Find the error 6 | * and show how ot fix it. 7 | * 8 | * bool has_zero(int a[], int n){ 9 | * int i; 10 | * for(i = 0; i < n; i++) 11 | * if(a[i] == 0) 12 | * return true; 13 | * else 14 | * return false; 15 | * 16 | * } 17 | */ 18 | 19 | #include 20 | 21 | bool has_zero(int a[], int n) 22 | 23 | int main(void){ 24 | 25 | 26 | 27 | } 28 | 29 | //The error in this function is that the function is terminated the moment 30 | //a non-zero element is found, not if all elements are non-zero. It won't test 31 | //all the elements. The fix is just to return false, and terminate if a zero is found. 32 | bool has_zero(int a[], int n){ 33 | int i; 34 | 35 | for(i = 0; i < n; i++) 36 | if(a[i] == 0) 37 | return true; 38 | 39 | 40 | return false; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex15.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 15 3 | * 4 | * The following (rather confusing) function finds the median of three numbers. 5 | * Rewrite the function so tat it has just one return statement. 6 | */ 7 | 8 | #include 9 | 10 | double median(double x, double y, double z); 11 | 12 | int main(void){ 13 | return 0; 14 | } 15 | 16 | double median(double x, double y, double z){ 17 | double median; 18 | if(x <= y) 19 | if(y <= z) median = y; 20 | else if(x <= z) median = z; 21 | else median = x; 22 | if(z <= y) median = y; 23 | if(x <= z) median = x; 24 | 25 | return median; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex16.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 16 3 | * 4 | * Condense the fact function in the same way we condensed power. 5 | * 6 | */ 7 | 8 | #include 9 | 10 | int fact(int n); 11 | 12 | int main(void){ 13 | return 0; 14 | } 15 | 16 | int fact(int n){ 17 | return n <=1 ? 1 : n * fact(n - 1); 18 | } 19 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex17.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 17 3 | * 4 | * Rewrite the fact function so that is is no longer recursive. 5 | */ 6 | 7 | #include 8 | 9 | int fact(int n); 10 | 11 | int main(void){ 12 | printf("fact test. Expected 720, actual: %d\n", fact(6)); 13 | return 0; 14 | } 15 | 16 | int fact(int n){ 17 | int result = 1; 18 | while(n > 0){ 19 | result *= n; 20 | n--; 21 | } 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex18.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 18 3 | * 4 | * Write a recursive version of the gcd function (exercise 3). 5 | * 6 | */ 7 | 8 | #include 9 | 10 | int gcd(int m, int n); 11 | 12 | int main(void){ 13 | printf("GCD Test (45, 30) expected 15, actual: %d", gcd(45, 30)); 14 | printf("\nGCD Test (100, 5) expected 5, actual: %d", gcd(100, 5)); 15 | printf("\nGCD Test (44, 144) expected 4, actual: %d\n", gcd(44, 144)); 16 | return 0; 17 | } 18 | 19 | int gcd(int m, int n){ 20 | if(n == 0){ 21 | return m; 22 | } else { 23 | gcd(n, m % n); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter09/exercises/ex19.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, exerice 19 3 | * 4 | * Consider the following "mystery" function: 5 | * 6 | * void pb(int n){ 7 | * if(n != 0){ 8 | * pb(n/2); 9 | * putchar('0' + n % 2); 10 | * } 11 | * } 12 | */ 13 | 14 | #include 15 | 16 | void pb(int n); 17 | 18 | int main(void){ 19 | pb(151); 20 | printf("\n"); 21 | return 0; 22 | } 23 | 24 | //Prints the value n in base2 form. 25 | void pb(int n){ 26 | if(n != 0){ 27 | pb(n / 2); 28 | putchar('0' + n % 2); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Chapter09/programming-projects/pp05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, programming project 5 3 | * 4 | * Modify programming project 17 from chapter 8 so that it includes the following 5 | * functions: 6 | * 7 | * void create_magic_square_(int n, char magic_square[n][n]); 8 | * void print_magic_square(int n, char magic_square[n][n]); 9 | * 10 | * After obtaining the number n from the user, main will call create_magic_square, 11 | * passing it an nxn array that is declared inside main. create_magic_square will 12 | * fill the array with the number 1,2,..m2 as described in the original project. 13 | * main will then call print_magic_square which will display the array in the format 14 | * described in the original project. 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | int main(void){ 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Chapter09/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 9, programming project 6 3 | * 4 | * Write a function that computes the value of the following polynomial: 5 | * 6 | * 3x^5 + 2x^4 - 5x^3 - x^ + 7x - 6 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int compute_poly(int x); 13 | 14 | int main(void){ 15 | printf("Result: Expected 92, actual, %d\n", compute_poly(2)); 16 | printf("Result: Expected 762, actual, %d\n", compute_poly(3)); 17 | } 18 | 19 | int compute_poly(int x){ 20 | return 3 * (pow(x, 5)) + 2 * (pow(x, 4)) - 5 * (pow(x, 3)) - pow(x, 2) 21 | + (7 * x) - 6; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter10/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 10, exercise 1 3 | * 4 | * The following program outline shows only function definitions and variable 5 | * definitions. 6 | * 7 | * int a; 8 | * 9 | * void f(int b){ 10 | * int c; 11 | * } 12 | * 13 | * void g(void){ 14 | * int d; 15 | * { 16 | * int e; 17 | * } 18 | * } 19 | * 20 | * int main(void){ 21 | * int f 22 | * } 23 | * 24 | * For each of the following scopes, list all variable and parameter names visible 25 | * in that scope: 26 | * 27 | * (a) The f function 28 | * (b) The g function 29 | * (c) The block in which e is declared 30 | * (d) The main functioh 31 | */ 32 | -------------------------------------------------------------------------------- /Chapter10/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 10, exercise 1 3 | 4 | The following program outline shows only function definitions and variable 5 | idefinitions. 6 | 7 | int a; 8 | void f(int b){ 9 | int c; 10 | } 11 | 12 | void g(void){ 13 | int d; 14 | { 15 | int e; 16 | } 17 | } 18 | 19 | int main(void){ 20 | int f 21 | } 22 | 23 | For each of the following scopes, list all variable and parameter names visible in that scope: 24 | 25 | (a) The f function: External variable a, local variables b and c. 26 | (b) The g function: External variable a, local variable d. 27 | (c) The block in which e is declared External variable a, local variable d. 28 | (d) The main function: External variable a, local variable f. 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter10/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 10, exercise 2 3 | 4 | The following program outline shows only function definitions and variable 5 | idefinitions. 6 | 7 | int b, c; 8 | 9 | void f(void){ 10 | int b, d; 11 | } 12 | 13 | void g(int a){ 14 | int c; 15 | { 16 | int a, d; 17 | } 18 | } 19 | 20 | int main(void){ 21 | int c, d; 22 | } 23 | 24 | For each of the following scopes list all variable and parameter names 25 | visible in that scope. If there's more than one variable or parameter 26 | with the same name indicate which one is visible. 27 | 28 | (a) The f function: External variable c, local variable b, d. 29 | (b) The g function: External variable b, local variable c, parameter a. 30 | (c) The block in which a and d are declared. External variable b, c, local variab a, d 31 | (d) The main function. External variable b, local variable c, d. 32 | -------------------------------------------------------------------------------- /Chapter10/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 10, exercise 3 3 | 4 | Suppose that a program has only one function(main). How many different variables 5 | named i could this program contain? 6 | 7 | One external, one parameter, one local and one in each local block 8 | -------------------------------------------------------------------------------- /Chapter11/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 11, exercise 1. 3 | 4 | "If i is a variable and p points to i, which of the following expressions are 5 | aliases for i?" 6 | 7 | Aliasing is using multiple names to access the same area of memory. p is an alias 8 | for i aslong as it references the same location as i. 9 | 10 | (a) *p 11 | *p is an alias for i. Changing *p will change i. 12 | 13 | (b) &p 14 | &p is not an alias for i. &p contains the address of p. 15 | 16 | (c) *&p 17 | Dereferencing the address of p. Not an alias 18 | 19 | (d) &*p 20 | The address of the dereferenced value of p. Not an alias. 21 | 22 | (e) *i 23 | i is an int, not a pointer. Not a alias. 24 | 25 | (f) &i 26 | Not an alias 27 | 28 | (g) *&i 29 | Dereferencing the address of i, an alias. 30 | 31 | (h) &*i 32 | i is still an int type, cannot use pointer operators. 33 | 34 | a and g are aliases. 35 | 36 | -------------------------------------------------------------------------------- /Chapter11/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 11, exercise 2. 3 | 4 | "If i is an int variable and p and q are pointers to int, which of the following 5 | assignments are legal?" 6 | 7 | int i, *p, *q; 8 | 9 | (a) p = i; 10 | Illegal, p is an pointer. 11 | 12 | (b) *p = &i; 13 | Illegal, *p is an integer variable, &i is a pointer to int. 14 | 15 | (c) &p = q; 16 | Illegal. &p is a pointer to pinter to an integer, q is pointer to int. 17 | 18 | (d) p = &q; 19 | Illegal. 20 | 21 | (e) p = *&q; 22 | Legal 23 | 24 | (f) p = q; 25 | Legal, Both p and q are pointers to integers. 26 | 27 | (g) p = *q; 28 | Illegal 29 | 30 | (h) *p = q; 31 | Illegal 32 | 33 | (i) *p = *q; 34 | Legal 35 | 36 | -------------------------------------------------------------------------------- /Chapter11/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11, exercise 4. 3 | * 4 | * Write the following function: 5 | * 6 | * void swap(int *p, int *q); 7 | * 8 | * When passed the addresses of two variables, swap should exchange the variables. 9 | */ 10 | 11 | #include 12 | void swap(int *p, int *q); 13 | 14 | int main(void){ 15 | int x = 10, y = 20; 16 | swap(&x, &y); 17 | printf("Swap test. Expected: x = 20, y = 10. Actual: x = %d, y = %d\n", x, y); 18 | 19 | return 0; 20 | } 21 | 22 | void swap(int *p, int *q){ 23 | int tmp; 24 | tmp = *p; 25 | *p = *q; 26 | *q = tmp; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter11/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11, exercise 5. 3 | * 4 | * Write the following function: 5 | * 6 | * void split(long total_sec, int *hr, int *min, int *sec); 7 | * 8 | * total_sec is a time represented as the number of seconds since midnight. hr, min 9 | * and sec are pointers to variables in which the function will store the equivalent 10 | * time in hours, minutes and seconds. 11 | */ 12 | 13 | #include 14 | 15 | void split(long total_sec, int *hr, int *min, int *sec); 16 | 17 | int main(void){ 18 | int hr = 0, min = 0, sec = 0; 19 | split(4050, &hr, &min, &sec); 20 | printf("4050 seconds is 1hr, 7min, 30 seconds. Test: %d:%d:%d\n", hr, min, sec); 21 | return 0; 22 | } 23 | 24 | void split(long total_sec, int *hr, int *min, int *sec){ 25 | *min = total_sec / 60; 26 | *sec = total_sec % 60; 27 | *hr = *min / 60; 28 | *min = *min % 60; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter11/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11, exercise 8. 3 | * 4 | * Write the following function 5 | * int *find_largest(int a[], int n); 6 | * 7 | * when passed an array a of length n, the function will return a pointer to 8 | * array's largest element. 9 | */ 10 | 11 | #include 12 | /* Function of type pointer to int*/ 13 | int *find_largest(int a[], int n); 14 | 15 | 16 | 17 | int main(void){ 18 | int array[] = {9, 25, 19, 18, 41, 7}; 19 | printf("find_largest test. Expected 41, actual:%d\n", *find_largest(array , 6)); 20 | 21 | 22 | return 0; 23 | } 24 | 25 | int *find_largest(int a[], int n){ 26 | int i, largest_element = a[0]; 27 | for(i = 0; i < n; i++){ 28 | if(a[i] > a[largest_element]) 29 | largest_element = i; 30 | } 31 | return &a[largest_element]; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter11/programming-projects/pp02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11 programming project 2 3 | * 4 | * Modify programming project 8 from Chapter 5 so that it includes the following 5 | * function: 6 | * 7 | * void find_closest_flight(int destired_time, int *departure_time, int* arrival_time); 8 | * 9 | * This function will find the flight whose depature time is closest to desired_time 10 | * (expressed in minutes since midnight). It will store the departure and arrival 11 | * times of this flight (also expressed in minutes since midnight) in the variables 12 | * pointed to by departure and arrival_time, respectively. 13 | */ 14 | 15 | //UNFINISHED. RELIES ON pp08 FROM CHAPTER 5 WHICH IS ALSO INCOMPLETE 16 | -------------------------------------------------------------------------------- /Chapter11/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11 programming project 3 3 | * 4 | * Modify programming project from chapter 6 so that includes the following function 5 | * 6 | * void reduce(int numerator, int denominator, int *reduced_numerator, 7 | * int *reduced denominator); 8 | * 9 | * numerator and denominator are the numerator and denominator of a fraction. 10 | * reduced_numerator and reduced_denominator are pointers to variables in which 11 | * the function will store the numerator and denominator of the fraction once it has 12 | * been reduced to lowest terms. 13 | * 14 | */ 15 | 16 | //INCOMPLETE, RELIES ON pp03, CHAPTER 6. 17 | -------------------------------------------------------------------------------- /Chapter11/programming-projects/pp04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 11 programming project 4 3 | * 4 | * Modify the poker.c program of section 10.5 by moving all external variables 5 | * into main and modifying function so that they communicate by passing arguments. 6 | * The analyze_hand function needs to change the straight, flush, four, three, and 7 | * pairs variable so it will have to be passed pointers to those variables. 8 | * 9 | */ 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 12, exercise 1. 3 | 4 | Q: Suppose that the following declarations are in effect: 5 | int a[] = {5, 15, 34, 53,14 ,2, 52, 72}; 6 | int *p = &a[1], *q = &a[5]; 7 | 8 | (a) What is the value of *(p+3)? 9 | The value of this expression is 14. 10 | 11 | (b) What is the value of *(q-3)? 12 | The value this expression is 34 13 | 14 | (c) What is the value of q - p; 15 | Subtracting a pointer from another pointer is equal to the difference 16 | between the two pointers measured by the index the point at. This expression 17 | equates to 5 - 1 = 4. 18 | 19 | (d) Is the condition of p < q true or false? 20 | Comparison operators used with points test against their relative positions 21 | they hold in the array; they must be in the same array. This expressions 22 | is evaluated as 1 < 5, which is true 23 | 24 | (e) Is the condition of *p < *q true or false? 25 | This this expression tests against the value in memory the pointers 26 | point at. The expression is evaluated as to 15 < 2, which is false. 27 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 12, exercise 2. 3 | 4 | What will be the contents of the a array after the following statements are 5 | executed? 6 | 7 | #define N 10 8 | 9 | int a[N] = {1,2,3,4,5,6,7,8,9,10}; 10 | int *p = &a[0], *q = &a[N-1], temp; 11 | 12 | while(p < q){ 13 | temp = *p; 14 | *p++ = *q; 15 | *q-- = temp; 16 | 17 | } 18 | 19 | After the first iteration, 10 and 1 have been swapped and the p and q place in the 20 | array have been incremented by one, and reduced by one respectively. 21 | 22 | p q 23 | 10, 2, 3, 4, 5, 6, 7, 8, 9, 1 24 | 25 | The second; 26 | p q 27 | 10, 9, 3, 4, 5, 6, 7, 8, 2, 1 28 | 29 | When the loop is complete the array has been reversed to: 30 | 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 31 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * 3 | * Rewrite the make_empty, is_empty and is_full functions of section 10.2 4 | * to use pointer variable top_ptr instead of integer variable top. 5 | */ 6 | 7 | #include 8 | #include 9 | #define STACK_SIZE 100 10 | int contents[STACK_SIZE]; 11 | int *top = &contents[0]; 12 | 13 | void make_empty(void){ 14 | top = &contents[0]; 15 | } 16 | 17 | bool is_empty(void){ 18 | return top == &contents[0]; 19 | } 20 | 21 | bool is_full(void){ 22 | return top == &contents[STACK_SIZE]; 23 | } 24 | 25 | void push(int i){ 26 | if(is_full()) 27 | //stack_overflow(); 28 | printf("full\n"); 29 | else 30 | *top++ = i; 31 | } 32 | 33 | int pop(void){ 34 | if(is_empty()) 35 | //stack_underflow() 36 | printf("empty\n"); 37 | else 38 | return *--top; 39 | } 40 | 41 | int main(void){ 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 12, exercise 5. 3 | 4 | Suppose that a is a one-dimensional array and p is a pointer variable. Assuming 5 | that the assignment p = a has just been performed, which of the following 6 | expressions are illegal because of mistmatched types? Of the remaining 7 | expressions, which are true (Have a nonzero value)? 8 | 9 | The name of an array can be used as a pointer to the first element 10 | in the array. 11 | 12 | p = a 13 | 14 | (a) p == a[0] 15 | False, this is equal to comparing &a[0] to a[0]. 16 | 17 | (b) p == &a[0] 18 | True, same as comparing &a[0] to &a[0]. As p is a pointer to a[0] 19 | 20 | (c) *p == a[0] 21 | True 22 | 23 | (d) p[0] == a[0] 24 | Pointers may be used as an array name. In this case we are comparing 25 | a[0] == a[0] which is true. 26 | 27 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * Solution to "C Programming: A modern approach" chapter 12, exercise 6 3 | * 4 | * Rewrite the following function use pointer arithmetic instead of subscripting. 5 | * Make as few changes as possible. 6 | * 7 | * int sum_array(const int a[]. int n){ 8 | * int i, sum; 9 | * 10 | * sum = 0; 11 | * for(i = 0; i < n; i++){ 12 | * sum += a[i]; 13 | * } 14 | * return sum; 15 | * } 16 | */ 17 | 18 | #include 19 | 20 | int sum_array(int a[], int n); 21 | 22 | int main(void){ 23 | int input[] = {1, 2, 3, 4, 5}; 24 | 25 | printf("\n%d\n", sum_array(input, 5)); 26 | return 0; 27 | } 28 | 29 | /* Returns the sum of all elements in array a*/ 30 | int sum_array(int a[], int n){ 31 | int *p, sum = 0; 32 | for(p = a; p < a + n; p++){ 33 | sum += *p; 34 | } 35 | 36 | return sum; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex07.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * Solution to "C Programming: A modern approach" chapter 12, exercise 7 3 | * 4 | * Write the following function: 5 | * 6 | * bool search(const int a[], int n, int key); 7 | * 8 | * a is an array to be searched, n is the number of elements in the array, 9 | * and key is the search key. Search should return true if key matches 10 | * some element of a, and false if it dosnt. Use pointer arithmetic 11 | * to visit array elements. 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | bool search(const int [], int, int); 18 | 19 | int main(void){ 20 | int input[] = {7,4,2}; 21 | printf("%d\n", search(input, 3, 2)); 22 | printf("%d\n", search(input, 3, 12)); 23 | 24 | 25 | return 0; 26 | } 27 | 28 | bool search(const int a[], int n, int key){ 29 | bool result = false; 30 | const int *p; 31 | for(p = a; p < a + n; p++){ 32 | if(*p == key){ 33 | result = true; 34 | } 35 | } 36 | return result; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * Solution to "C Programming: A modern approach" chapter 12, exercise 10 3 | * 4 | * Modify the find_midle function of section 11.5 so that is uses pointer arithmetic 5 | * to calculate the return value. 6 | */ 7 | 8 | #include 9 | int *find_middle(int a[], int n); 10 | 11 | int main(void){ 12 | int a[] = {1,2,3,4,5,6,7,8,9,10}; 13 | printf("%d\n", *find_middle(a, 10)); 14 | return 0; 15 | 16 | } 17 | 18 | //Returns the middle element of a 19 | int *find_middle(int a[], int n){ 20 | return a + (n / 2); 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex11.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * Solution to "C Programming: A modern approach" chapter 12, exercise 11 3 | * 4 | * Modify the find_largest function so that it uses pointer arithmetic 5 | * and not subscripting to visit array elements. 6 | */ 7 | 8 | #include 9 | 10 | int *find_largest(int a[], int n); 11 | 12 | int main(void){ 13 | int a[] = {89, 4001, 29, 4, 3, 4002, 4000}; 14 | printf("Expected result: 4002, actual result: %d\n", *find_largest(a, 7)); 15 | a[0] = 5000; 16 | printf("Expected result: 5000, actual result: %d\n", *find_largest(a, 7)); 17 | return 0; 18 | } 19 | 20 | 21 | int *find_largest(int a[], int n){ 22 | int i, *largest_element = a, *p; 23 | 24 | for(p = a; p < a + n; p++){ 25 | if(*p > *largest_element){ 26 | largest_element = p; 27 | } 28 | } 29 | 30 | return largest_element; 31 | } 32 | -------------------------------------------------------------------------------- /Chapter12/exercises/ex15.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith 2 | * Solution to "C Programming: A modern approach" chapter 12.4, pointers 3 | * and multi-dimensional arrays, exercise 15. 4 | * 5 | * Write a loop that prints all temperature readings stored in row of the 6 | * temperature array. Use a pointer to visit each element of that row 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | 13 | int main(void){ 14 | int temperatures[7][24] = {0}, *p; 15 | int i = 3; 16 | 17 | //Assign a pointer to row i in temperatures. 18 | //Test against the base of row i in temperature + the next 24 columns. 19 | //incremenet p 20 | for(p = temperatures[i]; p < temperatures[i] + 24; p++){ 21 | //Assign a value under 32 just for some flavour 22 | *p = rand() % 32; 23 | printf("%d ", *p); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter12/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 12 programming project 1 3 | * 4 | * (a) Write a program that reads a message, then prints the reversal of the 5 | * message. 6 | * 7 | * (b) Revise the program to use a pointer instead of an integer to keep track 8 | * of the current position in the array. 9 | */ 10 | 11 | #include 12 | int main(void){ 13 | char a[5] = {'A', 'B', 'C', 'D', 'E'}, *p; 14 | int i; 15 | 16 | for(i = 5; i >= 0; i--) 17 | printf("%c ", a[i]); 18 | 19 | printf("\n"); 20 | for(p = &a[4]; p >= a; p--) 21 | printf("%c ", *p); 22 | } 23 | -------------------------------------------------------------------------------- /Chapter12/programming-projects/pp03.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 12 programming project 3 3 | * 4 | * Simplify programming project 1(b) by taking advantage of the fact than 5 | * an array name can be used as a pointer. 6 | */ 7 | 8 | #include 9 | 10 | int main(void){ 11 | char a[5] = {'A', 'B', 'C', 'D', 'E'}, *p; 12 | char *a_last_element; 13 | 14 | for(a_last_element = &a[4]; a_last_element >= a; a_last_element--) 15 | printf("%c", *a_last_element); 16 | 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 1 3 | 4 | "The following function calls supposedly write a single new-line character, but some are incorrect. Identify 5 | which calls don't work and explain why." 6 | 7 | a. printf("%c", '\n'); 8 | This statement is valid as printf is expecting a charater literal. 9 | 10 | b. printf("%c", "\n"); 11 | This statement is invalid because a string is passed when a character is expected. 12 | 13 | c. printf("%s", '\n'); 14 | Invalid because a character literal is passed when a string is expected. 15 | 16 | d. printf("%s", "\n"); 17 | Valid. 18 | 19 | e. printf('\n'); 20 | Invalid because printf is expecting to a pointer/string. '\n' is a integer/character literal. 21 | 22 | f. printf("\n"); 23 | Valid. 24 | 25 | g. putchar('\n'); 26 | Valid 27 | 28 | h. putchar("\n"); 29 | Invalid, putchar expects a character/integer not a string. 30 | 31 | i. puts('\n'); 32 | Invalid puts expects a string, not a character literal. 33 | 34 | j. puts("\n"); 35 | Valid. 36 | 37 | k. puts(" "); 38 | Valid. 39 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 2 3 | 4 | "Suppose that p has been declared as follows: 5 | char *p = "abc"; 6 | 7 | Which of the following calls are legal? Show the output produced 8 | by each legal call, and explain why the others are illegal" 9 | 10 | a. putchar(p); 11 | Ilegal, putchar expects a character. 12 | 13 | b. putchar(*p); 14 | Legal. *p is a reference to the first element of array p, and thus should print a. 15 | 16 | c. puts(p); 17 | Legal. puts expects a string. The output is abc 18 | 19 | d. puts(*p); 20 | Ilegal, puts requires a string. *p is a reference to the first element of an array, which is an integer. 21 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 3 3 | 4 | "Suppose that we call scanf as follows: 5 | scanf("%d%s%d", &i, s, &j); 6 | If the user enters 12abc34 56def78, what will be the values of i, s and j 7 | after the call?" 8 | 9 | scanf will read input until it reaches a space or an input that does not match 10 | the format specifier. The result of this statement will be: 11 | 12 | i = 12. Input will stop at the character a 13 | s = abc34. Input will top at the space. 14 | j = 56. The rest of the input is discarded. 15 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 5 3 | 4 | "Write a functioned named capitalize that capitalizes all letters in it's 5 | argument will be a null-terminated string containing arbitrary characters, not 6 | just letters. Use array subscripting to access the characters in the string." 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int capitalize_a(char [], int n); 13 | int capitalize_b(char [], int n); 14 | 15 | int main(void){ 16 | char input_string[7] = {'A', 's', 'h', 'l', 'e', 'y', '\0'}; 17 | capitalize_b(input_string, 7); 18 | printf("%s\n", input_string); 19 | return 0; 20 | } 21 | 22 | int capitalize_a(char str[], int n){ 23 | int i = 0; 24 | 25 | for(i = 0; i < n - 1; i++) 26 | str[i] = toupper(str[i]); 27 | return i; 28 | } 29 | 30 | int capitalize_b(char str[], int n){ 31 | char *sp; 32 | 33 | for(sp = &str[0]; sp < &str[n]; sp++){ 34 | *sp = toupper(*sp); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 6 3 | 4 | "Write a function named censor that modifies a string by replacing every occurance 5 | of foo by xxx. For example, the string "food fool" becomes "xxxd xxxl". Make 6 | the function as short as possible without sacrificing clarity." 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int censor(char str[], int n){ 13 | int i = 0; 14 | while(str[i++] != '\0'){ 15 | if(str[i] == 'f' && str[i + 1] == 'o' && str[i + 2] == 'o'){ 16 | str[i] = 'x'; 17 | str[i + 1] = 'x'; 18 | str[i + 2] = 'x'; 19 | i = i + 2; 20 | } 21 | } 22 | } 23 | 24 | int main(void){ 25 | char input_string[50] = "Welcome to the foody food channel for fools"; 26 | censor(input_string, 50); 27 | printf("%s\n", input_string); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 7 3 | 4 | Suppose that str is an array of characters. Which one of the following statements 5 | is not equivalent to the other three? 6 | 7 | char str[] = "hello"; 8 | 9 | (a) *str = 0; 10 | (b) str[0] = '\0' 11 | (c) strcpy(str, "") 12 | (d) strcat(str, "") 13 | 14 | d is the malcontent! a, b and c in various ways create an empty string. a and b 15 | achieve this by making the first character of the string a null terminator character. c copies an empty string into str. 16 | d appends an empty string to the end of str, rather than the start. 17 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex08.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 8 3 | 4 | What will be the value of the string str after the following statements have been 5 | executed? 6 | 7 | strcpy(str, "tire-bouchon"); 8 | strcpy(&str[4], "d-or-wi"); 9 | starcat(str, "red?"); 10 | 11 | str = "tired-or-wired?\0" 12 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex09.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 9 3 | 4 | What will be the value of the string s1 after the following statements have been 5 | executed? 6 | 7 | strcopy(s1, "computer"); 8 | strcopy(s2, "science"); 9 | if (strcmp(s1, s2) < 0) 10 | strcat(s1, s2); 11 | else 12 | strcat(s2, s1); 13 | s1[strlen(s1) -6] = '\0'; 14 | 15 | s1 = computer 16 | s2 = science 17 | 18 | strcmp compares strings lexicographically, like a dictionary, so a is less than z, 19 | d is more than c etc. In this case, the result of strcmp will be < 0. 20 | 21 | strcat will append science to the string computer to make "computerscience\0". 22 | 23 | The length of "computerscience" is 15 as strlen returns the string length not 24 | including the null terminator. 25 | 26 | s1 = computers\0 27 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex10.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 10 3 | 4 | The following function supposedly creates an identical copy of a string. 5 | What's wrong with the function? 6 | 7 | char *duplicate(const char *p){ 8 | char *q; 9 | strcopy(q, p); 10 | return q; 11 | } 12 | 13 | q is a pointer to a char, but there is no way to deduce that's a string with 14 | a length. There is also no way to ensure that q can fit p. 15 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex11.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 11 3 | 4 | The Q&A section at the end of this chapter shows how the strcmp 5 | function might be written using array subscripting. Modify 6 | the function to use pointer arithmetic instead. 7 | */ 8 | #include 9 | 10 | int strcmp(char *s, char *t){ 11 | for(; *s == *t; s++, t++) 12 | if(*s == '\0') 13 | return 0; 14 | return *s - *t; 15 | } 16 | 17 | int main(){ 18 | char s1[] = "computer"; 19 | char s2[] = "science"; 20 | int result = strcmp(s1, s2); 21 | printf("%d\n", result); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex12.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 12 3 | 4 | Write the following function: 5 | 6 | void get_extension(const char *file_name, cat *extension); 7 | 8 | file_name points to a string containing a file name. The function should store 9 | the extension only, for example if "memo.txt" is passed, the function should 10 | store "txt". 11 | */ 12 | 13 | #include 14 | #include 15 | 16 | void get_extension(const char *file_name, char *extension){ 17 | while(*file_name != '\0'){ //check 18 | if(*file_name++ == '.'){ 19 | strcpy(extension, file_name); 20 | return; 21 | } 22 | } 23 | strcpy(extension, ""); 24 | } 25 | 26 | int main(void){ 27 | char *str = "testfile.txt"; 28 | char ext[20]; 29 | get_extension(str, ext); 30 | printf("Filename: %s, extension: %s\n", str, ext); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex13.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 13 3 | 4 | Write the following function: 5 | void build_index_url(const char *domain, char *index_url); 6 | 7 | Domain points to a string containing an internet domain, such as "knking.com". 8 | This function should add "http://www." to the beginning, and "/index.html" 9 | to the end. 10 | */ 11 | #include 12 | #include 13 | 14 | void build_index_url(const char *domain, char *index_url){ 15 | strcpy(index_url, "http://www."); 16 | strcat(index_url, domain); 17 | strcat(index_url, "/index.html"); 18 | } 19 | 20 | int main(void){ 21 | char complete_url[300]; 22 | build_index_url("knking.com", complete_url); 23 | printf("%s\n", complete_url); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex14.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 14 3 | 4 | What does the following program print? 5 | 6 | #include 7 | 8 | int main(void){ 9 | char s[] = "Hsjodi", *p; 10 | for (p = s; *p; p++) 11 | --*p; 12 | puts(s); 13 | return 0; 14 | } 15 | 16 | This program will loop through string s. The indirection + decrement statement on 17 | line 11 will cause each letter in the string to move back 1 position in the ascii 18 | table. 19 | 20 | H(72) becomes G(71) and s(115) becomes r(114) and so on. This program will output 21 | the word "Grinch". 22 | 23 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex15.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 15 3 | 4 | Let f be the following function: 5 | 6 | int f(char *s, char *t){ 7 | char *p1, *p2; 8 | for(p1 = s; *p1; p++){ 9 | for(p2 = t; *p2; p2++){ 10 | if(*p1 == *p2) break 11 | if (*p2 == '\0') break; 12 | } 13 | } 14 | return p1 - s; 15 | } 16 | 17 | (a) What is the value of f("abcd", "babc")? - 3 18 | (b) What is the value of f("abcd", "bcd")? - 0 19 | (c) In general, what value does f return when passed two strings s and t? 20 | The function f in general will return the first address character that is in S and not in T. 21 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex16.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 16 3 | 4 | Use the techniques of Section 13.6 to condense the count_space function of 5 | Setion 13.4. In particular, replace the for statement by a while loop. 6 | */ 7 | 8 | #include 9 | 10 | int count_space(const char *s){ 11 | int count = 0; 12 | while(*s++) 13 | if(*s == ' ') 14 | count++; 15 | return count; 16 | } 17 | 18 | int main(void){ 19 | char *input_string = "testing testing 1 2 3"; 20 | printf("Expected output = 4, actual output = %d\n", count_space(input_string)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Chapter13/exercises/ex18.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 14, exercise 18 3 | 4 | Write the following function: 5 | Void remove_filename(char *url); 6 | 7 | url points to a string containing a URL that ends with a file name, such as 8 | http://knking.com/index.html"). The function should modify the string 9 | by removing the file name and preceding slash. 10 | */ 11 | #include 12 | #include 13 | 14 | void remove_filename(char *url){ 15 | while(*url++); // loop until pointer is at the end of the string 16 | while(*--url != '/'); // loop back until the last / 17 | *url = '\0'; // cut the end of the string 18 | } 19 | 20 | int main(void){ 21 | char url[] = "www.ashleyksmith.com/index.html"; 22 | remove_filename(url); 23 | printf("%s\n", url); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | *Solution to "C Programming: A modern Approach" chapter 13, exercise 1 3 | 4 | Write parameterized macros that computer the following values 5 | (a) The cube of x 6 | (b) The remaining when n is divided by 4 7 | (c) 1 if the product of x and y is less than 100, 0 otherwise 8 | */ 9 | #include 10 | #include 11 | 12 | #define CUBE(x) ((x) * (x) * (x)) 13 | #define REMAINDER(n) ((n) % 4) 14 | #define MULTIPLY(x, y) ((x) * (y) < 100 ? 1 : 0) 15 | 16 | int main(void){ 17 | printf("C standard pow(5 ^ 3) = %.0f, using CUBE macro = %d\n", pow(5.0, 3), CUBE(5)); 18 | printf("Remainder of 78 divided by 4 is 2, using macro: %d\n", REMAINDER(78)); 19 | printf("5 * 5 is less than 100, expecting 1, actual: %d\n", MULTIPLY(5, 5)); 20 | } 21 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 1 3 | 4 | Write a macro NELEMS(a) that computers the number of elements in a one-deimensions array a. 5 | */ 6 | #include 7 | #include 8 | 9 | #define NELEMS(a) (sizeof((a))) 10 | 11 | int main(void){ 12 | char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; 13 | //-1 removes the string null character from the count. 14 | printf("Number of elements in alphabet is 26, actual: %d\n", NELEMS(alphabet) - 1); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 1 3 | 4 | Let DOUBLE be the following macro: 5 | 6 | #define DOUBLE(x) 2 * x 7 | (a) What is the value of DOUBLE(1+2)? 8 | (b) What is the value of 4 / DOUBLE(2)? 9 | (c) Fix the definition of DOUBLE. 10 | 11 | a. DOUBLE(1+2) becomes 2 * 1 + 2 becomes 3, the intended result is 4. 12 | b. 4 / DOUBLE(2) becomes 4 / 2 * 2 = 4, the intended result is 4 / (2 * 2) = 4. 13 | c. DOUBLE(x) (2 * (x)) 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex04.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 13, exercise 1 3 | 4 | For each of the following macros, give an example that illustrates a problem with the macro 5 | and show how to fix it. 6 | 7 | (a) #define AVG(x, y) (x-y)/2 8 | (b) #define AREA(x, y) (x) * (y) 9 | 10 | a. AVG(10 + 10, 5 + 5) / 2 = (((10 + 10) - 5) + 5) / 2 equates to 10. 11 | Solution: AVG(x, y) (((x) - (y)) / 2) 12 | 13 | b. In a situation where AREA is used with other arithmetic, the order of 14 | precedence can cause unintended behaviour, such as: 15 | 1 / AREA(2, 4) which becomes 1 / 2 * 4; 16 | Solution: AREA(x ,y) ((x) * (y)) 17 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 14, exercise 5 3 | 4 | Let TOUPPER be the following macro: 5 | #define TOUPPER(c) ('a' <= (c) && (c) <= 'z' ? (c) - 'a' + 'A' : (c)) 6 | 7 | Let s be a string and let i be an int variable. Show the output produced by each 8 | of the following program fragments. 9 | 10 | (a) strcpy(s, "abcd"); 11 | i = 0; 12 | putchar(TOUPPER(s[++i])); 13 | 14 | The preprocesser will replace TOUPPER instance with the replacement list. It will test s[1] and s[2] to be 15 | between a and z, which is true and will then captalize s[3] and print to standard output, which is the 16 | character 'D' 17 | 18 | (b) strcpy(s, "0123"); 19 | i = 0; 20 | putchar(TOUPPER(s[++i])); 21 | 22 | The output will be character 2. 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 14, exercise 6 3 | 4 | Write a macro DISP(f, x) that expands into a call of printf that displays the 5 | value of the function f when called with argument x. Write a second macro 6 | that similar to DISP by works for functions with two arguments. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | 13 | #define DISP(f,x) (printf("%s(%.1f) = %.1f\n", (#f), (x), (f(x)))) 14 | #define DISP2(f, x, y) (printf("%s(%.1f, %.1f) = %.1f\n", (#f), (x), (y), (f((x), (y))))) 15 | 16 | int main(void){ 17 | //round being passed as f will be turned into a string by # operator 18 | DISP(round, 64.7); 19 | DISP2(pow, 5.0, 2.0); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 14, exercise 7 3 | 4 | Let GENERIC_MAX be the following macro: 5 | #define GENERIC_MAX(type) 6 | type type##_max(type x, type y){ 7 | return x > y ? : y; 8 | } 9 | 10 | (a) Show the preprocessor's expansion of GENERIC_MAX(long). 11 | long long_max(long x, long y){ 12 | return x > y ? : y; 13 | } 14 | 15 | (b) Explain why GENERIC_MAX doesn't work for basic typed such as unsigned long. 16 | 17 | Because the macro argument is also used to name the function we cannot have 18 | spaced in the function name such as unsigned long unsigned long_max. 19 | 20 | (c) Describe a technique that would allow us to use GENERIC_MAX with basic types 21 | such as unsigned long. 22 | 23 | It's possible to redefine unsigned long, to unsignedlong, ulong or even long 24 | itself in order to overcome the syntax issues. 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter14/exercises/ex09.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 14, exercise 9 3 | 4 | Write the following parameterized macros. 5 | (a) CHECK(x, y, n) Has te value 1 if both x and y falled between 0 and n - 1. 6 | (b) MEDIAN(x, y, z) finds the median of x, y and z 7 | (c) POLYNOMIAL(x) Computes the polynomial 3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #define CHECK(x, y, n) ((x) && (y) < ((n) - 1) && (x) && (y) > 0 ? 1 : 0) 14 | #define MEDIAN(x, y, z) (x) 15 | #define POLYNOMIAL(x) ((3*pow((x),5.0)+(2*pow((x), 4.0))-(5*pow((x),3.0))-((x) * (x)) + (7*(x)) - 6)) 16 | int main(void){ 17 | printf("%d\n", CHECK(10, 5, 4)); 18 | printf("%d\n", MEDIAN(14, 7, 21)); 19 | printf("%.0f\n", POLYNOMIAL(2)); 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter15/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 15, Exercise 2 3 | 4 | Which of the following should not be put in a head file? Why not? 5 | (a) Function prototypes 6 | (b) Function definitions 7 | Should not be kept in a header file. C expects functions to be defined exactly 8 | once. If a function in a header file is included multiple times, then the program 9 | will contain multiple defintions. 10 | (c) Macro defintiions 11 | (d) Type definitions 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter15/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 15, Exercise 2 3 | 4 | we saw that witing #include instead of #include "file" may not work if file 5 | is one that we've written. Would there by any problem with writing #include "file" 6 | instead of #include if file is a system header? 7 | 8 | It will work observing the following behavior: It will search the local directory 9 | for file, if it does not exist it will search the system directory for file. 10 | -------------------------------------------------------------------------------- /Chapter15/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | #Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | #Solution to "C Programming: A modern Approach" Chapter 15, Exercise 5 3 | 4 | #Suppose that a program consists of three source files - main.c, f1.c and f2.c 5 | #plus two header files, f1.h and f2.h. All three source files include f1.h but only 6 | #f1.c and f2.c include f2.h. Write a makefile for this program assuming that 7 | #the compliler is gcc and that the executable file is to be name demo. 8 | 9 | demo: main.o f1.o f2.o 10 | gcc -o demo main.o f1.o f2.o 11 | 12 | main.o: main.c f1.h 13 | gcc -c main.c 14 | 15 | f1.o: f1.c f1.h f2.h 16 | gcc -c f1.c 17 | 18 | f2.o: f2.c f1.h f2.h 19 | gcc -c f2.c 20 | -------------------------------------------------------------------------------- /Chapter15/exercises/ex06.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 15, Exercise 6 3 | 4 | The following questions refer to the program described in exercise 5. 5 | 6 | (a) Which files need to be compiled when the program is built for the first time? 7 | All 8 | (b) If f1.c is changed after the program has been built, which files need to be recompiled? 9 | f1.c 10 | (c) If f1.h is changed after the program has been built, which files need to be recompiled? 11 | main, f1.c, f2.c 12 | (d) If f2.h is changed after the program has been built, which files need to be recompiled? 13 | f1.c, f2.c 14 | 15 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/01/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 15 programming project 1 3 | 4 | The justify program of section 15.3 justifies lines by inserting extra spaces 5 | between words. The way the write_line function currently works, the words closer 6 | to the end of a line tend to have wider gaps between them than the words 7 | at the beginning. Improve the program by having write_line alternate between 8 | putting the larger gaps at the end of the line and putting them at the 9 | beginning of the line. 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/01/makefile: -------------------------------------------------------------------------------- 1 | justify: justify.o line.o word.o 2 | gcc -o justify justify.o line.o word.o 3 | 4 | justify.o: justify.c line.h word.h 5 | gcc -c justify.c 6 | 7 | line.o: line.c line.h 8 | gcc -c line.c 9 | 10 | word.o: word.c word.h 11 | gcc -c word.c 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/01/quote: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/01/word.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* word.c (Chapter 15, page 363) */ 11 | 12 | #include 13 | #include "word.h" 14 | 15 | int read_char(void) 16 | { 17 | int ch = getchar(); 18 | 19 | if (ch == '\n' || ch == '\t') 20 | return ' '; 21 | return ch; 22 | } 23 | 24 | void read_word(char *word, int len) 25 | { 26 | int ch, pos = 0; 27 | 28 | while ((ch = read_char()) == ' ') 29 | ; 30 | while (ch != ' ' && ch != EOF) { 31 | if (pos < len) 32 | word[pos++] = ch; 33 | ch = read_char(); 34 | } 35 | word[pos] = '\0'; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/01/word.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* word.h (Chapter 15, page 361) */ 11 | 12 | #ifndef WORD_H 13 | #define WORD_H 14 | 15 | /********************************************************** 16 | * read_word: Reads the next word from the input and * 17 | * stores it in word. Makes word empty if no * 18 | * word could be read because of end-of-file. * 19 | * Truncates the word if its length exceeds * 20 | * len. * 21 | **********************************************************/ 22 | void read_word(char *word, int len); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/02/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 15 programming project 1 3 | 4 | Modify the justify program by having th read_word function (instead of main) store the * 5 | character at the end of a word that's been truncated. 6 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/02/justify.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* justify.c (Chapter 15, page 363) */ 11 | /* Formats a file of text */ 12 | 13 | #include 14 | #include "line.h" 15 | #include "word.h" 16 | 17 | int main(void) 18 | { 19 | char word[MAX_WORD_LEN+2]; 20 | int word_len; 21 | 22 | clear_line(); 23 | for (;;) { 24 | read_word(word, MAX_WORD_LEN+1); 25 | word_len = strlen(word); 26 | if (word_len == 0) { 27 | flush_line(); 28 | return 0; 29 | } 30 | if (word_len + 1 > space_remaining()) { 31 | write_line(); 32 | clear_line(); 33 | } 34 | add_word(word); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/02/makefile: -------------------------------------------------------------------------------- 1 | justify: justify.o line.o word.o 2 | gcc -o justify justify.o line.o word.o 3 | 4 | justify.o: justify.c line.h word.h 5 | gcc -c justify.c 6 | 7 | line.o: line.c line.h 8 | gcc -c line.c 9 | 10 | word.o: word.c word.h 11 | gcc -c word.c 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/02/quote: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an 2 | enormous success. Although accidents of history of antidistablishmentarianism 3 | surely helped, it evidently satisfied a need 4 | 5 | for a system implementation language efficient 6 | enough to displace assembly language, 7 | yet sufficiently abstract and fluent to describe 8 | algorithms and interactions in a wide variety 9 | of environments. 10 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/03/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 15 programming project 3 3 | 4 | Modify the qsort.c program of section 9.6 so that th quicksort and split functions 5 | are in a seperate file named quicksort.c Create a header file named quicksort.h 6 | that contains prototypes for the two functions and have both the qsort.c and 7 | quicksort.c include this file. 8 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/03/makefile: -------------------------------------------------------------------------------- 1 | # Author: Ashley K. Smith 2 | # Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 3 3 | 4 | qsort: qsort.o quicksort.o 5 | gcc -o qsort qsort.o quicksort.o 6 | 7 | qsort.o: qsort.c quicksort.h 8 | gcc -c qsort.c 9 | 10 | quicksort.o: quicksort.c quicksort.h 11 | gcc -c quicksort.c 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/03/qsort.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* qsort.c (Chapter 9, page 207) */ 11 | /* Sorts an array of integers using Quicksort algorithm */ 12 | 13 | #include 14 | #include "quicksort.h" 15 | #define N 10 16 | 17 | int main(void) 18 | { 19 | int a[N], i; 20 | 21 | printf("Enter %d numbers to be sorted: ", N); 22 | for (i = 0; i < N; i++) 23 | scanf("%d", &a[i]); 24 | 25 | quicksort(a, 0, N - 1); 26 | 27 | printf("In sorted order: "); 28 | for (i = 0; i < N; i++) 29 | printf("%d ", a[i]); 30 | printf("\n"); 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/03/quicksort.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 3 3 | */ 4 | 5 | #include "quicksort.h" 6 | 7 | void quicksort(int a[], int low, int high){ 8 | int middle; 9 | 10 | if (low >= high) return; 11 | 12 | middle = split(a, low, high); 13 | quicksort(a, low, middle - 1); 14 | quicksort(a, middle + 1, high); 15 | } 16 | 17 | int split(int a[], int low, int high){ 18 | int part_element = a[low]; 19 | 20 | for(;;){ 21 | while(low < high && part_element <= a[high]) 22 | high--; 23 | 24 | if (low >= high) break; 25 | a[low++] = a[high]; 26 | 27 | while(low < high && a[low] <= part_element) 28 | low++; 29 | 30 | if(low >= high)break; 31 | a[high--] = a[low]; 32 | } 33 | 34 | a[high] = part_element; 35 | return high; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/03/quicksort.h: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith 2 | * Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 3 3 | */ 4 | 5 | #ifndef QUICKSORT_H 6 | #define QUICKSORT_H 7 | 8 | void quicksort(int a[], int low, int high); 9 | int split(int a[], int low, int high); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/04/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 15 programming project 4 3 | 4 | Modify the remind.c program of section 13.5 so that the read_line function is in 5 | a separate file named remind.c. Create a header file named readline.h that contains 6 | a prototype for the function and have both remind.c and readline.c include this file. 7 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/04/makefile: -------------------------------------------------------------------------------- 1 | # Author: Ashley K. Smith 2 | # Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 4 3 | 4 | remind: remind.o readline.o 5 | gcc -o remind remind.o readline.o 6 | 7 | remind.o: remind.c readline.h 8 | gcc -c remind.c 9 | 10 | readline.o: readline.c readline.h 11 | gcc -c readline.c 12 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/04/readline.c: -------------------------------------------------------------------------------- 1 | /* Ashley K. Smith (github.com/ashksmith) 2 | * Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 4 3 | */ 4 | 5 | #include "readline.h" 6 | #include 7 | int read_line(char str[], int n){ 8 | int ch, i = 0; 9 | 10 | while((ch = getchar() != '\n')) 11 | if (i < n) 12 | str[i++] = ch; 13 | str[i] = '\0'; 14 | return i; 15 | } 16 | -------------------------------------------------------------------------------- /Chapter15/programming-projects/04/readline.h: -------------------------------------------------------------------------------- 1 | /* Ashley K. Smith (github.com/ashksmith) 2 | * Part of the solution to Solution to "C Programming: A Modern Approach" chapter 15, programming project 4 3 | */ 4 | 5 | 6 | #ifndef READLINE_H 7 | #define READLINE_H 8 | 9 | int read_line(char str[], int n); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 1 3 | 4 | In the following declarations, the x and y structures have members named x and y. 5 | 6 | struct { int x, int y} x; 7 | struct { int x, int y} y; 8 | 9 | Are these declarations legal on an individual basis? Cold both declarations appear 10 | as shown in a program? Justify your answer. 11 | 12 | The following declarations are valid because each struct forms it's own scope 13 | or namespace. 14 | 15 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex02.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 16 programming project 2 3 | 4 | (a) Declare structure variables named c1, c2 and c3 each having members real 5 | and imaginary of type double. 6 | 7 | (b) Modify the declaration in part (a) so that c1's members initially have the values 8 | 0.0 and 1.0, while c2's members are 1.0 and 0.0 intilally. 9 | 10 | (c) Write statements that copy the members of c2 into c1. 11 | 12 | (d) Write statements that add the corresponding members of c1 and c2, storing 13 | the result in c3. 14 | */ 15 | 16 | #include 17 | 18 | int main(void){ 19 | // (a) + (b) 20 | struct { 21 | double real; 22 | double imaginary; 23 | } c1 = {0.0, 1.0}, c2 = {1.0, 0.1}, c3; 24 | 25 | // (c) 26 | c1 = c2; 27 | 28 | // (d) 29 | c3.real = c1.real + c2.real; 30 | c3.imaginary = c1.imaginary + c2.imaginary; 31 | 32 | 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex04.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 16 programming project 4 3 | 4 | Repeat exercise 3, but this time using a type named complex. 5 | */ 6 | 7 | #include 8 | 9 | // (a) 10 | typedef struct { double real; double imaginary; } complex; 11 | // (c) 12 | complex make_complex(double d1, double d2){ 13 | complex s = {d1, d2}; 14 | return s; 15 | } 16 | 17 | // (d) 18 | complex add_complex(complex s1, complex s2){ 19 | complex s = {s1.real + s2.real, s1.imaginary + s2.imaginary}; 20 | return s; 21 | } 22 | 23 | int main(void){ 24 | // (b) 25 | complex c1, c2, c3; 26 | 27 | // example 28 | c1 = make_complex(5.0, 10.0); 29 | c2 = make_complex(3.0, 5.0); 30 | c3 = add_complex(c1, c2); 31 | 32 | printf("Expected result: 8, 15. Actual result: %.0f, %.0f\n", c3.real, c3.imaginary); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 16 programming project 6 3 | 4 | Write the following function: 5 | 6 | struct time split_time(long total_seconds); 7 | 8 | total_sectonds is a time represented as the number of seconds since midnight. The 9 | function returns a structure containing the equivalent time in hours, minutes and 10 | seconds. 11 | */ 12 | #include 13 | struct time { int minutes, hours, seconds; }; 14 | 15 | struct time split_time(long total_seconds){ 16 | int i = 0; 17 | struct time t = {0,0,0}; 18 | t.minutes = (total_seconds % 3600) / 60; 19 | t.hours = total_seconds / 3600; 20 | t.seconds = total_seconds % 60; 21 | return t; 22 | } 23 | 24 | int main(void){ 25 | struct time t = split_time(4898); 26 | printf("Expected: 01:21:38, actual: %d:%d:%d\n", t.hours, t.minutes, t.seconds); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex08.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 16 programming project 8 3 | 4 | Let colour be the following structure: 5 | 6 | struct colour { 7 | int red; 8 | int green; 9 | int blue; 10 | }; 11 | 12 | (a) Write a declaration for a const variable named MEGENTA of type struct colour 13 | whose members have the values 255, 0 and 255. 14 | 15 | (b) Repeat part (a) but use a designated initialiser that doesn't specify the value 16 | of green, allowing it to be 0. 17 | */ 18 | 19 | #include 20 | struct colour { int red, green, blue; }; 21 | 22 | int main(void){ 23 | // (a) 24 | const struct colour MAGENTA = {255, 0, 255}; 25 | // (b) 26 | const struct colour MAGENTAB = {.red = 255, .blue = 255}; 27 | 28 | printf("red = %d, green = %d, red = %d\n", MAGENTA.red, MAGENTA.green, MAGENTA.blue); 29 | printf("red = %d, green = %d, red = %d\n", MAGENTAB.red, MAGENTAB.green, MAGENTAB.blue); 30 | } 31 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex11.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 11 3 | 4 | Suppose that s is the following struct: 5 | struct { 6 | double a; 7 | union { 8 | char b[4]; 9 | double c; 10 | int d; 11 | } e; 12 | char f[4]; 13 | }s; 14 | 15 | if char values occupy one byte, int values occupy four bytes, and double values occupy 16 | eight bytes, how much space will a C compiler allocate for? 17 | 18 | Unions will only store enough data for the larger type in it's members, in this 19 | case it's a double, of 8 bytes. The structure contains a double, and a char array 20 | 4 bytes in length. 21 | 22 | Size of struct = 8 bytes + 8 bytes + 4 bytes = 20 bytes. 23 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex12.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 12 3 | 4 | Support that u is the following union: 5 | 6 | union { 7 | double a; 8 | struct { 9 | char b[4]; 10 | double c; 11 | int d; 12 | } e; 13 | char f[4]; 14 | } u; 15 | 16 | Solve the same as exercise 11. How much space will a C compiler allocate for union u? 17 | 18 | size of u = 4 bytes + 8 bytes + 4 bytes = 16 bytes. 19 | 20 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex13.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 13 3 | 4 | Suppose that s is the following structure (point is a structure tag declared in ex10): 5 | 6 | struct shape { 7 | int shape_kind; 8 | struct point center; 9 | union { 10 | struct { 11 | int height, width; 12 | } rectangle; 13 | struct { 14 | int radius; 15 | } circle; 16 | } u; 17 | } s; 18 | 19 | If the value of shape_kind is RECTANGLE, the height and width members store the dimen- 20 | sions of a rectangle. If the value of shape_kind is CIRCLE the radius member stores 21 | the radius of a circle. Indicate which of the following statements are legal, and show 22 | how to repair the ones that aren't: 23 | 24 | (a) s.shape_kind = RECTANGLE; 25 | Legal 26 | (b) s.center.x = 10; 27 | Legal 28 | (c) s.height = 25; 29 | Illegal, s.u.rectangle.height = 25; 30 | (d) s.u.rectangle.width = 8; 31 | Legal 32 | (e) s.u.circle = 5; 33 | Illegal, set kind to CIRCLE first 34 | (f) s.u.radius = 5; 35 | Illegal s.u.circle.radius = 5; 36 | 37 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex15.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 16 programming project 15 3 | 4 | (a) Declare a tag for an enumeration who values represent the seven days of the 5 | week. 6 | (b) Use typedef to define a name for the enumeration of part (a) 7 | */ 8 | 9 | #include 10 | 11 | int main(void){ 12 | // (a) 13 | enum days_of_week {MON, TUE, WED, THU, FRI, SAT, SUN}; 14 | // (b) 15 | typedef enum {MA, TI, KE, TO, PE, LA, SU} Day_Finnish; 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex16.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 16 3 | 4 | Which of the following statements about enumeration constants are true? 5 | (a) An enumeration constant may represent any integer specified by the programmer 6 | True 7 | 8 | (b) Enumeration constants have exactly the same properties as constants created using 9 | #define 10 | 11 | False 12 | 13 | (c) Enumeration have the values 0,1,2... by default. 14 | True 15 | 16 | (d) All constants in an enumeration must have different values. 17 | False 18 | 19 | (e) Enumeration constants may be used as integers in expressions. 20 | True 21 | -------------------------------------------------------------------------------- /Chapter16/exercises/ex17.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 16, Exercise 17 3 | 4 | Suppose that b ad i are declared as follows: 5 | 6 | enum { FALSE, TRUE } b; 7 | int i; 8 | 9 | Which of the following statements are legal? Which ones are "safe" (Always yield a 10 | meaningful result)? 11 | 12 | (a) i = FALSE; 13 | Legal 14 | 15 | (b) b = i; 16 | Legal and unsafe 17 | 18 | (c) b++; 19 | Legal and unsafe 20 | 21 | (d) i = b; 22 | Legal 23 | 24 | (e) i = 2 * b + 1; 25 | Legal 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 exercise 1 3 | 4 | Having to check the return value of malloc (or any other memory allocation 5 | function) each tim we call it can be annoyance. Write a function named my_alloc 6 | that serves as a "wrapper" for malloc. When we call my_malloc and ask it to 7 | allocate n bytes, it in turn calls malloc, tests to make sure that malloc 8 | dosn't return a null pointer, and then return the pointer from malloc. Have 9 | my_malloc print an error message and terminate the program if malloc returns 10 | a NULL pointer. 11 | */ 12 | #include 13 | #include 14 | 15 | void *my_malloc(size_t n){ 16 | void *p = malloc(n); 17 | 18 | printf("%d\n", sizeof(p)); 19 | if(p == NULL){ 20 | printf("Fuck you error\n"); 21 | exit(EXIT_FAILURE); 22 | } 23 | 24 | return p; 25 | } 26 | 27 | int main(void){ 28 | 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 17, Exercise 7 3 | 4 | The following loop is supposed to delete all nodes from a linked list and release 5 | the memory that they occupy. Unfortunately, the loop is incorrect. Explain what's 6 | wrong with it and show how to fix the bug. 7 | 8 | for(p = first; p != NULL; p = p->next) 9 | free(p); 10 | 11 | This loop creates a dangling pointer p. The memory that p points to is recovered before 12 | p is able to point to the next link in the chain in the post statement list. 13 | p points to nothing, attempting to access next will result in undefined behavior. 14 | 15 | This can be fixed by maintaining a pointer to the next link in the chain. 16 | 17 | struct node tmp; 18 | for(p = first; p != NULL;){ 19 | tmp = p->next; 20 | free(p); 21 | p = tmp; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex09.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 17, Exercise 9 3 | 4 | True or false: if x is a structure and a is a member of that structure, then (&x)->a is the same 5 | as x.a. Justify your answer 6 | 7 | struct sample{ 8 | int a; 9 | } x; 10 | 11 | True. (&x)->a is an indirected pointer to a, thus is the same as x.a. 12 | 13 | (&x)->a is the same as doing (*(&x)).a 14 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex10.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 17, Exercise 10 3 | 4 | Modify the print_part function of section 16.2 so that its paramter is a pointer to a part 5 | structure. Use the -> operator in your answer. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | struct part { 12 | int number; 13 | char name[101]; 14 | int on_hand; 15 | } part1 = {5, "Samsung TV", 7}; 16 | 17 | void print_part(struct part *p){ 18 | printf("Part number: %d\n", p->number); 19 | printf("Part name: %s\n", p->name); 20 | printf("Quantity on hand: %d\n", p->on_hand); 21 | } 22 | 23 | int main(void){ 24 | print_part(&part1); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex15.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 17, Exercise 15 3 | 4 | Show the output of the following program, and explain what it does 5 | 6 | #include 7 | int f1(int(*f)(int)); 8 | int f2(int i); 9 | 10 | int main(void){ 11 | printf("Answer: %d\n", f1(f2)); 12 | return 0; 13 | } 14 | 15 | int f1(int (*f)(int)){ 16 | int n = 0; 17 | while((*f)(n)) 18 | n++; 19 | return n; 20 | } 21 | 22 | int f2 (int i){ 23 | return i * i + i - 12; 24 | } 25 | 26 | The program contains two functions. F1 takes arguments of a pointer to an integer function, in this case f2. 27 | f1 will execute f1 while the number of positive, and essentially how many functions calls to f2 were made. 28 | 29 | The output of this program is 3. 30 | -------------------------------------------------------------------------------- /Chapter17/exercises/ex16.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 17, Exercise 16 3 | 4 | Write the following function. The call sum(g, i, j) should return g(i) + ... + g(j) 5 | int sum(int (*f)(int), int start, int end); 6 | */ 7 | 8 | int sum(int (*f)(int), int start, int end){ 9 | int total; 10 | while(start <= end){ 11 | total += (*f)(start); 12 | start++; 13 | } 14 | return start; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/01/inventory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter17/programming-projects/01/inventory -------------------------------------------------------------------------------- /Chapter17/programming-projects/01/makefile: -------------------------------------------------------------------------------- 1 | inventory: inventory.o readline.o 2 | gcc -o inventory inventory.o readline.o 3 | 4 | inventory.o: inventory.c 5 | gcc -c inventory.c 6 | 7 | readline.o: readline.c readline.h 8 | gcc -c readline.c 9 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/01/readline.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* readline.c (Chapter 16, page 395) */ 11 | 12 | #include 13 | #include 14 | #include "readline.h" 15 | 16 | int read_line(char str[], int n) 17 | { 18 | int ch, i = 0; 19 | 20 | while (isspace(ch = getchar())) 21 | ; 22 | while (ch != '\n' && ch != EOF) { 23 | if (i < n) 24 | str[i++] = ch; 25 | ch = getchar(); 26 | } 27 | str[i] = '\0'; 28 | return i; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/02/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 2 3 | 4 | Modify the inventory.c program of section 16.3 so that the p (print) commands calls qsort to sort the inventory 5 | array before it prints the parts. 6 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/02/inventory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter17/programming-projects/02/inventory -------------------------------------------------------------------------------- /Chapter17/programming-projects/02/makefile: -------------------------------------------------------------------------------- 1 | inventory: inventory.o readline.o 2 | gcc -o inventory inventory.o readline.o 3 | 4 | inventory.o: inventory.c 5 | gcc -c inventory.c 6 | 7 | readline.o: readline.c readline.h 8 | gcc -c readline.c 9 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/02/readline.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* readline.c (Chapter 16, page 395) */ 11 | 12 | #include 13 | #include 14 | #include "readline.h" 15 | 16 | int read_line(char str[], int n) 17 | { 18 | int ch, i = 0; 19 | 20 | while (isspace(ch = getchar())) 21 | ; 22 | while (ch != '\n' && ch != EOF) { 23 | if (i < n) 24 | str[i++] = ch; 25 | ch = getchar(); 26 | } 27 | str[i] = '\0'; 28 | return i; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/03/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 3 3 | 4 | Modify the inventory2.c program of section 17.5 by adding an e, erase, command 5 | that allows the user to remove a part from the database. 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/03/inventory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter17/programming-projects/03/inventory -------------------------------------------------------------------------------- /Chapter17/programming-projects/03/makefile: -------------------------------------------------------------------------------- 1 | inventory: inventory.o readline.o 2 | gcc -o inventory inventory.o readline.o 3 | 4 | inventory.o: inventory.c 5 | gcc -c inventory.c 6 | 7 | readline.o: readline.c readline.h 8 | gcc -c readline.c 9 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/03/readline.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* readline.c (Chapter 16, page 395) */ 11 | 12 | #include 13 | #include 14 | #include "readline.h" 15 | 16 | int read_line(char str[], int n) 17 | { 18 | int ch, i = 0; 19 | 20 | while (isspace(ch = getchar())) 21 | ; 22 | while (ch != '\n' && ch != EOF) { 23 | if (i < n) 24 | str[i++] = ch; 25 | ch = getchar(); 26 | } 27 | str[i] = '\0'; 28 | return i; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter17/programming-projects/05/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 5 3 | 4 | Write a program that sorts a series of words entered by the user: 5 | Enter word: foo 6 | Enter word: bar 7 | Enter word: baz 8 | Enter word: quux 9 | 10 | In sorted order: bar baz foo quux 11 | 12 | Assume that each word is no more than 20 characters long. Stop reaading when the user enters an empty word. 13 | Store each word in a dynamically allowcated string, using an array of pointers to keep track of the strings, 14 | as in remind2.c program (section 17.2). After all words have been read, sort the array (Using any sorting 15 | technique) and then use a loop to print the words in sorted order. 16 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 2 3 | 4 | Answer each of the following questions with auto, extern, register and/or static 5 | 6 | (a) which stroage class is used primarily to indicate that a variable or function 7 | can be shared by several files? 8 | 9 | extern 10 | 11 | (b) Suppose that a variable x is to be shared by several functions in one file, but 12 | hidden from functions in other files. Which storage class should x be declared 13 | to have? 14 | 15 | static 16 | 17 | (c) Which storage classes can affect the storage duration of a variable? 18 | 19 | static, extern 20 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 3 3 | 4 | List the storage duration, scope and linkage of each variable and paramter 5 | in the following file: 6 | 7 | extern float a; // static duration, file scope, undefined? 8 | 9 | void f(register double b){ // b: auto duration, block scope, no linkage 10 | static int c; // static duration, block scope and no linkage 11 | auto char d; // auto duration, block scope and no linkage 12 | } 13 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex04.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 4 3 | 4 | Let f be the following function. What will be the value of f(10) if f has never 5 | been called before? What will be the value of f(10) if has been called fives times 6 | previously 7 | 8 | static variables are initialised only once prior to the program execution. 9 | The value of f if f has never been called before is 10 * 0 = 0. 10 | The value of f if f has been called 5 times before is 10 * 4 = 50. 11 | 12 | inf f(int i){ 13 | static int j = 0; 14 | return i * j++; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 5 3 | 4 | State whether each of the following statements is true or false. Justify each answer 5 | 6 | (a) Every variable with static storage duration has file scope 7 | False; static variables inside a block has block scope. 8 | 9 | (b) Every variable declared inside a function has no linkage 10 | False; extern variables have unknown linkage 11 | 12 | (c) Every variable with internal linkage has static duration 13 | True; Variables that are intended to be used with internal linkage, it must 14 | have a value. 15 | 16 | (d) Every parameter has block scope 17 | True; Parameters are passed by value, and are copies of the arguments. 18 | All variables inside a block, have block scope. 19 | 20 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex06.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 6 3 | 4 | The following function is supposed to print an error message. Each message is 5 | preceeded by an integer, indicating the number of times the function has been called 6 | unfortunately, the function always displays 1 as the number of the error message. 7 | Locate the error and show how to fix it without making any changes outside the 8 | function 9 | 10 | void print_error(const char *message){ 11 | int n = 1; 12 | printf("Error %d: %s", n++, message); 13 | } 14 | 15 | In the function above the variable has block scope, and is lost after the block 16 | exits, and reinitialised to 1 each time the function is called. 17 | 18 | The function below fixes this with a static variable that has static storage 19 | duration, the value of n is kept and incremented with each call. 20 | 21 | void print_error(const char *message){ 22 | static int n = 1; 23 | printf("Error %d: %s", n++, message); 24 | } 25 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 7 3 | 4 | Suppose that we declare x to be a const object. Which one of the following 5 | statements about x is false? 6 | 7 | (a) If x of is type int, it can be used as the value of a case label in a switch 8 | statement. 9 | 10 | False, const objects cannot be used in constant expressions, like that of 11 | a switch case. Constant in C refers to literal constants such as 1,2,3 or 12 | 'a', 'b', 'c'. In this sense, contant objects are not constants. 13 | 14 | (b) The compiler will check that no assignment is made to x. 15 | 16 | (c) x is subject to the same scope rules as variables. 17 | 18 | (d) x can be of any type. 19 | 20 | -------------------------------------------------------------------------------- /Chapter18/exercises/ex08.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 18, exercise 8 3 | 4 | Write a complete description of the type of x as specified by each of the following 5 | declarations. 6 | 7 | (a) char (*x[10])(int); 8 | x is an array of 10 pointers to functions with an integer argument that return char 9 | 10 | (b) int (*x(int))[5]; 11 | x is a function with 1 int argument that returns a pointer to an array of 5 integers. 12 | 13 | (c) float *(*x(void))(int); 14 | x is a function that has no parameters 15 | that returns a pointer to a function with one integer argument 16 | that returns a pointer to a float 17 | 18 | (d) void (*x(int, void (*y)(int)))(int); 19 | x is a function with one integer argument, and an argument which points to a function 20 | with one integer argument and no return type, that returns a pointer to an function with 21 | an integer argument and no return type. 22 | 23 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex01.h: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 19, Exercise 1 3 | 4 | A queue is similar to a stack, except that items are added at one and but removed from 5 | the other in a FIFO fashion. Operations on a queue might include: 6 | Inserting an item at the end of a queue 7 | Removing an item from the beginning of the queue 8 | Returning the first item in the queue (Without changing the queue) 9 | Returning the last item in the queue (Without changing the queue) 10 | Testing whether the queue is empty 11 | 12 | Write an interface for the queue module in the form of a header file. 13 | */ 14 | 15 | #ifndef QUEUE_H 16 | #define QUEUE_H 17 | 18 | #include 19 | 20 | typedef int Item; 21 | typedef struct queue_type *Queue; 22 | 23 | void enqueue(Item); 24 | void dequeue(Item); 25 | Item get_first(void); 26 | Item get_last(void); 27 | bool is_queue_empty(void); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex02/README.md: -------------------------------------------------------------------------------- 1 | Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 2 3 | 4 | Modify the stack2.c file to use the PUBLIC and PRIVATE macros. 5 | 6 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex02/stack.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* stack.h (Chapter 19, page 488) */ 11 | 12 | #ifndef STACK_H 13 | #define STACK_H 14 | 15 | #include /* C99 only */ 16 | 17 | void make_empty(void); 18 | bool is_empty(void); 19 | bool is_full(void); 20 | void push(int i); 21 | int pop(void); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/a/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 3 3 | 4 | (a) Write an array based implementation of the queue module described in ex01. 5 | Use three integers to keep track of the queue's status, with one integer storing 6 | the first emprt slot in the array, second for storing the position of the next 7 | item and the third storing the number of items in the queue. An insertion or 8 | removal that would cause either of the 9 | 10 | First two integers to be incrememnted past the end of the array should instead 11 | reset the the variable to zero, thus causing it to wrap around to the beginning 12 | of the array. 13 | 14 | (b) Write a linked-list implementation of the queue module described in Exercise 1. 15 | Use two pointers, one poinging to the first node in the list and the other 16 | pointing to the last node. When an item is inserted into the queue, add it 17 | to the end of the list. When an item is removed from the queue, delete the 18 | first noe in the list. 19 | 20 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/a/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | 4 | int main(void){ 5 | enqueue(5); 6 | enqueue(75); 7 | enqueue(12); 8 | printf("%d\n", peek_first()); 9 | dequeue(); 10 | printf("%d\n", peek_last()); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/a/makefile: -------------------------------------------------------------------------------- 1 | main: client.o queue.o 2 | gcc -o main client.o queue.o 3 | 4 | client.o: client.c queue.h 5 | gcc -c client.c queue.h 6 | 7 | queue.o: queue.c queue.h 8 | gcc -c queue.c queue.h 9 | 10 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/b/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 3 3 | 4 | (a) Write an array based implementation of the queue module described in ex01. 5 | Use three integers to keep track of the queue's status, with one integer storing 6 | the first emprt slot in the array, second for storing the position of the next 7 | item and the third storing the number of items in the queue. An insertion or 8 | removal that would cause either of the 9 | 10 | First two integers to be incrememnted past the end of the array should instead 11 | reset the the variable to zero, thus causing it to wrap around to the beginning 12 | of the array. 13 | 14 | (b) Write a linked-list implementation of the queue module described in Exercise 1. 15 | Use two pointers, one poinging to the first node in the list and the other 16 | pointing to the last node. When an item is inserted into the queue, add it 17 | to the end of the list. When an item is removed from the queue, delete the 18 | first noe in the list. 19 | 20 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/b/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | 4 | int main(void){ 5 | enqueue(12); 6 | printf("Last: %d, First: %d\n", peek_last(), peek_first()); 7 | enqueue(15); 8 | printf("Last: %d, First: %d\n", peek_last(), peek_first()); 9 | enqueue(27); 10 | printf("Last: %d, First: %d\n", peek_last(), peek_first()); 11 | dequeue(); 12 | printf("Last: %d, First: %d\n", peek_last(), peek_first()); 13 | dequeue(); 14 | dequeue(); 15 | dequeue(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/b/makefile: -------------------------------------------------------------------------------- 1 | main: client.o queue.o 2 | gcc -o main client.o queue.o 3 | 4 | client.o: client.c queue.h 5 | gcc -c client.c queue.h 6 | 7 | queue.o: queue.c queue.h 8 | gcc -c queue.c queue.h 9 | 10 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex03/b/queue.h: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" Chapter 19, Exercise 1 3 | 4 | A queue is similar to a stack, except that items are added at one and but removed from 5 | the other in a FIFO fashion. Operations on a queue might include: 6 | Inserting an item at the end of a queue 7 | Removing an item from the beginning of the queue 8 | Returning the first item in the queue (Without changing the queue) 9 | Returning the last item in the queue (Without changing the queue) 10 | Testing whether the queue is empty 11 | 12 | Write an interface for the queue module in the form of a header file. 13 | */ 14 | 15 | #ifndef QUEUE_H 16 | #define QUEUE_H 17 | 18 | #include 19 | 20 | typedef int Item; 21 | 22 | void enqueue(Item); 23 | 24 | Item dequeue(void); 25 | 26 | Item peek_first(void); 27 | 28 | Item peek_last(void); 29 | 30 | bool is_queue_empty(void); 31 | #endif 32 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/a/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 4 3 | 4 | (a) Write an implementation of the Stack type, assuming that Stack is a structure 5 | containing a fixed length array. 6 | 7 | (b) Redo the Stack type, this time using a linked-list representation instead of 8 | an array. 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/a/client.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | 4 | int main(void){ 5 | Stack s1; 6 | s1.top = 0; 7 | push(&s1, 50); 8 | push(&s1, 27); 9 | printf("%d\n", pop(&s1)); 10 | push(&s1, 209); 11 | printf("%d\n", pop(&s1)); 12 | printf("%d\n", pop(&s1)); 13 | printf("%d\n", pop(&s1)); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/a/makefile: -------------------------------------------------------------------------------- 1 | main.out: client.o stack.o 2 | gcc -o main.out client.o stack.o 3 | 4 | client.o: client.c stack.h 5 | gcc -c client.c 6 | 7 | stack.o: stack.c stack.h 8 | gcc -c stack.c 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/a/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stack.h" 4 | 5 | bool is_empty(Stack *s){ 6 | if(s->top == 0) 7 | return true; 8 | return false; 9 | } 10 | 11 | bool is_full(Stack *s){ 12 | if(s->top >= MAX_CONTENTS_SIZE) 13 | return true; 14 | return false; 15 | 16 | } 17 | 18 | void push(Stack *s, int i){ 19 | if(is_full(s)){ 20 | printf("Overflow\n"); 21 | exit(EXIT_FAILURE); 22 | } 23 | s->top++; 24 | s->contents[s->top] = i; 25 | } 26 | 27 | int pop(Stack *s){ 28 | if(is_empty(s) == true){ 29 | printf("Underflow\n"); 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | return s->contents[s->top--]; 34 | } 35 | 36 | void empty_stack(Stack *s){ 37 | if(is_empty(s)) 38 | return; 39 | for(; s->top >= 0; s->top--) 40 | s->contents[s->top] = 0; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/a/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | #define MAX_CONTENTS_SIZE 25 5 | 6 | #include 7 | 8 | typedef struct { 9 | int top; 10 | int contents[MAX_CONTENTS_SIZE]; 11 | } Stack; 12 | 13 | void empty_stack(Stack *); 14 | bool is_empty(Stack *); 15 | bool is_full(Stack *); 16 | void push(Stack *, int); 17 | int pop(Stack *); 18 | 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/b/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 4 3 | 4 | (a) Write an implementation of the Stack type, assuming that Stack is a structure 5 | containing a fixed length array. 6 | 7 | (b) Redo the Stack type, this time using a linked-list representation instead of 8 | an array. 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/b/client.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | 4 | int main(void){ 5 | Stack s1; 6 | s1 = create(); 7 | push(s1, 5); 8 | push(s1, 10); 9 | pop(s1); 10 | pop(s1); 11 | push(s1, 500); 12 | push(s1, 102); 13 | pop(s1); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/b/makefile: -------------------------------------------------------------------------------- 1 | main.out: client.o stack.o 2 | gcc -o main.out client.o stack.o 3 | 4 | client.o: client.c stack.h 5 | gcc -c client.c 6 | 7 | stack.o: stack.c stack.h 8 | gcc -c stack.c 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/b/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stack.h" 4 | 5 | struct node { 6 | int item; 7 | struct node *next; 8 | }; 9 | 10 | struct struct_type { 11 | struct node *top; 12 | }; 13 | 14 | void push(Stack s, int i){ 15 | printf("Pushing: %d\n", i); 16 | struct node *n = malloc(sizeof(struct node)); 17 | n->item = i; 18 | n->next = s->top; 19 | s->top = n; 20 | } 21 | 22 | int pop(Stack s){ 23 | if(is_empty(s) == true){ 24 | printf("Stack underflow\n"); 25 | exit(EXIT_FAILURE); 26 | } 27 | int i = s->top->item; 28 | s->top = s->top->next; 29 | printf("Popping: %d\n", i); 30 | return i; 31 | } 32 | 33 | bool is_empty(Stack s){ 34 | if(s->top == NULL){ 35 | printf("Stack is empty\n"); 36 | return true; 37 | } 38 | return false; 39 | } 40 | 41 | Stack create(){ 42 | Stack s; 43 | s = malloc(sizeof(struct struct_type)); 44 | s->top = NULL; 45 | return s; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex04/b/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | #define MAX_CONTENTS_SIZE 25 5 | 6 | #include 7 | 8 | typedef struct struct_type *Stack; 9 | Stack create(); 10 | void empty_stack(Stack); 11 | bool is_empty(Stack); 12 | void push(Stack, int); 13 | int pop(Stack); 14 | 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/README.md: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 17 programming project 6 3 | 4 | (a) Add a peek function to stackADT.c. This function will have a parameter of type 5 | Stack. When called, it returns the top item on the stack but dosn't modify the stack. 6 | 7 | (b) Repeat part (a), modifying stackADT2.c this time. 8 | 9 | (c) Repeat part (a), modifying stackADT3.c this time. 10 | 11 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/a/makefile: -------------------------------------------------------------------------------- 1 | main: stack.o stackclient.o 2 | gcc -o main stackADT.o stackclient.o 3 | 4 | stack.o: stackADT.c stackADT.h 5 | gcc -c stackADT.c 6 | 7 | stackclient.o: stackclient.c stackADT.h 8 | gcc -c stackclient.c 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/a/stackADT.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* stackADT.h (Chapter 19, page 496) */ 11 | 12 | #ifndef STACKADT_H 13 | #define STACKADT_H 14 | 15 | #include /* C99 only */ 16 | 17 | typedef int Item; 18 | 19 | typedef struct stack_type *Stack; 20 | 21 | Stack create(void); 22 | void destroy(Stack s); 23 | void make_empty(Stack s); 24 | bool is_empty(Stack s); 25 | bool is_full(Stack s); 26 | void push(Stack s, Item i); 27 | Item pop(Stack s); 28 | Item peek(Stack s); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/b/makefile: -------------------------------------------------------------------------------- 1 | main: stack.o stackclient.o 2 | gcc -o main stackADT2.o stackclient.o 3 | 4 | stack.o: stackADT2.c stackADT2.h 5 | gcc -c stackADT2.c 6 | 7 | stackclient.o: stackclient.c stackADT2.h 8 | gcc -c stackclient.c 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/b/stackADT2.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* stackADT2.h (Chapter 19, page 498) */ 11 | 12 | #ifndef STACKADT_H 13 | #define STACKADT_H 14 | 15 | #include /* C99 only */ 16 | 17 | typedef int Item; 18 | 19 | typedef struct stack_type *Stack; 20 | 21 | Stack create(int size); 22 | void destroy(Stack s); 23 | void make_empty(Stack s); 24 | bool is_empty(Stack s); 25 | bool is_full(Stack s); 26 | void push(Stack s, Item i); 27 | Item pop(Stack s); 28 | Item peek(Stack s); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/c/makefile: -------------------------------------------------------------------------------- 1 | main: stack.o stackclient.o 2 | gcc -o main stackADT3.o stackclient.o 3 | 4 | stack.o: stackADT3.c stackADT.h 5 | gcc -c stackADT3.c 6 | 7 | stackclient.o: stackclient.c stackADT.h 8 | gcc -c stackclient.c 9 | -------------------------------------------------------------------------------- /Chapter19/exercises/ex06/c/stackADT.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* stackADT.h (Chapter 19, page 496) */ 11 | 12 | #ifndef STACKADT_H 13 | #define STACKADT_H 14 | 15 | #include /* C99 only */ 16 | 17 | typedef int Item; 18 | 19 | typedef struct stack_type *Stack; 20 | 21 | Stack create(void); 22 | void destroy(Stack s); 23 | void make_empty(Stack s); 24 | bool is_empty(Stack s); 25 | bool is_full(Stack s); 26 | void push(Stack s, Item i); 27 | Item pop(Stack s); 28 | Item peek(Stack s); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Chapter19/programming-projects/pp01/client.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | 3 | int main(void){ 4 | Stack s = create(); 5 | 6 | char ch; 7 | while((ch = getchar()) != '\n'){ 8 | if(ch == '(' || ch == '{') 9 | push(s, ch); 10 | 11 | if(ch == '}' && pop(s) != '{'){ 12 | printf("Braces / Parentheses not nested correctly\n"); 13 | break; 14 | } else if(ch == ')' && pop(s) != '('){ 15 | printf("Braces / Parentheses not nested correcntly\n"); 16 | break; 17 | } 18 | 19 | } 20 | 21 | if(is_empty(s)){ 22 | printf("Braces / Parentheses matched correctly\n"); 23 | } else { 24 | printf("Braces / Parentheses did not match\n"); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter19/programming-projects/pp01/makefile: -------------------------------------------------------------------------------- 1 | stack.out: client.o stack.o 2 | gcc -o stack.out client.o stack.o 3 | 4 | client.o: client.c stack.h 5 | gcc -c client.c 6 | 7 | stack.o: stack.c stack.h 8 | gcc -c stack.c 9 | -------------------------------------------------------------------------------- /Chapter19/programming-projects/pp01/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | #include 4 | #include 5 | #include 6 | 7 | #define STACK_SIZE 100 8 | 9 | typedef struct stack_type *Stack; 10 | 11 | Stack create(void); 12 | void make_empty(void); 13 | bool is_empty(Stack); 14 | bool is_full(Stack); 15 | void push(Stack, char ch); 16 | char pop(Stack); 17 | int stack_underflow(); 18 | int stack_overflow(); 19 | char peek(Stack); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /Chapter20/exercises/ex05.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 20, exercise 1 3 | 4 | Write macros named GET_RED, GET_GREEN and GET_BLUE that when given a 5 | colour as an argument return it's 8bit red, green and blue intensities. 6 | 7 | */ 8 | 9 | #define MK_COLOUR(r,g,b) ((long) (b) << 16 | (g) << 8 | (r)) 10 | #define GET_RED(i) ((char) (i >> 0) & 0xff) 11 | #define GET_GREEN(i) ((char) (i >> 8) & 0xff) 12 | #define GET_BLUE(i) ((char) (i >> 16) & 0xff) 13 | #include 14 | 15 | int main(void){ 16 | long i = MK_COLOUR(28,93,42); 17 | printf("R G B\n"); 18 | 19 | printf("%d:%d:%d\n", GET_RED(i), GET_GREEN(i), GET_BLUE(i)); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter20/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 20, exercise 6 3 | 4 | Use the bitwise operators to write the folowing function: 5 | 6 | unsigned short swap_bytes(unsigned short i); 7 | 8 | a 9 | swap bytes should return the number that results from swapping the two bytes 10 | in i. For example, if i has the value 0x1234 (00010010 00110100 in binary) 11 | then swap bytes should return 0x3412 (00110100 00010010 in binary). 12 | 13 | b 14 | Condense the function into one statement 15 | 16 | */ 17 | 18 | #include 19 | unsigned short swap_bytes(unsigned short i); 20 | 21 | int main(void){ 22 | unsigned short x = swap_bytes(440); 23 | printf("%d\n", x); 24 | 25 | 26 | return 0; 27 | } 28 | // part a and b 29 | unsigned short swap_bytes(unsigned short i){ 30 | return (i << 8) | (i >> 8); 31 | } 32 | -------------------------------------------------------------------------------- /Chapter20/exercises/ex08.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 20, exercise 8 3 | 4 | Let f be the following function: 5 | unsigned int f(unsigned int i, int m, int n){ 6 | return (i >> (m + 1)) & ~(~0 << n); 7 | } 8 | 9 | (a) What is the value ~(~0 << n)? 10 | It's the same as ~(1 << n). 11 | 12 | 13 | (b) What does this function do? 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter20/exercises/ex12.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 20, exercise 12 3 | 4 | The following function supposedly combines two bytes to form an unsigned short 5 | integer. Explain why the function dosn't work and show how to fix it. 6 | 7 | unsigned short create_short(unsigned char high_byte, unsigned char low_byte){ 8 | return high_byte << 8 + low_byte; 9 | } 10 | 11 | Just like before, the problem is operator precedence. This function is returning high_byte 12 | left shifted 8 + low_bytes to the left. The problem is remedied by using parthentheses. 13 | 14 | unsigned short create_short(unsigned char high_byte, unsigned char low_byte){ 15 | return (high_byte << 8) + low_byte; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Chapter20/exercises/ex13.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 20, exercise 13 3 | 4 | If n is an unsigned int variable, what effect does the following statement have on the 5 | bits in n? 6 | 7 | n &= n - 1; 8 | 9 | Each time the statement is executed, we remove 1, or the least significant bit. 10 | -------------------------------------------------------------------------------- /Chapter21/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 21, exercise 2 3 | 4 | Having located the header files on your system, find a standard header which a macro hides 5 | a function. 6 | 7 | In dirent.h the following function is hidden by a macro, depending on two conditions. 8 | 9 | /* Return the file descriptor used by DIRP. */ 10 | extern int dirfd (DIR *__dirp) __THROW __nonnull ((1)); 11 | 12 | # if defined __OPTIMIZE__ && defined _DIR_dirfd 13 | # define dirfd(dirp) _DIR_dirfd (dirp) 14 | # endif 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter21/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 21, exercise 3 3 | 4 | When a macro hides a function, which come first in the header file: The macro 5 | definition or the function prototype? Justify your answer. 6 | 7 | The function prototype must always come before the macro. Without the prototype 8 | the macro cannot be expanded properly. GCC won't even let you place the prototype 9 | and macro in the wrong order. 10 | -------------------------------------------------------------------------------- /Chapter21/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 21, exercise 5 3 | 4 | The islower function, which belongs to , tests whether a character is a 5 | legal lower case letter. Why would the following macro version of islower not be 6 | legal according to the C standard? (You may assume that the character set is ASCII). 7 | 8 | #define islower(c) ((c) >= 'a' && (c) <= 'z') 9 | 10 | As seen in a previous chapter, using a macro such as this runs the possibility of 11 | evaluating the argument c twice. The C standard specifies that a macro must only 12 | evaluate it's arguments exactly once. 13 | -------------------------------------------------------------------------------- /Chapter21/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 21, exercise 7 3 | 4 | In which standard header would your expect to find each of the following? 5 | 6 | (a) A function that determines the current day of the week 7 | time.h 8 | 9 | (b) A function that tests whether a character is a digit 10 | ctype.h 11 | 12 | (c) A macro that gives the largest unsined in value 13 | limits.h / stdint 14 | 15 | (d) A function that rounds a floating point number to the next higher integer 16 | math.h 17 | 18 | (e) A macro that specifies the number of bits in a character 19 | limits.h 20 | 21 | (f) A macro that specifies the number of significant digits in a double variable 22 | float.h 23 | 24 | (g) A function that searches a string for a particular character 25 | string.h 26 | 27 | (h) A function that opens a file for reading 28 | stdio.h 29 | -------------------------------------------------------------------------------- /Chapter21/programming-projects/pp01.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 21, programming project 1 3 | 4 | Write a program that declares the s structure (see Section 21.4) and prints the 5 | sizes and offsets of a, b and c members. Have the program print the size of the 6 | entire structure as well. From this information, determine whether or not the 7 | structure has any holes. If it does, describe the location and size of each 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | struct s { 14 | char a; 15 | int b[2]; 16 | float c; 17 | } s; 18 | 19 | int main(void){ 20 | // should be 0, 8, 12 or so 21 | printf("a offset: %d\n", offsetof(struct s, a)); 22 | printf("a size: %d\n", sizeof(s.a)); 23 | printf("b offset: %d\n", offsetof(struct s, b)); 24 | printf("b size: %d\n", sizeof(s.b)); 25 | printf("c offset: %d\n", offsetof(struct s, c)); 26 | printf("c size: %d\n", sizeof(s.c)); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex01.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 1 3 | 4 | Indicate whether each of the followin files is more likely to contain 5 | text data or a binary data: 6 | 7 | (a) A file of object code produced by a compiler 8 | Binary 9 | 10 | (b) a program listing prodced by a C compiler 11 | A listing needs to be human readable: Text 12 | 13 | (c) An email message sent from one computer to another 14 | Text 15 | 16 | (d) A file containing a graphics image 17 | Binary, with some human readable meta information and headers 18 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex02.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 2 3 | 4 | Indicate which mode string is most likely to be passed to fopen in each 5 | of the following situations: 6 | 7 | (a) A database management system opens a file containing records to be 8 | updated. 9 | "rb+" 10 | 11 | (b) A mail program opens a file of saved messages so that it can add 12 | additonal messages to the end. 13 | 14 | "a" 15 | 16 | (c) A graphics program opens a file containing a picture to be displayed 17 | on the screen. 18 | "rb" 19 | 20 | (d) An operating system command interpreter opens a "shell script" 21 | containing commands to be executed. 22 | "r" 23 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex03.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 3 3 | 4 | Find the error in the following program and show how to fix it: 5 | 6 | FILE *fp; 7 | 8 | if(fp = fopen(filename, "r")){ 9 | // Read characters until nd of file 10 | } 11 | fclose(fp); 12 | 13 | The program never tests fp for a NULL pointer. Closing a NULL pointer 14 | results in an error. 15 | 16 | FILE *fp 17 | 18 | if((fp = fopen(filename, "r")) != NULL){ 19 | // read charachers 20 | } else { 21 | // handle error, exit 22 | } 23 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex05.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 6 3 | 4 | Is there any different between the printf conversion specifications %.4d and %04d? If so 5 | explain what it is. 6 | 7 | When precision is used with an integer value, it specifies the minimum number of digits. Leading 8 | zeroes will be added if the number has fewer than specified. 9 | 10 | %04d specifies a minimum field width of 4, with padded leading zeroes. 11 | 12 | They are the same. 13 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 6 3 | 4 | Write a call of printf that prints: 5 | 1 widget 6 | 7 | if the widget variable (of type int) has the value 1, and 8 | 9 | n widgets otherwise 10 | 11 | Where n is the value of widget. You are not allowed to the if statement or any other statement; 12 | the answer must be a single call of printf. 13 | */ 14 | #include 15 | 16 | int main(void){ 17 | int widget = 1; 18 | printf("%d widget%s\n", widget, (widget == 1) ? "" : "s"); 19 | widget = 5; 20 | printf("%d widget%s\n", widget, (widget == 1) ? "" : "s"); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex07.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 7 3 | 4 | Suppose the call scanf as follows: 5 | 6 | n = scanf("%d%f%d", &i, &x, &j); 7 | 8 | (i, j and n are int variables and x is a float variable.) Assuming that the input stream contains 9 | the characters shown, give the value of i, j and n and x after the call. In addition, indicate 10 | which characters were consumed by the call. 11 | 12 | int = int float int 13 | 14 | (a) 10 20 30 15 | i = 10, x = 20, j = 30, n = 3 16 | 17 | (b) 1.0 2.0 3.0 18 | i = 1, x = 2.0, j = 3, n = 3 19 | 20 | (c) 0.1 0.2 0.3 21 | i = 0. x = 0.1, j = 0, n = 3 22 | 23 | (d) .1 .2 .3 24 | i = undefined, x = undefined, j = undefined, n = 0 25 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex09.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 9 3 | 4 | Which one of the following calls is not a valid way of reading one character from the 5 | standard input stream? 6 | 7 | (a) getch() 8 | (b) getchar() 9 | (c) getc(stdin) 10 | (d) fgetc(stdin) 11 | 12 | getch, it's not a function in the standard library. 13 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex11.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 11 3 | 4 | The following loop appears in the fcopy.c program: 5 | 6 | while((ch = getc(source_fp)) != EOF) 7 | putc(ch, dest_fp); 8 | 9 | Supose that we neglected to put parentheses around ch = getc(source_fp): 10 | 11 | while(ch = (getc(source_fp) != EOF)) 12 | putc(ch, dest_fp); 13 | 14 | Would the program compile without an error? If so, what would the program 15 | do when it's run? 16 | 17 | There is an issue with the order of precedence in the program. The != 18 | operator has a greater precedence than assignment. The program will first 19 | test that getc(source_fp) does not equal EOF and return a boolean value. 20 | which is then assigned to ch. ch will be equal to 1 (SOH) until it hits 21 | EOF, then it will be 0 (NUL) 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex12.txt: -------------------------------------------------------------------------------- 1 | Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 12 3 | 4 | Find the error in the following function and show how to fix it. 5 | 6 | int count_periods(const char *filename){ 7 | FILE *fp; 8 | int n = 0; 9 | 10 | if((fp = fopen(filename, "r")) != NULL){ 11 | while(fgetc(fp) != EOF) 12 | if(fgetc(fp) == '.') 13 | n++; 14 | fclose(fp); 15 | } 16 | return n; 17 | } 18 | 19 | 20 | The error is that fgetc is called twice. In each call of the function a new charact- 21 | er is taken from it's argument, in effect skipping a letter. 22 | -------------------------------------------------------------------------------- /Chapter22/exercises/ex15.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | Solution to "C Programming: A modern Approach" chapter 22, exercise 15 3 | 4 | Write calls fo fseek that perform the following file-position operations on a 5 | binary file who data is arranged as 64byte "records". se fp as the file pointer 6 | in each case. 7 | 8 | (a) Move to the beginning of record n. (Assume that the first record in the file 9 | is record 0.) 10 | 11 | (b) Move to the beginning of the last record in the file. 12 | 13 | (c) Move foward one record. 14 | 15 | (d) Move backwards two records. 16 | */ 17 | #include 18 | int main(void){ 19 | fseek(fp, 0L, SEEK_END); 20 | fseek(fp, -64L, SEEK_CUR); 21 | fseek(fp, 64L, SEEK_CUR); 22 | fseek(FP, -128L, SEEK_CUR); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter22/programming-projects/08/.pp08.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter22/programming-projects/08/.pp08.c.swp -------------------------------------------------------------------------------- /Chapter22/programming-projects/08/readline.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * From C PROGRAMMING: A MODERN APPROACH, Second Edition * 3 | * By K. N. King * 4 | * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * 5 | * All rights reserved. * 6 | * This program may be freely distributed for class use, * 7 | * provided that this copyright notice is retained. * 8 | *********************************************************/ 9 | 10 | /* readline.c (Chapter 16, page 395) */ 11 | 12 | #include 13 | #include 14 | #include "readline.h" 15 | 16 | int read_line(char str[], int n) 17 | { 18 | int ch, i = 0; 19 | 20 | while (isspace(ch = getchar())) 21 | ; 22 | while (ch != '\n' && ch != EOF) { 23 | if (i < n) 24 | str[i++] = ch; 25 | ch = getchar(); 26 | } 27 | str[i] = '\0'; 28 | return i; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter22/programming-projects/08/stock.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashksmith/c-programming-a-modern-approach-solutions/f66cc6d1fa7a68c20c1d7a2c59f08a2f1bb31626/Chapter22/programming-projects/08/stock.dat -------------------------------------------------------------------------------- /Chapter22/programming-projects/pp06.c: -------------------------------------------------------------------------------- 1 | /* Author: Ashley K. Smith (https://github.com/ashksmith/) 2 | * Solution to "C Programming: A modern Approach" chapter 22 programming project 6 3 | 4 | Write a program that displays the contents of a file as bytes and as characters. 5 | Have the user specifiy the file name on the command line. Each line should 6 | show 10 bytes from the file as hexadecimal numbers and as characters. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main(int argc, char *argv[]){ 14 | printf("Offset Bytes characters\n"); 15 | printf("------ ----------------------------- ----------\n"); 16 | 17 | FILE *fp; int ch; 18 | if((fp = fopen(argv[1], "rb")) == NULL){ 19 | printf("Has been an error\n"); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | int x = 10, i = 0, y = 0; 24 | char *bytes[29], *characters[10]; 25 | while((ch = (char)fgetc(fp)) != EOF){ 26 | while(ftell(fp) < x){ 27 | printf("%6d %-29s %-10s", x - 10, "", ""); 28 | } 29 | x *= 10; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Programming: a Modern Approach Solutions 2 | 3 | My solutions to the exercises from the book "C Programming: A Modern Approach" by Kim N. King 4 | --------------------------------------------------------------------------------