├── Ch22_Input_Output ├── ch22_prog_proj_07 │ ├── a │ │ ├── foo.txt │ │ └── foo.txt.rle │ └── b │ │ ├── foo.txt │ │ └── foo.txt.rle ├── ch22_prog_proj_08 │ ├── readline.c │ └── readline.h ├── ch22_prog_proj_04a.c ├── ch22_prog_proj_06.c ├── ch22_prog_proj_05.c ├── ch22_prog_proj_02.c ├── ch22_prog_proj_03.c ├── ch22_prog_proj_04c.c ├── ch22_prog_proj_04b.c └── ch22_prog_proj_01.c ├── Ch15_Writing_Large_Programs ├── ch15_prog_proj_04 │ ├── readline.h │ ├── readline.c │ ├── Makefile │ └── remind.c ├── ch15_prog_proj_03 │ ├── quicksort.h │ ├── Makefile │ ├── qsort.c │ └── quicksort.c ├── ch15_prog_proj_01 │ ├── newquote.txt │ ├── newquote_write_line_edit.txt │ ├── Makefile │ ├── quote.txt │ ├── word.c │ ├── word.h │ ├── justify.c │ ├── line.c │ └── line.h ├── ch15_prog_proj_05 │ ├── Makefile │ ├── stack.h │ ├── stack.c │ └── main.c └── ch15_prog_proj_02 │ ├── Makefile │ ├── word.c │ ├── word.h │ ├── justify.c │ ├── line.c │ └── line.h ├── Ch13_Strings ├── ch13_prog_proj_05 │ ├── Makefile │ └── sum.c ├── ch13_prog_proj_04 │ ├── Makefile │ └── reverse.c ├── ch13_prog_proj_06 │ ├── Makefile │ └── planet.c ├── ch13_prog_proj_18.c ├── ch13_prog_proj_16.c ├── ch13_prog_proj_08.c ├── ch13_prog_proj_13.c ├── ch13_prog_proj_17.c ├── ch13_prog_proj_03.c ├── ch13_prog_proj_12.c ├── ch13_prog_proj_07.c ├── ch13_prog_proj_09.c ├── ch13_prog_proj_01.c ├── ch13_prog_proj_11.c ├── ch13_prog_proj_14.c ├── ch13_prog_proj_10.c └── ch13_prog_proj_02.c ├── Ch04_Expressions ├── ch4_prog_proj_01.c ├── ch4_prog_proj_02.c ├── ch4_prog_proj_03.c ├── ch4_prog_proj_04.c ├── ch4_prog_proj_06.c └── ch4_prog_proj_05.c ├── Ch02_C_Fundamentals ├── ch2_prog_proj_04.c ├── ch2_prog_proj_02.c ├── ch2_prog_proj_06.c ├── ch2_prog_proj_03.c ├── ch2_prog_proj_05.c ├── ch2_prog_proj_01.c ├── ch2_prog_proj_07.c └── ch2_prog_proj_08.c ├── Ch03_Formatted_Input_Output ├── ch3_prog_proj_01.c ├── ch3_prog_proj_04.c ├── ch3_prog_proj_03.c ├── ch3_prog_proj_02.c ├── ch3_prog_proj_06.c └── ch3_prog_proj_05.c ├── Ch06_Loops ├── ch6_prog_proj_06.c ├── ch6_prog_proj_05.c ├── ch6_prog_proj_01.c ├── ch6_prog_proj_03.c ├── ch6_prog_proj_08.c ├── ch6_prog_proj_02.c ├── ch6_prog_proj_11.c ├── ch6_prog_proj_12.c ├── ch6_prog_proj_09.c ├── ch6_prog_proj_07.c ├── ch6_prog_proj_10.c └── ch6_prog_proj_04.c ├── Ch17_Advanced_Uses_of_Pointers ├── ch17_prog_proj_02 │ ├── readline.c │ ├── Makefile │ └── readline.h ├── ch17_prog_proj_03 │ ├── readline.c │ ├── Makefile │ └── readline.h ├── ch17_prog_proj_01 │ ├── Makefile │ ├── readline.c │ └── readline.h ├── ch17_prog_proj_04 │ ├── Makefile │ ├── quote.txt │ ├── word.c │ ├── word.h │ ├── justify.c │ └── line.h └── ch17_prog_proj_06.c ├── Ch16_Structures_Unions_and_Enumerations ├── ch16_prog_proj_04 │ ├── readline.c │ ├── Makefile │ └── readline.h ├── ch16_prog_proj_02 │ ├── Makefile │ ├── readline.c │ └── readline.h ├── ch16_prog_proj_03 │ ├── Makefile │ ├── readline.c │ └── readline.h ├── ch16_prog_proj_06.c └── ch16_prog_proj_01.c ├── Ch19_Program_Design ├── ch19_exercise_04 │ ├── a │ │ ├── stack.h │ │ └── stack.c │ └── b │ │ └── stack.h ├── ch19_prog_proj_01 │ ├── stackADT.h │ ├── main.c │ └── stackADT.c ├── ch19_prog_proj_02 │ ├── stackADT.h │ ├── stackADT.c │ └── rpn.c ├── ch19_exercise_06 │ ├── b │ │ └── stackADT2.h │ ├── a │ │ ├── stackADT.h │ │ └── stackADT.c │ └── c │ │ └── stackADT.h ├── ch19_exercise_07 │ └── stackADT2.h ├── ch19_exercise_01 │ └── queue.h ├── ch19_exercise_03 │ ├── a │ │ ├── queue.h │ │ └── queue1.c │ └── b │ │ ├── queue.h │ │ └── queue2.c ├── ch19_prog_proj_05 │ ├── queueADT.h │ └── queueclient.c ├── ch19_prog_proj_07 │ ├── queueADT.h │ └── queueclient.c ├── ch19_prog_proj_06 │ ├── queueADT2.h │ └── queueclient.c ├── ch19_exercise_05 │ ├── queue.h │ └── queue.c ├── ch19_prog_proj_04 │ ├── stackADT.h │ └── stackclient.c ├── ch19_prog_proj_03 │ ├── stackADT.h │ └── stackclient.c └── ch19_exercise_02 │ └── stack2.c ├── Ch07_Basic_Types ├── ch7_prog_proj_03.c ├── ch7_prog_proj_10.c ├── ch7_prog_proj_11.c ├── ch7_prog_proj_06.c ├── ch7_prog_proj_14.c ├── ch7_prog_proj_02.c ├── ch7_prog_proj_09.c ├── ch7_prog_proj_04.c ├── ch7_prog_proj_12.c ├── ch7_prog_proj_13.c ├── ch7_prog_proj_07.c └── ch7_prog_proj_05.c ├── Ch20_Low_Level_Programming └── ch20_prog_proj_01.c ├── Ch08_Arrays ├── ch8_prog_proj_04.c ├── ch8_prog_proj_12.c ├── ch8_prog_proj_02.c ├── ch8_prog_proj_01.c ├── ch8_prog_proj_03.c ├── ch8_prog_proj_13.c ├── ch8_prog_proj_07.c ├── ch8_prog_proj_15.c ├── ch8_prog_proj_05.c ├── ch8_prog_proj_14.c ├── ch8_prog_proj_06.c ├── ch8_prog_proj_11.c ├── ch8_prog_proj_16.c ├── ch8_prog_proj_17.c └── ch8_prog_proj_10.c ├── Ch12_Pointers_and_Arrays ├── ch12_prog_proj_03.c ├── ch12_prog_proj_07.c ├── ch12_prog_proj_04.c ├── ch12_prog_proj_01.c ├── ch12_prog_proj_06.c ├── ch12_prog_proj_05.c └── ch12_prog_proj_02.c ├── Ch09_Functions ├── ch9_prog_proj_06.c ├── ch9_prog_proj_07.c ├── ch9_prog_proj_02.c ├── ch9_prog_proj_01.c ├── ch9_prog_proj_04.c └── ch9_prog_proj_05.c ├── Ch05_Selection_Statements ├── ch5_prog_proj_01.c ├── ch5_prog_proj_04.c ├── ch5_prog_proj_05.c ├── ch5_prog_proj_10.c ├── ch5_prog_proj_07.c ├── ch5_prog_proj_06.c ├── ch5_prog_proj_09.c ├── ch5_prog_proj_02.c ├── ch5_prog_proj_03.c ├── ch5_prog_proj_11.c └── ch5_prog_proj_08.c ├── Ch21_The_Standard_Library └── ch21_prog_proj_01.c ├── Ch11_Pointers ├── ch11_prog_proj_03.c └── ch11_prog_proj_01.c └── Ch10_Program_Organization └── ch10_exercises.c /Ch22_Input_Output/ch22_prog_proj_07/a/foo.txt: -------------------------------------------------------------------------------- 1 | Foo bar!!! -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_07/b/foo.txt: -------------------------------------------------------------------------------- 1 | Foo bar!!! -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_07/a/foo.txt.rle: -------------------------------------------------------------------------------- 1 | Fo bar! -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_07/b/foo.txt.rle: -------------------------------------------------------------------------------- 1 | Fo bar! -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_04/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | int read_line(char str[], int n); 12 | 13 | #endif /* READLINE_H_ */ 14 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_03/quicksort.h: -------------------------------------------------------------------------------- 1 | /* 2 | * quicksort.h 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUICKSORT_H_ 9 | #define QUICKSORT_H_ 10 | 11 | 12 | void quicksort(int a[], int low, int high); 13 | 14 | int split(int a[], int low, int high); 15 | 16 | 17 | #endif /* QUICKSORT_H_ */ -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/newquote.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_04/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include "readline.h" 10 | 11 | int read_line(char str[], int n) 12 | { 13 | int ch, i = 0; 14 | 15 | while((ch = getchar()) != '\n') 16 | if(i < n) 17 | str[i++] = ch; 18 | str[i] = '\0'; 19 | 20 | return i; 21 | } 22 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/newquote_write_line_edit.txt: -------------------------------------------------------------------------------- 1 | C is quirky, flawed, and an enormous success. Although 2 | accidents of history surely helped, it evidently satisfied a 3 | need for a system implementation language efficient enough 4 | to displace assembly language, yet sufficiently abstract and 5 | fluent to describe algorithms and interactions in a wide 6 | variety of environments. -- Dennis M. Ritchie -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_05/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = x86_64-w64-mingw32-gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Source File 6 | SRC = sum.c 7 | # Object File 8 | OBJ = sum.o 9 | # Program File (./PROG argvs) 10 | PROG = sum 11 | 12 | all: $(PROG) 13 | 14 | $(PROG): $(OBJ) 15 | $(CC) $(OBJ) -o $@ 16 | 17 | $(OBJ): $(SRC) 18 | $(CC) $(CFLAGS) -c $(SRC) 19 | 20 | clean: 21 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch4_prog_proj_01.c 3 | * 4 | * Created on: Mar 12, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Reversing number's digits 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n; 15 | 16 | printf("Enter a two-digit number: "); 17 | scanf("%d", &n); 18 | 19 | printf("The reversal is: %d%d\n", n % 10, n / 10); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_04/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = x86_64-w64-mingw32-gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Source File 6 | SRC = reverse.c 7 | # Object File 8 | OBJ = reverse.o 9 | # Program File (./PROG argvs) 10 | PROG = reverse 11 | 12 | all: $(PROG) 13 | 14 | $(PROG): $(OBJ) 15 | $(CC) $(OBJ) -o $@ 16 | 17 | $(OBJ): $(SRC) 18 | $(CC) $(CFLAGS) -c $(SRC) 19 | 20 | clean: 21 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_06/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = x86_64-w64-mingw32-gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Source File 6 | SRC = planet.c 7 | # Object File 8 | OBJ = planet.o 9 | # Program File (./PROG argvs) 10 | PROG = planet 11 | 12 | all: $(PROG) 13 | 14 | $(PROG): $(OBJ) 15 | $(CC) $(OBJ) -o $@ 16 | 17 | $(OBJ): $(SRC) 18 | $(CC) $(CFLAGS) -c $(SRC) 19 | 20 | clean: 21 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_04/reverse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * reverse.c 3 | * 4 | * Created on: Dec 18, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Echo command-line arguments in reverse order 9 | 10 | #include 11 | #include 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | int i; 16 | for(i = argc - 1; i > 0; i--) 17 | { 18 | printf("%s ", argv[i]); 19 | } 20 | printf("\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_04.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 4: Amount with taxes 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float amount, taxes; 15 | 16 | printf("Enter an amount: "); 17 | scanf("%f", &amount); 18 | 19 | taxes = amount * 0.05f; 20 | 21 | printf("With tax added: $%.2f\n", amount + taxes); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch4_prog_proj_02.c 3 | * 4 | * Created on: Apr 1, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Reversing number's digits (Extended) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n; 15 | 16 | printf("Enter a three-digit number: "); 17 | scanf("%d", &n); 18 | 19 | printf("The reversal is: %d%d%d\n", n % 10, n / 10 % 10, n / 10 / 10); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_02.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Project 2: Volume of a sphere 10 | 11 | #include 12 | #define PI (3.14159f) 13 | 14 | int main(void) 15 | { 16 | float radius = 10.0f, volume; 17 | 18 | volume = (4.0f/3.0f) * PI * radius * radius * radius; 19 | 20 | printf("Volume of sphere = %.2f cubic meters\n", volume); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch4_prog_proj_03.c 3 | * 4 | * Created on: Apr 1, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Reversing number's digits (No Arithmetic) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int d1, d2, d3; 15 | 16 | printf("Enter a three-digit number: "); 17 | scanf("%1d%1d%1d", &d1, &d2, &d3); 18 | 19 | printf("The reversal is: %d%d%d\n", d3, d2, d1); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_05/sum.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sum.c 3 | * 4 | * Created on: Dec 18, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Adding up command-line arguments 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int sum = 0, i = 0; 17 | 18 | while(++i < argc) 19 | sum += atoi(argv[i]); 20 | 21 | printf("Total: %d\n", sum); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_05/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = main.o stack.o 7 | # Program File (./PROG argvs) 8 | PROG = rpn 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | main.o: main.c stack.h 16 | $(CC) $(CFLAGS) -c main.c 17 | 18 | stack.o: stack.c stack.h 19 | $(CC) $(CFLAGS) -c stack.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_01.c 3 | * 4 | * Created on: Mar 19, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Display date 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int day, month, year; 15 | 16 | printf("Enter a date (mm/dd/yyy): "); 17 | scanf("%d /%d /%d", &month, &day, &year); 18 | 19 | printf("You entered the date %04d%02d%02d\n", year, month, day); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_06.c 3 | * 4 | * Created on: Mar 12, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Even squares between 1 and n 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n, i; 15 | 16 | printf("Enter a number: "); 17 | scanf("%d", &n); 18 | 19 | printf("Even squares are:\n"); 20 | 21 | for(i = 2; i * i <= n; i += 2) 22 | { 23 | printf("%d\n", i * i); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_06.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 6: Calculate Polynomial (modified) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int x, polynomial; 15 | 16 | printf("Enter x: "); 17 | scanf("%d", &x); 18 | 19 | polynomial = (((((((((3 * x) + 2) * x) - 5) * x) - 1) * x) + 7) * x) - 6; 20 | 21 | printf("Polynomial = %d\n", polynomial); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_05.c 3 | * 4 | * Created on: Mar 12, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Reversing number's digits 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n; 15 | 16 | printf("Enter a number: "); 17 | scanf("%d", &n); 18 | 19 | printf("The reversal is: "); 20 | 21 | do 22 | { 23 | printf("%d", n % 10); 24 | n /= 10; 25 | } while(n > 0); 26 | 27 | printf("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_03/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = qsort.o quicksort.o 7 | # Program File (./PROG argvs) 8 | PROG = qsort 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | qsort.o: qsort.c quicksort.h 16 | $(CC) $(CFLAGS) -c qsort.c 17 | 18 | quicksort.o: quicksort.c quicksort.h 19 | $(CC) $(CFLAGS) -c quicksort.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_04/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = remind.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = remind 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | remind.o: remind.c readline.h 16 | $(CC) $(CFLAGS) -c remind.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_08/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Aug 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #include "readline.h" 12 | 13 | int read_line(char str[], int n) 14 | { 15 | int ch, i = 0; 16 | 17 | while (isspace(ch = getchar())); 18 | 19 | while (ch != '\n' && ch != EOF) 20 | { 21 | if (i < n) 22 | str[i++] = ch; 23 | 24 | ch = getchar(); 25 | } 26 | 27 | str[i] = '\0'; 28 | return i; 29 | } 30 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_02/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Apr 7, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while(isspace(ch = getchar())); 17 | 18 | while(ch != '\n' && ch != EOF) 19 | { 20 | if(i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_03/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while(isspace(ch = getchar())); 17 | 18 | while(ch != '\n' && ch != EOF) 19 | { 20 | if(i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_01/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_02/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_03/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_04/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while(isspace(ch = getchar())); 17 | 18 | while(ch != '\n' && ch != EOF) 19 | { 20 | if(i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_02/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_03/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_04/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 5 | # Object File 6 | OBJ = inventory.o readline.o 7 | # Program File (./PROG argvs) 8 | PROG = inventory 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | inventory.o: inventory.c readline.h 16 | $(CC) $(CFLAGS) -c inventory.c 17 | 18 | readline.o: readline.c readline.h 19 | $(CC) $(CFLAGS) -c readline.c 20 | 21 | clean: 22 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_03.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 3: Volume of a sphere (modified) 9 | 10 | #include 11 | #define PI (3.14159f) 12 | 13 | int main(void) 14 | { 15 | float radius, volume; 16 | 17 | printf("Enter the radius: "); 18 | scanf("%f", &radius); 19 | 20 | volume = (4.0f/3.0f) * PI * radius * radius * radius; 21 | 22 | printf("Volume of sphere = %.2f cubic meters\n", volume); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_01/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Apr 7, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while (isspace(ch = getchar())); 17 | 18 | while (ch != '\n' && ch != EOF) 19 | { 20 | if (i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_02/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while (isspace(ch = getchar())); 17 | 18 | while (ch != '\n' && ch != EOF) 19 | { 20 | if (i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_03/readline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.c 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "readline.h" 11 | 12 | int read_line(char str[], int n) 13 | { 14 | int ch, i = 0; 15 | 16 | while (isspace(ch = getchar())); 17 | 18 | while (ch != '\n' && ch != EOF) 19 | { 20 | if (i < n) 21 | str[i++] = ch; 22 | 23 | ch = getchar(); 24 | } 25 | str[i] = '\0'; 26 | 27 | return i; 28 | } 29 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_04/a/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACK_H_ 9 | #define STACK_H_ 10 | 11 | #include 12 | 13 | #define STACK_SIZE 100 14 | 15 | typedef struct 16 | { 17 | int contents[STACK_SIZE]; 18 | int top; 19 | } Stack; 20 | 21 | void make_empty(Stack *s); 22 | 23 | bool is_empty(const Stack *s); 24 | 25 | bool is_full(const Stack *s); 26 | 27 | void push(Stack *s, int i); 28 | 29 | int pop(Stack *s); 30 | 31 | #endif /* STACK_H_ */ 32 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = justify.o word.o line.o 7 | # Program File (./PROG argvs) 8 | PROG = justify 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | justify.o: justify.c word.h line.h 16 | $(CC) $(CFLAGS) -c justify.c 17 | 18 | word.o: word.c word.h 19 | $(CC) $(CFLAGS) -c word.c 20 | 21 | line.o: line.c line.h 22 | $(CC) $(CFLAGS) -c line.c 23 | 24 | clean: 25 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = justify.o word.o line.o 7 | # Program File (./PROG argvs) 8 | PROG = justify 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | justify.o: justify.c word.h line.h 16 | $(CC) $(CFLAGS) -c justify.c 17 | 18 | word.o: word.c word.h 19 | $(CC) $(CFLAGS) -c word.c 20 | 21 | line.o: line.c line.h 22 | $(CC) $(CFLAGS) -c line.c 23 | 24 | clean: 25 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_01/stackADT.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT.h 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACKADT_H_ 9 | #define STACKADT_H_ 10 | 11 | #include // C99 12 | 13 | typedef char Item; 14 | typedef struct stack_type *Stack; 15 | 16 | Stack create(void); 17 | 18 | void destroy(Stack s); 19 | 20 | void make_empty(Stack s); 21 | 22 | bool is_empty(Stack s); 23 | 24 | bool is_full(Stack s); 25 | 26 | void push(Stack s, Item i); 27 | 28 | Item pop(Stack s); 29 | 30 | #endif /* STACKADT_H_ */ 31 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_02/stackADT.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT.h 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACKADT_H_ 9 | #define STACKADT_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct stack_type *Stack; 15 | 16 | Stack create(void); 17 | 18 | void destroy(Stack s); 19 | 20 | void make_empty(Stack s); 21 | 22 | bool is_empty(Stack s); 23 | 24 | bool is_full(Stack s); 25 | 26 | void push(Stack s, Item i); 27 | 28 | Item pop(Stack s); 29 | 30 | #endif /* STACKADT_H_ */ 31 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/Makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | # Compiler Flags 4 | CFLAGS = -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 5 | # Object File 6 | OBJ = justify.o word.o line.o 7 | # Program File (./PROG argvs) 8 | PROG = justify 9 | 10 | all: $(PROG) 11 | 12 | $(PROG): $(OBJ) 13 | $(CC) $(OBJ) -o $@ 14 | 15 | justify.o: justify.c word.h line.h 16 | $(CC) $(CFLAGS) -c justify.c 17 | 18 | word.o: word.c word.h 19 | $(CC) $(CFLAGS) -c word.c 20 | 21 | line.o: line.c line.h 22 | $(CC) $(CFLAGS) -c line.c 23 | 24 | clean: 25 | rm -rf $(OBJ) $(PROG) -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_06/b/stackADT2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT2.h 3 | * 4 | * Created on: May 10, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACKADT2_H_ 9 | #define STACKADT2_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct stack_type *Stack; 15 | 16 | Stack create(int size); 17 | void destroy(Stack s); 18 | void make_empty(Stack s); 19 | bool is_empty(Stack s); 20 | bool is_full(Stack s); 21 | 22 | void push(Stack s, Item i); 23 | Item pop(Stack s); 24 | Item peek(Stack s); 25 | 26 | #endif /* STACKADT2_H_ */ 27 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_07/stackADT2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT2.h 3 | * 4 | * Created on: May 10, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACKADT2_H_ 9 | #define STACKADT2_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct stack_type *Stack; 15 | 16 | Stack create(int size); 17 | 18 | void destroy(Stack s); 19 | 20 | void make_empty(Stack s); 21 | 22 | bool is_empty(Stack s); 23 | 24 | bool is_full(Stack s); 25 | 26 | void push(Stack s, Item i); 27 | 28 | Item pop(Stack s); 29 | 30 | #endif /* STACKADT2_H_ */ 31 | -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_04.c 3 | * 4 | * Created on: Mar 19, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Display telephone number 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int area_code, central_office_code, line_number; 15 | 16 | printf("Enter phone number [(xxx) xxx-xxxx]: "); 17 | scanf("(%d) %d-%d", &area_code, ¢ral_office_code, &line_number); 18 | 19 | printf("You entered %03d.%03d.%04d\n", area_code, central_office_code, 20 | line_number); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_03.c 3 | * 4 | * Created on: Mar 14, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Summing a Series of Numbers 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | double n, sum = 0; 15 | 16 | printf("This program sums a series of double values.\n"); 17 | printf("Enter double values (0 to terminate): "); 18 | scanf("%lf", &n); 19 | 20 | while(n != 0.0) 21 | { 22 | sum += n; 23 | scanf("%lf", &n); 24 | } 25 | 26 | printf("The sum is: %f\n", sum); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_05/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.h 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACK_H_ 9 | #define STACK_H_ 10 | 11 | #include 12 | 13 | #define STACK_SIZE 100 14 | 15 | extern int contents[STACK_SIZE]; 16 | extern int top; 17 | 18 | 19 | void make_empty(void); 20 | 21 | bool is_empty(void); 22 | 23 | bool is_full(void); 24 | 25 | void push(int i); 26 | 27 | int pop(void); 28 | 29 | void stack_overflow(void); 30 | 31 | void stack_underflow(void); 32 | 33 | 34 | #endif /* STACK_H_ */ 35 | -------------------------------------------------------------------------------- /Ch20_Low_Level_Programming/ch20_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch20_prog_proj_01.c 3 | * 4 | * Created on: May 18, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Float representation 9 | 10 | #include 11 | 12 | union 13 | { 14 | float value; 15 | struct 16 | { 17 | unsigned int fraction: 23, exponent: 8, sign: 1; 18 | } IEEE_STD; 19 | } Float; 20 | 21 | int main(void) 22 | { 23 | Float.IEEE_STD.sign = 1; 24 | Float.IEEE_STD.exponent = 128; 25 | Float.IEEE_STD.fraction = 0; 26 | 27 | printf("Float value = %.1f\n", Float.value); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/quote.txt: -------------------------------------------------------------------------------- 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 | -- Dennis M. Ritchie -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/quote.txt: -------------------------------------------------------------------------------- 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 | -- Dennis M. Ritchie -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_05.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 5: Calculate Polynomial 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int x, xpow2, xpow3, xpow4, xpow5, polynomial; 15 | 16 | printf("Enter x: "); 17 | scanf("%d", &x); 18 | 19 | xpow2 = x * x; 20 | xpow3 = xpow2 * x; 21 | xpow4 = xpow3 * x; 22 | xpow5 = xpow4 * x; 23 | 24 | polynomial = (3 * xpow5) + (2 * xpow4) - (5 * xpow3) 25 | - xpow2 + (7 * x) - 6; 26 | 27 | printf("Polynomial = %d\n", polynomial); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_03/qsort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * qsort.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Quicksort algorithm 9 | 10 | #include 11 | #include "quicksort.h" 12 | #define N 10 13 | 14 | int main(void) 15 | { 16 | int a[N], i; 17 | 18 | printf("Enter %d numbers to be stored: ", N); 19 | 20 | for(i = 0; i < N; i++) 21 | scanf("%d", &a[i]); 22 | 23 | quicksort(a, 0, N - 1); 24 | 25 | printf("In sorted order: "); 26 | for(i = 0; i < N; i++) 27 | printf("%d ", a[i]); 28 | 29 | printf("\n"); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/word.c: -------------------------------------------------------------------------------- 1 | /* 2 | * word.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include "word.h" 10 | 11 | int read_char(void) 12 | { 13 | int ch = getchar(); 14 | 15 | if(ch == '\n' || ch == '\t') 16 | return ' '; 17 | 18 | return ch; 19 | } 20 | 21 | 22 | void read_word(char *word, int len) 23 | { 24 | int ch, pos = 0; 25 | 26 | while((ch = read_char()) == ' '); 27 | 28 | while(ch != ' ' && ch != EOF) 29 | { 30 | if(pos < len) 31 | word[pos++] = ch; 32 | 33 | ch = read_char(); 34 | } 35 | word[pos] = '\0'; 36 | } 37 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_01.c 3 | * 4 | * Created on: Mar 12, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Largest non-negative number entered 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float number, largest_number = 0.0f; 15 | 16 | for(;;) 17 | { 18 | printf("Enter a number: "); 19 | scanf("%f", &number); 20 | 21 | if(number <= 0) 22 | break; 23 | 24 | if(number > largest_number) 25 | largest_number = number; 26 | } 27 | 28 | printf("\nThe largest number entered was %g\n", largest_number); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_04.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Reversing a Series of Numbers (modified) 9 | 10 | #include 11 | 12 | #define N 10 13 | #define SIZE (int) (sizeof(a) / sizeof(a[0])) 14 | 15 | int main(void) 16 | { 17 | int a[N], i; 18 | 19 | printf("Enter %d numbers: ", N); 20 | for (i = 0; i < SIZE; i++) 21 | scanf("%d", &a[i]); 22 | 23 | printf("In reverse order: "); 24 | for (i = SIZE - 1; i >= 0; i--) 25 | printf(" %d", a[i]); 26 | 27 | printf("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_10.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 10: Number of Vowels 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | char input_ch; 16 | int num_vowels = 0; 17 | 18 | printf("Enter a sentence: "); 19 | 20 | while((input_ch = getchar()) != '\n') 21 | { 22 | switch(toupper(input_ch)) 23 | { 24 | case 'A': case 'E': case 'I': case 'O': case 'U': 25 | num_vowels++; break; 26 | } 27 | } 28 | 29 | printf("Your sentence contains %d vowels\n", num_vowels); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_01.c 3 | * 4 | * Created on: Apr 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 1: Picture on the screen 9 | 10 | /* 11 | * x [] []|[] [] [] [] [] [] [] x 12 | * x []|[] [] [] [] [] [] x 13 | * x |[] [] [] [] [] x 14 | * |x [] [] [] x 15 | * | x [] x 16 | * | x 17 | * 18 | */ 19 | 20 | #include 21 | 22 | int main(void) 23 | { 24 | printf(" *\n"); 25 | printf(" *\n"); 26 | printf(" *\n"); 27 | printf("* *\n"); 28 | printf(" * *\n"); 29 | printf(" *\n"); 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_18.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 18: Date Format 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int dd, mm, yyyy; 15 | char *months[] = 16 | { 17 | "January", "February", "March", "April", "May", "June", 18 | "July", "August", "September", "October", "November", "December" 19 | }; 20 | 21 | printf("Enter a data (mm/dd/yyyy): "); 22 | scanf("%d /%d /%d", &mm, &dd, &yyyy); 23 | 24 | printf("You entered the data %s %d, %d\n", months[mm - 1], dd, yyyy); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/word.c: -------------------------------------------------------------------------------- 1 | /* 2 | * word.c 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include "word.h" 10 | 11 | int read_char(void) 12 | { 13 | int ch = getchar(); 14 | 15 | if (ch == '\n' || ch == '\t') 16 | return ' '; 17 | 18 | return ch; 19 | } 20 | 21 | 22 | void read_word(char *word, int len) 23 | { 24 | int ch, pos = 0; 25 | 26 | while ((ch = read_char()) == ' '); 27 | 28 | while (ch != ' ' && ch != EOF) 29 | { 30 | if (pos < len) 31 | word[pos++] = ch; 32 | 33 | ch = read_char(); 34 | } 35 | word[pos] = '\0'; 36 | } 37 | -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch4_prog_proj_04.c 3 | * 4 | * Created on: Apr 1, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Decimal to Octal Conversion 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n; 15 | int d1, d2, d3, d4, d5; // Octal number: d1d2d3d4d5 16 | 17 | printf("Enter a number between 0 and 32767: "); 18 | scanf("%d", &n); 19 | 20 | d5 = n % 8; 21 | n /= 8; 22 | 23 | d4 = n % 8; 24 | n /= 8; 25 | 26 | d3 = n % 8; 27 | n /= 8; 28 | 29 | d2 = n % 8; 30 | n /= 8; 31 | 32 | d1 = n; 33 | 34 | printf("In octal, your number is: %d%d%d%d%d\n", d1, d2, d3, d4, d5); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_03.c 3 | * 4 | * Created on: Nov 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Fraction's lowest terms (GCD) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int numerator, denominator, m, n, remainder; 15 | 16 | printf("Enter a fracation: "); 17 | scanf("%d /%d", &numerator, &denominator); 18 | 19 | m = numerator; 20 | n = denominator; 21 | 22 | while(n != 0) 23 | { 24 | remainder = m % n; 25 | m = n; 26 | n = remainder; 27 | } 28 | 29 | printf("In lowest terms: %d/%d\n", numerator / m, denominator / m); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_03.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Message Reversal (Same as 1.b) 9 | 10 | #include 11 | #define LEN 100 12 | 13 | int main(void) 14 | { 15 | char input, message[LEN], *msg_ptr = message; 16 | 17 | // Reading the message 18 | printf("Enter a message: "); 19 | while ((input = getchar()) != '\n' && msg_ptr < message + LEN) 20 | *msg_ptr++ = input; 21 | 22 | // Reversing the message 23 | printf("Reversal is: "); 24 | while (msg_ptr > message) 25 | putchar(*--msg_ptr); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_12.c 3 | * 4 | * Created on: Apr 25, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 12: Scrabble (modified) 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | char input_ch; 16 | int score = 0; 17 | int face_values[26] = 18 | { 19 | 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 20 | 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 21 | }; 22 | 23 | printf("Enter a word: "); 24 | 25 | while ((input_ch = toupper(getchar())) != '\n') 26 | score += face_values[input_ch - 'A']; 27 | 28 | printf("Scrabble value: %d\n", score); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/word.h: -------------------------------------------------------------------------------- 1 | /* 2 | * word.h 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef WORD_H_ 9 | #define WORD_H_ 10 | 11 | /********************************************************************************* 12 | * read_word: Reads the next word from the input and stores it in word. * 13 | * Makes word empty if no word could be read because of end-of-file. * 14 | * Truncates the word if its length exceeds len. * 15 | *********************************************************************************/ 16 | void read_word(char *word, int len); 17 | 18 | 19 | #endif /* WORD_H_ */ 20 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/word.c: -------------------------------------------------------------------------------- 1 | /* 2 | * word.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include "word.h" 10 | 11 | int read_char(void) 12 | { 13 | int ch = getchar(); 14 | 15 | if(ch == '\n' || ch == '\t') 16 | return ' '; 17 | 18 | return ch; 19 | } 20 | 21 | 22 | void read_word(char *word, int len) 23 | { 24 | int ch, pos = 0; 25 | 26 | while((ch = read_char()) == ' '); 27 | 28 | while(ch != ' ' && ch != EOF) 29 | { 30 | if(pos < len) 31 | word[pos++] = ch; 32 | 33 | ch = read_char(); 34 | } 35 | word[pos] = '\0'; 36 | 37 | if(pos > MAX_WORD_LEN) 38 | word[MAX_WORD_LEN] = '*'; 39 | } 40 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/word.h: -------------------------------------------------------------------------------- 1 | /* 2 | * word.h 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef WORD_H_ 9 | #define WORD_H_ 10 | 11 | /********************************************************************************* 12 | * read_word: Reads the next word from the input and stores it in word. * 13 | * Makes word empty if no word could be read because of end-of-file. * 14 | * Truncates the word if its length exceeds len. * 15 | *********************************************************************************/ 16 | void read_word(char *word, int len); 17 | 18 | 19 | #endif /* WORD_H_ */ 20 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_01/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUE_H_ 9 | #define QUEUE_H_ 10 | 11 | #include // C99 12 | 13 | // Inserting item at the end of the queue 14 | void enqueue(int i); 15 | 16 | // Removing an item from the beginning of the queue 17 | int dequeue(void); 18 | 19 | // Returning the first item in the queue (without changing the queue) 20 | int front_item(void); 21 | 22 | // Returning the last item in the queue (without changing the queue) 23 | int rear_item(void); 24 | 25 | // Testing whether the queue is empty 26 | bool is_empty(void); 27 | 28 | #endif /* QUEUE_H_ */ 29 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_03/a/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUE_H_ 9 | #define QUEUE_H_ 10 | 11 | #include // C99 12 | 13 | // Inserting item at the end of the queue 14 | void enqueue(int i); 15 | 16 | // Removing an item from the beginning of the queue 17 | int dequeue(void); 18 | 19 | // Returning the first item in the queue (without changing the queue) 20 | int front_item(void); 21 | 22 | // Returning the last item in the queue (without changing the queue) 23 | int rear_item(void); 24 | 25 | // Testing whether the queue is empty 26 | bool is_empty(void); 27 | 28 | #endif /* QUEUE_H_ */ 29 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_03/b/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUE_H_ 9 | #define QUEUE_H_ 10 | 11 | #include // C99 12 | 13 | // Inserting item at the end of the queue 14 | void enqueue(int i); 15 | 16 | // Removing an item from the beginning of the queue 17 | int dequeue(void); 18 | 19 | // Returning the first item in the queue (without changing the queue) 20 | int front_item(void); 21 | 22 | // Returning the last item in the queue (without changing the queue) 23 | int rear_item(void); 24 | 25 | // Testing whether the queue is empty 26 | bool is_empty(void); 27 | 28 | #endif /* QUEUE_H_ */ 29 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_08.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 8: One-month calendar 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n, i, day; 15 | 16 | printf("Enter number of days in month: "); 17 | scanf("%d", &n); 18 | 19 | printf("Enter starting day of the week (1=Sun, 7=Sat): "); 20 | scanf("%d", &day); 21 | 22 | for(i = 1; i < day; i++) 23 | { 24 | printf(" "); 25 | } 26 | 27 | for(i = 1; i <= n; i++, day++) 28 | { 29 | printf("%2d ", i); 30 | 31 | if(day == 7) 32 | { 33 | day = 0; 34 | printf("\n"); 35 | } 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_08/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Aug 6, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | #ifndef READLINE_H_ 10 | #define READLINE_H_ 11 | 12 | /********************************************************** 13 | * read_line: Skips leading white-space characters, then * 14 | * reads the remainder of the input line and * 15 | * stores it in str. Truncates the line if its * 16 | * length exceeds n. Returns the number of * 17 | * characters stored. * 18 | **********************************************************/ 19 | int read_line(char str[], int n); 20 | 21 | #endif /* READLINE_H_ */ 22 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_11.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 11: First name and Last name 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | char last_ch, first_ch; 15 | 16 | printf("Enter a first and last name: "); 17 | 18 | while((first_ch = getchar()) == ' '); // Skipping initial spaces 19 | 20 | while(getchar() != ' '); // Skipping first name letters 21 | 22 | while((last_ch = getchar()) == ' '); // Skipping intermediate spaces 23 | 24 | do 25 | { 26 | printf("%c", last_ch); 27 | } while((last_ch = getchar()) != '\n'); 28 | 29 | printf(", %c.\n", first_ch); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_06.c 3 | * 4 | * Created on: Mar 14, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: sizeof different types 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | printf("Size of int is %lu\n", (unsigned long) sizeof(int)); 15 | printf("Size of short is %lu\n", (unsigned long) sizeof(short)); 16 | printf("Size of long is %lu\n", (unsigned long) sizeof(long)); 17 | printf("Size of float is %lu\n", (unsigned long) sizeof(float)); 18 | printf("Size of double is %lu\n", (unsigned long) sizeof(double)); 19 | printf("Size of long double is %lu\n", (unsigned long) sizeof(long double)); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/word.h: -------------------------------------------------------------------------------- 1 | /* 2 | * word.h 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef WORD_H_ 9 | #define WORD_H_ 10 | 11 | #define MAX_WORD_LEN 20 12 | 13 | /********************************************************************************* 14 | * read_word: Reads the next word from the input and stores it in word. * 15 | * Makes word empty if no word could be read because of end-of-file. * 16 | * Truncates the word if its length exceeds len. * 17 | *********************************************************************************/ 18 | void read_word(char *word, int len); 19 | 20 | 21 | #endif /* WORD_H_ */ 22 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_14.c 3 | * 4 | * Created on: Apr 21, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 14: Square root using Newton's method 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | double x, y, avg = 1.0; 16 | 17 | printf("Enter a positive number: "); 18 | scanf("%lf", &x); 19 | 20 | do 21 | { 22 | // new y (avg) becomes old y (y) 23 | y = avg; 24 | 25 | // compute the new y 26 | avg = (y + x / y) / 2; 27 | 28 | } while (fabs(avg - y) > 0.00001 * y); 29 | 30 | // get the latest y value 31 | y = avg; 32 | 33 | printf("Square root: %g\n", y); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_02.c 3 | * 4 | * Created on: Mar 14, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Printing a Table of Squares 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int i, n; 15 | 16 | printf("This program prints a table of squares.\n"); 17 | printf("Enter number of entries in table: "); 18 | scanf("%d", &n); 19 | 20 | getchar(); // Read the extra \n left by scanf 21 | 22 | for(i = 1; i <= n; i++) 23 | { 24 | printf("%10d%10d\n", i, i * i); 25 | 26 | if(i % 24 == 0) 27 | { 28 | printf("Press Enter to coninue..."); 29 | 30 | while(getchar() != '\n'); // Wait till enter is pressed 31 | } 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_03.c 3 | * 4 | * Created on: Mar 19, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: ISBN breakdown 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int GSI_prefix, group_id, publisher_code, item_no, check_digit; 15 | 16 | printf("Enter ISBN: "); 17 | scanf("%d-%d-%d-%d-%d", &GSI_prefix, &group_id, 18 | &publisher_code, &item_no, &check_digit); 19 | 20 | printf("GSI prefix: %d\n", GSI_prefix); 21 | 22 | printf("Group identifier: %d\n", group_id); 23 | 24 | printf("Publisher code: %d\n", publisher_code); 25 | 26 | printf("Item number: %d\n", item_no); 27 | 28 | printf("Check digit: %d\n", check_digit); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/justify.c: -------------------------------------------------------------------------------- 1 | /* 2 | * justify.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Justify 9 | 10 | #include 11 | #include "line.h" 12 | #include "word.h" 13 | 14 | int main(void) 15 | { 16 | char word[MAX_WORD_LEN + 2]; 17 | int word_len; 18 | 19 | clear_line(); 20 | 21 | for(;;) 22 | { 23 | read_word(word, MAX_WORD_LEN + 1); 24 | word_len = strlen(word); 25 | 26 | if(word_len == 0) 27 | { 28 | flush_line(); 29 | return 0; // Terminate program 30 | } 31 | 32 | if(word_len + 1 > space_remaining()) 33 | { 34 | write_line(); 35 | clear_line(); 36 | } 37 | add_word(word); 38 | } 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_02.c 3 | * 4 | * Created on: Mar 12, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Greatest common divison (GCD) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int m, n, remainder; 15 | 16 | printf("Enter two integers: "); 17 | scanf("%d %d", &m, &n); 18 | 19 | // As long as n != 0, m != gcd 20 | while(n != 0) 21 | { 22 | // GCD - Euclid's algorithm: 23 | // 1) Compute the remainder of m / n 24 | remainder = m % n; 25 | 26 | // 2) Copy n into m 27 | m = n; 28 | 29 | // 3) Copy remainder into n 30 | n = remainder; 31 | 32 | // 4) Repeat the process... 33 | } 34 | 35 | printf("Greatest common divisor: %d\n", m); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch4_prog_proj_06.c 3 | * 4 | * Created on: Apr 2, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Computing the EAN check digit 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, 15 | first_sum, second_sum, total; 16 | 17 | printf("Enter the first 12 digits of an EAN: "); 18 | scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&d1, &d2, 19 | &d3, &d4, &d5, &d6, &d7, &d8, &d9, &d10, &d11, &d12); 20 | 21 | first_sum = d2 + d4 + d6 + d8 + d10 + d12; 22 | second_sum = d1 + d3 + d5 + d7 + d9 + d11; 23 | total = 3 * first_sum + second_sum; 24 | 25 | printf("Check digit: %d\n", 9 - ((total - 1) % 10)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_02.c 3 | * 4 | * Created on: Mar 19, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Formatting product information 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | 15 | int item_no, month, day, year; 16 | float unit_price; 17 | 18 | printf("Enter item number: "); 19 | scanf("%d", &item_no); 20 | 21 | printf("Enter unit price: "); 22 | scanf("%f", &unit_price); 23 | 24 | printf("Enter purchase date (mm/dd/yyy): "); 25 | scanf("%d /%d /%d", &month, &day, &year); 26 | 27 | 28 | printf("Item\t\tUnit\t\tPurchase\n\t\tPrice\t\tDate\n"); 29 | printf("%-d\t\t$%7.2f\t%d/%d/%d\n", item_no, unit_price, 30 | month, day, year); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_06.c 3 | * 4 | * Created on: Oct 25, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Polynomial 9 | 10 | #include 11 | 12 | double compute_polynomial(double x); 13 | 14 | int main(void) 15 | { 16 | // Independent variable 17 | double x; 18 | 19 | // Reading x 20 | printf("Enter the value of x: "); 21 | scanf("%lf", &x); 22 | 23 | // Compute polynomial 24 | printf("The value of the polynomial = %lf\n", compute_polynomial(x)); 25 | 26 | return 0; 27 | } 28 | 29 | double compute_polynomial(double x) 30 | { 31 | double x2 = x * x; 32 | double x3 = x2 * x; 33 | double x4 = x3 * x; 34 | double x5 = x4 * x; 35 | 36 | return (3 * x5 + 2 * x4 - 5 * x3 - x2 + 7 * x - 6); 37 | } 38 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_01/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Apr 7, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_02/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Apr 7, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_03/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_06.c 3 | * 4 | * Created on: Mar 15, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Adding Fractions 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int num1, denom1, num2, denom2, result_num, result_denom; 15 | 16 | printf("Enter two fractions separated by a plus sign: "); 17 | scanf("%d /%d +%d /%d", &num1, &denom1, &num2, &denom2); 18 | 19 | // Note: "%d /%d +%d /%d" is better than "%d/%d+%d/%d" 20 | // as we are allowed to insert spaces before and after 21 | // each character like in exercise 6 22 | 23 | result_num = num1 * denom2 + num2 * denom1; 24 | result_denom = denom1 * denom2; 25 | 26 | printf("The sum is %d/%d\n", result_num, result_denom); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_04a.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_04a.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 4: Text File Statistics 10 | // (a) Number of Characters 11 | 12 | #include 13 | #include 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | FILE *fp; 18 | unsigned int count; 19 | 20 | if (argc != 2) 21 | { 22 | fprintf(stderr, "Usage: ./program filename.txt\n"); 23 | exit(EXIT_FAILURE); 24 | } 25 | 26 | if ((fp = fopen(argv[1], "r")) == NULL) 27 | { 28 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 29 | exit(EXIT_FAILURE); 30 | } 31 | 32 | for (count = 0; getc(fp) != EOF; count++); 33 | 34 | fclose(fp); 35 | fprintf(stdout, "Number of characters = %u\n", count); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_09.c 3 | * 4 | * Created on: Apr 21, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 9: Time Conversion (12 -> 24) 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | int hrs, mins; 16 | char meridiem; 17 | 18 | printf("Enter a 12-hour time: "); 19 | scanf("%d :%d %c", &hrs, &mins, &meridiem); 20 | 21 | // Convert from 12-hour time to 24-hour time: 22 | if (toupper(meridiem) == 'P' && hrs != 12) // 1 PM -> 13, 12 PM -> 12 23 | { 24 | hrs += 12; 25 | } 26 | else if (toupper(meridiem) == 'A' && hrs == 12) // 1 AM -> 1, 12 AM -> 0 27 | { 28 | hrs = 0; 29 | } 30 | 31 | printf("Equivalent 24-hour time: %.2d:%.2d\n", hrs, mins); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_02/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_03/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_04/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * readline.h 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef READLINE_H_ 9 | #define READLINE_H_ 10 | 11 | /************************************************************ 12 | * read_line: Skips leading white-space characters, then * 13 | * reads the remainder of the input line and * 14 | * stores it in str. Truncates the line if its * 15 | * length exceeds n. Returns the number of * 16 | * characters stored * 17 | * * 18 | ************************************************************/ 19 | 20 | int read_line(char str[], int n); 21 | 22 | #endif /* READLINE_H_ */ 23 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_02.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Repeated Digits (modified 2) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int digit_seen_count[10] = {0}; 15 | int digit; 16 | long n; 17 | 18 | printf("Enter a number: "); 19 | scanf("%ld", &n); 20 | 21 | while (n > 0) 22 | { 23 | digit = n % 10; 24 | 25 | digit_seen_count[digit]++; 26 | 27 | n /= 10; 28 | } 29 | 30 | printf("Digit: "); 31 | for (digit = 0; digit < 10; digit++) 32 | printf("%3d", digit); 33 | 34 | printf("\nOccurrences:"); 35 | for (digit = 0; digit < 10; digit++) 36 | printf("%3d", digit_seen_count[digit]); 37 | 38 | printf("\n"); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_11.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 11: Calculating mathematical constant e 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n, i, factorial_i = 1; 15 | float e = 1.0f; 16 | 17 | printf("This program calculates an approximate value of\n"); 18 | printf("the mathematical constant e, using this formula:\n"); 19 | printf("1 + 1/1! + 1/2! + 1/3! + ... + 1/n!\n"); 20 | printf("where n is an integer\n\n"); 21 | 22 | printf("Enter n: "); 23 | scanf("%d", &n); 24 | 25 | for(i = 1; i <= n; i++) 26 | { 27 | // Calculate factorial i to get 1/i! 28 | factorial_i *= i; 29 | 30 | e += 1.0f / factorial_i; 31 | } 32 | 33 | printf("e is approximately equal to %f\n", e); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_07.c 3 | * 4 | * Created on: Nov 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 7: Smallest number of bills 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int usd_amount, remainder, bills; 15 | 16 | printf("Enter a dollar amount: "); 17 | scanf("%d", &usd_amount); 18 | 19 | bills = usd_amount / 20; 20 | printf("$20 bills: %d\n", bills); 21 | remainder = usd_amount - bills * 20; 22 | 23 | usd_amount = remainder; 24 | bills = usd_amount / 10; 25 | printf("$10 bills: %d\n", bills); 26 | remainder = usd_amount - bills * 10; 27 | 28 | usd_amount = remainder; 29 | bills = usd_amount / 5; 30 | printf(" $5 bills: %d\n", bills); 31 | remainder = usd_amount - bills * 5; 32 | 33 | printf(" $1 bills: %d\n", remainder); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/justify.c: -------------------------------------------------------------------------------- 1 | /* 2 | * justify.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Justify 9 | 10 | #include 11 | #include "line.h" 12 | #include "word.h" 13 | #define MAX_WORD_LEN 20 14 | 15 | int main(void) 16 | { 17 | char word[MAX_WORD_LEN + 2]; 18 | int word_len; 19 | 20 | clear_line(); 21 | 22 | for(;;) 23 | { 24 | read_word(word, MAX_WORD_LEN + 1); 25 | word_len = strlen(word); 26 | 27 | if(word_len == 0) 28 | { 29 | flush_line(); 30 | return 0; // Terminate program 31 | } 32 | 33 | if(word_len > MAX_WORD_LEN) 34 | word[MAX_WORD_LEN] = '*'; 35 | 36 | if(word_len + 1 > space_remaining()) 37 | { 38 | write_line(); 39 | clear_line(); 40 | } 41 | add_word(word); 42 | } 43 | 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_01.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 1: Number of digits 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int number, digits_count; 16 | 17 | printf("Enter a number: "); 18 | scanf("%d", &number); 19 | 20 | if (number >= 0 && number <= 9) 21 | { 22 | digits_count = 1; 23 | } 24 | else if (number >= 10 && number <= 99) 25 | { 26 | digits_count = 2; 27 | } 28 | else if (number >= 100 && number <= 999) 29 | { 30 | digits_count = 3; 31 | } 32 | 33 | printf("The number %d has %d ", number, digits_count); 34 | 35 | if(digits_count > 1) 36 | { 37 | printf("digits\n"); 38 | } 39 | else 40 | { 41 | printf("digit\n"); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_07.c 3 | * 4 | * Created on: Oct 25, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 7: Power Function 10 | 11 | #include 12 | 13 | int power(int x, int n); 14 | 15 | int main(void) 16 | { 17 | int x, n; 18 | 19 | // Reading x 20 | printf("Enter x: "); 21 | scanf("%d", &x); 22 | 23 | // Reading n 24 | printf("Enter the power n: "); 25 | scanf("%d", &n); 26 | 27 | // Printing output 28 | printf("%d to the power %d (or %d^%d) = %d", x, n, x, n, power(x, n)); 29 | 30 | return 0; 31 | } 32 | 33 | int power(int x, int n) 34 | { 35 | int p; 36 | 37 | if (n == 0) 38 | return 1; 39 | else if (n % 2) // n is odd 40 | p = power(x * x, (n - 1) / 2) * x; 41 | else // n is even 42 | p = power(x * x, n / 2); 43 | return p; 44 | } 45 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_05/queueADT.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queueADT.h 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUEADT_H_ 9 | #define QUEUEADT_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct queue_type *Queue; 15 | 16 | Queue create(void); 17 | 18 | void destroy(Queue q); 19 | 20 | void make_empty(Queue q); 21 | 22 | // Inserting item at the end of the queue 23 | void enqueue(Queue q, Item i); 24 | 25 | // Removing an item from the beginning of the queue 26 | Item dequeue(Queue q); 27 | 28 | // Returning the first item in the queue (without changing the queue) 29 | Item front(Queue q); 30 | 31 | // Returning the last item in the queue (without changing the queue) 32 | Item rear(Queue q); 33 | 34 | bool is_empty(Queue q); 35 | 36 | bool is_full(Queue q); 37 | 38 | #endif /* QUEUEADT_H_ */ 39 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_07/queueADT.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queueADT.h 3 | * 4 | * Created on: May 12, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUEADT_H_ 9 | #define QUEUEADT_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct queue_type *Queue; 15 | 16 | Queue create(void); 17 | 18 | void destroy(Queue q); 19 | 20 | void make_empty(Queue q); 21 | 22 | // Inserting item at the end of the queue 23 | void enqueue(Queue q, Item i); 24 | 25 | // Removing an item from the beginning of the queue 26 | Item dequeue(Queue q); 27 | 28 | // Returning the first item in the queue (without changing the queue) 29 | Item front(Queue q); 30 | 31 | // Returning the last item in the queue (without changing the queue) 32 | Item rear(Queue q); 33 | 34 | bool is_empty(Queue q); 35 | 36 | bool is_full(Queue q); 37 | 38 | #endif /* QUEUEADT_H_ */ 39 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_06/queueADT2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queueADT2.h 3 | * 4 | * Created on: May 12, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUEADT2_H_ 9 | #define QUEUEADT2_H_ 10 | 11 | #include // C99 12 | 13 | typedef int Item; 14 | typedef struct queue_type *Queue; 15 | 16 | Queue create(int size); 17 | 18 | void destroy(Queue q); 19 | 20 | void make_empty(Queue q); 21 | 22 | // Inserting item at the end of the queue 23 | void enqueue(Queue q, Item i); 24 | 25 | // Removing an item from the beginning of the queue 26 | Item dequeue(Queue q); 27 | 28 | // Returning the first item in the queue (without changing the queue) 29 | Item front(Queue q); 30 | 31 | // Returning the last item in the queue (without changing the queue) 32 | Item rear(Queue q); 33 | 34 | bool is_empty(Queue q); 35 | 36 | bool is_full(Queue q); 37 | 38 | #endif /* QUEUEADT2_H_ */ 39 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_01.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Repeated Digits (modified) 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | bool digit_seen[10] = {false}; 16 | bool digit_repeated[10] = {false}; 17 | int digit; 18 | long n; 19 | 20 | printf("Enter a number: "); 21 | scanf("%ld", &n); 22 | 23 | while (n > 0) 24 | { 25 | digit = n % 10; 26 | 27 | if (digit_seen[digit]) 28 | digit_repeated[digit] = true; 29 | else 30 | digit_seen[digit] = true; 31 | 32 | n /= 10; 33 | } 34 | 35 | printf("Repeated digit(s):"); 36 | 37 | for (digit = 0; digit < 10; digit++) 38 | if (digit_repeated[digit]) 39 | printf(" %d", digit); 40 | 41 | printf("\n"); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_04.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 4: Wind Force 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int wind_speed; 16 | 17 | printf("Enter the wind speed (in knots): "); 18 | scanf("%d", &wind_speed); 19 | 20 | printf("Wind force estimation: "); 21 | 22 | if (wind_speed < 1) 23 | { 24 | printf("Calm\n"); 25 | } 26 | else if (wind_speed <= 3) 27 | { 28 | printf("Light air\n"); 29 | } 30 | else if (wind_speed <= 27) 31 | { 32 | printf("Breeze\n"); 33 | } 34 | else if (wind_speed <= 47) 35 | { 36 | printf("Gale\n"); 37 | } 38 | else if (wind_speed <= 63) 39 | { 40 | printf("Storm\n"); 41 | } 42 | else 43 | { 44 | printf("Hurricane\n"); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_12.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 12: Calculating mathematical constant e 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int i, factorial_i = 1; 15 | float e = 1.0f, epsilon, term = 1.0f; 16 | 17 | printf("This program calculates an approximate value of\n"); 18 | printf("the mathematical constant e, using this formula:\n"); 19 | printf("1 + 1/1! + 1/2! + 1/3! + ... + 1/n!\n"); 20 | printf("where n is an integer\n\n"); 21 | 22 | printf("Enter epsilon: "); 23 | scanf("%f", &epsilon); 24 | 25 | for(i = 1; term >= epsilon; i++) 26 | { 27 | // Calculate factorial i to get 1/i! 28 | factorial_i *= i; 29 | 30 | term = 1.0f / factorial_i; 31 | 32 | e += term; 33 | } 34 | 35 | printf("e is approximately equal to %f\n", e); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_03/quicksort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * quicksort.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include "quicksort.h" 9 | 10 | void quicksort(int a[], int low, int high) 11 | { 12 | int middle; 13 | 14 | if(low >= high) 15 | return; 16 | 17 | middle = split(a, low, high); 18 | quicksort(a, low, middle - 1); 19 | quicksort(a, middle + 1, high); 20 | } 21 | 22 | 23 | 24 | int split(int a[], int low, int high) 25 | { 26 | int part_element = a[low]; 27 | 28 | for(;;) 29 | { 30 | while(low < high && part_element <= a[high]) 31 | high--; 32 | 33 | if(low >= high) 34 | break; 35 | 36 | a[low++] = a[high]; 37 | 38 | while(low < high && a[low] <= part_element) 39 | low++; 40 | 41 | if(low >= high) 42 | break; 43 | 44 | a[high--] = a[low]; 45 | } 46 | 47 | a[high] = part_element; 48 | 49 | return high; 50 | } 51 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/justify.c: -------------------------------------------------------------------------------- 1 | /* 2 | * justify.c 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Justify 9 | 10 | #include 11 | #include "line.h" 12 | #include "word.h" 13 | #define MAX_WORD_LEN 20 14 | 15 | int main(void) 16 | { 17 | char word[MAX_WORD_LEN + 2]; 18 | int word_len; 19 | 20 | clear_line(); 21 | 22 | for (;;) 23 | { 24 | read_word(word, MAX_WORD_LEN + 1); 25 | word_len = strlen(word); 26 | 27 | if (word_len == 0) 28 | { 29 | flush_line(); 30 | return 0; // Terminate program 31 | } 32 | 33 | if (word_len > MAX_WORD_LEN) 34 | word[MAX_WORD_LEN] = '*'; 35 | 36 | if (word_len + 1 > space_remaining()) 37 | { 38 | write_line(); 39 | clear_line(); 40 | } 41 | add_word(word); 42 | } 43 | 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_05/queueclient.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queueclient.c 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: queueclient.c 9 | 10 | #include 11 | #include "queueADT.h" 12 | 13 | int main(void) 14 | { 15 | Queue q1, q2; 16 | int n; 17 | 18 | q1 = create(); 19 | q2 = create(); 20 | 21 | enqueue(q1, 1); 22 | enqueue(q1, 2); 23 | 24 | n = dequeue(q1); 25 | printf("Dequeued %d from q1\n", n); 26 | 27 | enqueue(q2, n); 28 | n = dequeue(q1); 29 | printf("Dequeued %d from q1\n", n); 30 | 31 | enqueue(q2, n); 32 | 33 | destroy(q1); 34 | 35 | while (!is_empty(q2)) 36 | printf("Dequeued %d from q2\n", dequeue(q2)); 37 | 38 | enqueue(q2, 3); 39 | make_empty(q2); 40 | 41 | if (is_empty(q2)) 42 | printf("q2 is empty\n"); 43 | else 44 | printf("q2 is not empty\n"); 45 | 46 | destroy(q2); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_07/queueclient.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queueclient.c 3 | * 4 | * Created on: May 12, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 7: queueclient.c 9 | 10 | #include 11 | #include "queueADT.h" 12 | 13 | int main(void) 14 | { 15 | Queue q1, q2; 16 | int n; 17 | 18 | q1 = create(); 19 | q2 = create(); 20 | 21 | enqueue(q1, 1); 22 | enqueue(q1, 2); 23 | 24 | n = dequeue(q1); 25 | printf("Dequeued %d from q1\n", n); 26 | 27 | enqueue(q2, n); 28 | n = dequeue(q1); 29 | printf("Dequeued %d from q1\n", n); 30 | 31 | enqueue(q2, n); 32 | 33 | destroy(q1); 34 | 35 | while (!is_empty(q2)) 36 | printf("Dequeued %d from q2\n", dequeue(q2)); 37 | 38 | enqueue(q2, 3); 39 | make_empty(q2); 40 | 41 | if (is_empty(q2)) 42 | printf("q2 is empty\n"); 43 | else 44 | printf("q2 is not empty\n"); 45 | 46 | destroy(q2); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_06/queueclient.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queueclient.c 3 | * 4 | * Created on: May 12, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: queueclient.c 9 | 10 | #include 11 | #include "queueADT2.h" 12 | 13 | int main(void) 14 | { 15 | Queue q1, q2; 16 | int n; 17 | 18 | q1 = create(2); 19 | q2 = create(2); 20 | 21 | enqueue(q1, 1); 22 | enqueue(q1, 2); 23 | 24 | n = dequeue(q1); 25 | printf("Dequeued %d from q1\n", n); 26 | 27 | enqueue(q2, n); 28 | n = dequeue(q1); 29 | printf("Dequeued %d from q1\n", n); 30 | 31 | enqueue(q2, n); 32 | 33 | destroy(q1); 34 | 35 | while (!is_empty(q2)) 36 | printf("Dequeued %d from q2\n", dequeue(q2)); 37 | 38 | enqueue(q2, 3); 39 | make_empty(q2); 40 | 41 | if (is_empty(q2)) 42 | printf("q2 is empty\n"); 43 | else 44 | printf("q2 is not empty\n"); 45 | 46 | destroy(q2); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_07.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 7: Largest and smallest elements in an array 9 | 10 | #include 11 | #define N 10 12 | 13 | void max_min(int a[], int n, int *max, int *min); 14 | 15 | int main(void) 16 | { 17 | int b[N], i, big, small; 18 | 19 | printf("Enter %d numbers: ", N); 20 | for (i = 0; i < N; i++) 21 | scanf("%d", &b[i]); 22 | 23 | max_min(b, N, &big, &small); 24 | 25 | printf("Largest: %d\n", big); 26 | printf("Smallest: %d\n", small); 27 | 28 | return 0; 29 | } 30 | 31 | void max_min(int a[], int n, int *max, int *min) 32 | { 33 | int *p; 34 | *max = *min = *a; 35 | 36 | for (p = a + 1; p < a + n; p++) 37 | { 38 | if (*p > *max) 39 | *max = *p; 40 | else if (*p < *min) 41 | *min = *p; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_05.c 3 | * 4 | * Created on: Oct 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: tax income 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float income, tax = 0.0f; 15 | 16 | printf("Enter the taxable income: "); 17 | scanf("%f", &income); 18 | 19 | if(income <= 750) 20 | { 21 | tax = 0.01f * income; 22 | } 23 | else if(income <= 2250) 24 | { 25 | tax = 7.5f + 0.02f * income; 26 | } 27 | else if(income <= 3750) 28 | { 29 | tax = 37.50f + 0.03f * income; 30 | } 31 | else if(income <= 5250) 32 | { 33 | tax = 82.50f + 0.04f * income; 34 | } 35 | else if(income <= 7000) 36 | { 37 | tax = 142.50f + 0.05f * income; 38 | } 39 | else if(income > 7000) 40 | { 41 | tax = 230.00f + 0.06f * income; 42 | } 43 | 44 | printf("Tax due = $%.2f", tax); 45 | 46 | return 0; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_16.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 16: Message Reversal 9 | 10 | #include 11 | #define LEN 100 12 | 13 | void reverse(char *message); 14 | 15 | int main(void) 16 | { 17 | char input, message[LEN] = {0}, *msg_ptr = message; 18 | 19 | printf("Enter a message: "); 20 | 21 | // Reading the message 22 | input = getchar(); 23 | 24 | while(input != '\n' && msg_ptr < message + LEN) 25 | { 26 | *msg_ptr++ = input; 27 | input = getchar(); 28 | } 29 | msg_ptr--; 30 | 31 | 32 | reverse(message); 33 | printf("Reversal is: %s\n", message); 34 | 35 | return 0; 36 | } 37 | 38 | void reverse(char *message) 39 | { 40 | char *tail = message, *head = message, temp; 41 | while(*tail) 42 | tail++; 43 | 44 | tail--; 45 | 46 | while(head < tail) 47 | { 48 | temp = *head; 49 | *head++ = *tail; 50 | *tail-- = temp; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_03.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Repeated Digits (modified 3) 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | bool digit_seen[10] = {false}; 16 | int digit; 17 | long n; 18 | 19 | while (1) 20 | { 21 | printf("Enter a number: "); 22 | scanf("%ld", &n); 23 | 24 | // Check termination condition 25 | if (n <= 0) 26 | break; 27 | 28 | while (n > 0) 29 | { 30 | digit = n % 10; 31 | 32 | if (digit_seen[digit]) 33 | break; 34 | digit_seen[digit] = true; 35 | n /= 10; 36 | } 37 | 38 | if (n > 0) 39 | printf("Repeated digit\n"); 40 | else 41 | printf("No repeated digit\n"); 42 | 43 | // Reset digit_seen array 44 | for (digit = 0; digit < 10; digit++) 45 | digit_seen[digit] = false; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_13.c 3 | * 4 | * Created on: Apr 25, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 13: First name and Last name (modified) 9 | 10 | #include 11 | #define NAME_LEN 20 12 | 13 | int main(void) 14 | { 15 | char input_ch, first_ch, last_name[NAME_LEN]; 16 | int i, len = 0; 17 | 18 | printf("Enter a first and last name: "); 19 | 20 | while((input_ch = getchar()) == ' '); // Skipping initial spaces 21 | 22 | first_ch = input_ch; 23 | 24 | while(getchar() != ' '); // Skipping first name letters 25 | 26 | while((input_ch = getchar()) == ' '); // Skipping intermediate spaces 27 | 28 | do 29 | { 30 | last_name[len++] = input_ch; 31 | } while((input_ch = getchar()) != '\n'); 32 | 33 | printf("You entered the name: "); 34 | 35 | for (i = 0; i < len; i++) 36 | putchar(last_name[i]); 37 | 38 | printf(", %c.\n", first_ch); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_04.c 3 | * 4 | * Created on: Mar 14, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Alphabetic phone number 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | char ch; 15 | 16 | printf("Enter phone number: "); 17 | ch = getchar(); 18 | 19 | while (ch != '\n') 20 | { 21 | switch(ch) 22 | { 23 | case 'A': case 'B': case 'C': putchar('2'); break; 24 | case 'D': case 'E': case 'F': putchar('3'); break; 25 | case 'G': case 'H': case 'I': putchar('4'); break; 26 | case 'J': case 'K': case 'L': putchar('5'); break; 27 | case 'M': case 'N': case 'O': putchar('6'); break; 28 | case 'P': case 'R': case 'S': putchar('7'); break; 29 | case 'T': case 'U': case 'V': putchar('8'); break; 30 | case 'W': case 'X': case 'Y': putchar('9'); break; 31 | default: putchar(ch); 32 | } 33 | 34 | ch = getchar(); 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_02.c 3 | * 4 | * Created on: Oct 25, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: tax income 9 | 10 | #include 11 | 12 | float compute_tax(float income); 13 | 14 | int main(void) 15 | { 16 | float income; 17 | 18 | printf("Enter the taxable income: "); 19 | scanf("%f", &income); 20 | 21 | printf("Tax due = %.2f", compute_tax(income)); 22 | 23 | return 0; 24 | } 25 | 26 | float compute_tax(float income) 27 | { 28 | float tax = 0.0f; 29 | 30 | if (income <= 750) 31 | tax = 0.01f * income; 32 | else if (income <= 2250) 33 | tax = 7.5f + 0.02f * income; 34 | else if (income <= 3750) 35 | tax = 37.50f + 0.03f * income; 36 | else if (income <= 5250) 37 | tax = 82.50f + 0.04f * income; 38 | else if (income <= 7000) 39 | tax = 142.50f + 0.05f * income; 40 | else if (income > 7000) 41 | tax = 230.00f + 0.06f * income; 42 | 43 | return tax; 44 | } 45 | -------------------------------------------------------------------------------- /Ch21_The_Standard_Library/ch21_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch21_prog_proj_01.c 3 | * 4 | * Created on: May 23, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: offsetof 9 | 10 | #include 11 | #include 12 | 13 | struct s 14 | { 15 | char a; 16 | int b[2]; 17 | float c; 18 | }; 19 | 20 | int main(void) 21 | { 22 | struct s example; 23 | 24 | printf("Size of a = %llu\n", sizeof(example.a)); 25 | printf("Offset of a = %llu\n\n", offsetof(struct s, a)); 26 | 27 | printf("Size of b = %llu\n", sizeof(example.b)); 28 | printf("Offset of b = %llu\n\n", offsetof(struct s, b)); 29 | 30 | printf("Size of c = %llu\n", sizeof(example.c)); 31 | printf("Offset of c = %llu\n\n", offsetof(struct s, c)); 32 | 33 | printf("Size of the whole structure = %llu\n", sizeof(struct s)); 34 | 35 | return 0; 36 | } 37 | 38 | /* 39 | * From the output, we get that there is a hole of size 3 bytes that lies 40 | * between member a and member b of the structure 41 | * 42 | */ 43 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_10.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 10: Numerical to Letter Grade 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int grade, tens_digit; 16 | 17 | printf("Enter numerical grade: "); 18 | scanf("%d", &grade); 19 | 20 | if(grade < 0 || grade > 100) 21 | { 22 | printf("Error: invalid numerical grade"); 23 | return 0; 24 | } 25 | 26 | tens_digit = grade / 10; 27 | 28 | switch(tens_digit) 29 | { 30 | case 0: case 1: case 2: case 3: case 4: case 5: 31 | printf("Letter grade: F\n"); 32 | break; 33 | case 6: 34 | printf("Letter grade: D\n"); 35 | break; 36 | case 7: 37 | printf("Letter grade: C\n"); 38 | break; 39 | case 8: 40 | printf("Letter grade: B\n"); 41 | break; 42 | case 9: case 10: 43 | printf("Letter grade: A\n"); 44 | break; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_05/stack.c: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "stack.h" 11 | 12 | int contents[STACK_SIZE]; 13 | 14 | int top = 0; 15 | 16 | void make_empty(void) 17 | { 18 | top = 0; 19 | } 20 | 21 | bool is_empty(void) 22 | { 23 | return top == 0; 24 | } 25 | 26 | bool is_full(void) 27 | { 28 | return top == STACK_SIZE; 29 | } 30 | 31 | void push(int i) 32 | { 33 | if(is_full()) 34 | { 35 | stack_overflow(); 36 | } 37 | else 38 | { 39 | contents[top++] = i; 40 | } 41 | } 42 | 43 | int pop(void) 44 | { 45 | if(is_empty()) 46 | { 47 | stack_underflow(); 48 | } 49 | 50 | return contents[--top]; 51 | } 52 | 53 | void stack_overflow(void) 54 | { 55 | printf("Expression is too complex"); 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | void stack_underflow(void) 60 | { 61 | printf("Not enough operands in expression"); 62 | exit(EXIT_FAILURE); 63 | } 64 | -------------------------------------------------------------------------------- /Ch11_Pointers/ch11_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch11_prog_proj_03.c 3 | * 4 | * Created on: Nov 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Fraction's lowest terms (GCD) 9 | 10 | #include 11 | #include 12 | 13 | void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator); 14 | int gcd(int a, int b); 15 | 16 | int main(void) 17 | { 18 | int num1, den1, num2, den2; 19 | 20 | printf("Enter a fraction: "); 21 | scanf("%d/%d", &num1, &den1); 22 | 23 | reduce(num1, den1, &num2, &den2); 24 | 25 | printf("In lowest terms: %d/%d", num2, den2); 26 | 27 | return 0; 28 | } 29 | 30 | void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator) 31 | { 32 | int hcf = gcd(numerator, denominator); 33 | (*reduced_numerator) = numerator / hcf; 34 | (*reduced_denominator) = denominator / hcf; 35 | } 36 | 37 | int gcd(int a, int b) 38 | { 39 | if(b == 0) 40 | return a; 41 | 42 | return gcd(b, a % b); 43 | } 44 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_07.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 7: 5 x 5 array of integers 9 | 10 | #include 11 | 12 | #define NUM_ROWS 5 13 | #define NUM_COLS 5 14 | 15 | int main(void) 16 | { 17 | int arr[NUM_ROWS][NUM_COLS], row, col; 18 | int row_totals[NUM_ROWS] = {0}, column_totals[NUM_COLS] = {0}; 19 | 20 | for (row = 0; row < NUM_ROWS; row++) 21 | { 22 | printf("Enter row %d: ", row + 1); 23 | for (col = 0; col < NUM_COLS; col++) 24 | { 25 | scanf("%d", &arr[row][col]); 26 | row_totals[row] += arr[row][col]; 27 | column_totals[col] += arr[row][col]; 28 | } 29 | } 30 | 31 | printf("\nRow totals:"); 32 | for (row = 0; row < NUM_ROWS; row++) 33 | printf(" %d", row_totals[row]); 34 | 35 | printf("\nColumn totals:"); 36 | for (col = 0; col < NUM_COLS; col++) 37 | printf(" %d", column_totals[col]); 38 | 39 | printf("\n"); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_05/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef QUEUE_H_ 9 | #define QUEUE_H_ 10 | 11 | #include // C99 12 | 13 | #define QUEUE_SIZE 100 14 | 15 | typedef struct 16 | { 17 | int buffer[QUEUE_SIZE]; // Queue array 18 | int rear; // Position of the first empty slot in the array 19 | int front; // Position of the next item to be removed 20 | int qsize; // Number of items in the queue 21 | } Queue; 22 | 23 | // Inserting item at the end of the queue 24 | void enqueue(Queue *q, int i); 25 | 26 | // Removing an item from the beginning of the queue 27 | int dequeue(Queue *q); 28 | 29 | // Returning the first item in the queue (without changing the queue) 30 | int front_item(const Queue *q); 31 | 32 | // Returning the last item in the queue (without changing the queue) 33 | int rear_item(const Queue *q); 34 | 35 | // Testing whether the queue is empty 36 | bool is_empty(const Queue *q); 37 | 38 | #endif /* QUEUE_H_ */ 39 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_12.c 3 | * 4 | * Created on: Apr 21, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 12: Expression Evaluation 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float operand, accumulator; 15 | char operator; 16 | 17 | // Automatically accumulate the first operand 18 | printf("Enter an expression: "); 19 | scanf("%f", &accumulator); 20 | 21 | do 22 | { 23 | // Skip possible whitespaces 24 | while((operator = getchar()) == ' '); 25 | 26 | if (operator == '\n') 27 | break; 28 | else 29 | scanf("%f", &operand); 30 | 31 | switch (operator) 32 | { 33 | case '+': accumulator += operand; break; 34 | case '-': accumulator -= operand; break; 35 | case '*': accumulator *= operand; break; 36 | case '/': accumulator /= operand; break; 37 | default: break; 38 | } 39 | } while (1); 40 | 41 | printf("Value of expression: %.1f\n", accumulator); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_04/a/stack.c: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.c 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Stack type (Fixed-length array representation) 9 | 10 | #include 11 | #include 12 | #include "stack.h" 13 | 14 | #define PUBLIC /* empty */ 15 | #define PRIVATE static 16 | 17 | PRIVATE void terminate(const char *message) 18 | { 19 | printf("%s\n", message); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | PUBLIC void make_empty(Stack *s) 24 | { 25 | s->top = 0; 26 | } 27 | 28 | PUBLIC bool is_empty(const Stack *s) 29 | { 30 | return s->top == 0; 31 | } 32 | 33 | PUBLIC bool is_full(const Stack *s) 34 | { 35 | return s->top == STACK_SIZE; 36 | } 37 | 38 | PUBLIC void push(Stack *s, int i) 39 | { 40 | if (is_full(s)) 41 | terminate("Error in push: stack is full."); 42 | 43 | s->contents[s->top++] = i; 44 | } 45 | 46 | PUBLIC int pop(Stack *s) 47 | { 48 | if (is_empty(s)) 49 | terminate("Error in pop: stack is empty."); 50 | 51 | return s->contents[--s->top]; 52 | } 53 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_04.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Palindrome (Same as 2.b) 9 | 10 | #include 11 | #include 12 | #include 13 | #define LEN 100 14 | 15 | int main(void) 16 | { 17 | char input, message[LEN], *left_ch = message, *right_ch = message; 18 | bool palindrome = true; 19 | 20 | // Reading the message 21 | printf("Enter a message: "); 22 | while ((input = getchar()) != '\n' && right_ch < message + LEN) 23 | if (isalpha(input)) 24 | *right_ch++ = tolower(input); 25 | 26 | // Go back to the message end 27 | right_ch--; 28 | 29 | // Palindrome Detection 30 | while (left_ch < right_ch) 31 | { 32 | if (*left_ch++ != *right_ch--) 33 | { 34 | palindrome = false; 35 | break; 36 | } 37 | } 38 | 39 | if (palindrome) 40 | printf("Palindrome"); 41 | else 42 | printf("Not a palindrome"); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_04/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 | // (Section 19.4, Page 496) 11 | 12 | /* 13 | * stackADT.h 14 | * 15 | * Created on: May 11, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | #ifndef STACKADT_H_ 20 | #define STACKADT_H_ 21 | 22 | #include // C99 23 | 24 | typedef struct stack_type *Stack; 25 | 26 | Stack create(void); 27 | void destroy(Stack s); 28 | void make_empty(Stack s); 29 | bool is_empty(Stack s); 30 | bool is_full(Stack s); 31 | 32 | void push(Stack s, void *i); 33 | void *pop(Stack s); 34 | 35 | #endif /* STACKADT_H_ */ 36 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_08.c 3 | * 4 | * Created on: Dec 20, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 8: Scrabble 9 | 10 | #include 11 | #include 12 | 13 | int compute_scrabble_value(const char *word); 14 | 15 | int main(void) 16 | { 17 | char input_word[100]; // Total number of tiles in real life 18 | char *word_ptr = input_word; 19 | int score; 20 | 21 | printf("Enter a word: "); 22 | 23 | while ((*word_ptr = getchar()) != '\n') 24 | word_ptr++; 25 | 26 | *word_ptr = '\0'; // replace '\n' by '\0' 27 | 28 | score = compute_scrabble_value(input_word); 29 | printf("Scrabble value: %d\n", score); 30 | 31 | return 0; 32 | } 33 | 34 | int compute_scrabble_value(const char *word) 35 | { 36 | int score = 0; 37 | 38 | int face_value[26] = 39 | { 40 | 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 41 | }; 42 | 43 | while (*word) 44 | if (isalpha(*word)) 45 | score += face_value[toupper(*word++) - 'A']; 46 | 47 | return score; 48 | } 49 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_04/b/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.h 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef STACK_H_ 9 | #define STACK_H_ 10 | 11 | #include 12 | 13 | struct node 14 | { 15 | int data; 16 | struct node *next; 17 | }; 18 | 19 | // I've included two different solutions 20 | // Feel free to unlock the solution of your interest by uncommenting 21 | // a directive while commenting the other 22 | 23 | #define Q4_B_SOL1 24 | //#define Q4_B_SOL2 25 | 26 | // I do this alot in my local repo but this is the first time I show it 27 | // in my Github repo (Hopefully this will give you some intuition) 28 | 29 | #ifdef Q4_B_SOL1 30 | 31 | typedef struct 32 | { 33 | struct node *top; 34 | } Stack; 35 | 36 | #elif Q4_B_SOL2 37 | 38 | typedef struct node *Stack; 39 | 40 | #endif /* Q4_B_SOL */ 41 | 42 | void make_empty(Stack *s); 43 | 44 | bool is_empty(const Stack *s); 45 | 46 | bool is_full(const Stack *s); 47 | 48 | void push(Stack *s, int i); 49 | 50 | int pop(Stack *s); 51 | 52 | #endif /* STACK_H_ */ 53 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_01/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Stack 9 | 10 | #include 11 | #include 12 | #include // C99 Only 13 | #include "stackADT.h" 14 | 15 | int main(void) 16 | { 17 | char input_ch; 18 | bool match; 19 | Stack s = create(); 20 | 21 | printf("Enter parentheses and/or braces: "); 22 | 23 | while ((input_ch = getchar()) != '\n') 24 | { 25 | if (input_ch == '(' || input_ch == '{') 26 | push(s, input_ch); 27 | else if (input_ch == ')' && pop(s) != '(') 28 | { 29 | match = false; 30 | break; 31 | } 32 | else if (input_ch == '}' && pop(s) != '{') 33 | { 34 | match = false; 35 | break; 36 | } 37 | else // { matched } or ( matched ) 38 | match = true; 39 | } 40 | 41 | // Checking if stack is empty 42 | if (is_empty(s) && match) 43 | printf("Parentheses/braces are nested properly\n"); 44 | else 45 | printf("Parentheses/braces aren't nested properly\n"); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_15.c 3 | * 4 | * Created on: Dec 20, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 15: Caesar Cipher 9 | 10 | #include 11 | #define MAX_LEN 80 12 | 13 | int main(void) 14 | { 15 | char input_ch, plain[MAX_LEN], cipher; 16 | int msg_len = 0, key, i; 17 | 18 | printf("Enter message to be encrypted: "); 19 | 20 | while ((input_ch = getchar()) != '\n') 21 | { 22 | plain[msg_len++] = input_ch; 23 | } 24 | 25 | printf("Enter shift amount (1-25): "); 26 | scanf("%d", &key); 27 | 28 | printf("Encrypted message: "); 29 | 30 | for (i = 0; i < msg_len; i++) 31 | { 32 | if(plain[i] >= 'A' && plain[i] <= 'Z') 33 | { 34 | cipher = ((plain[i] - 'A') + key) % 26 + 'A'; 35 | } 36 | else if(plain[i] >= 'a' && plain[i] <= 'z') 37 | { 38 | cipher = ((plain[i] - 'a') + key) % 26 + 'a'; 39 | } 40 | else 41 | { 42 | cipher = plain[i]; 43 | } 44 | 45 | putchar(cipher); 46 | } 47 | putchar('\n'); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_09.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 9: Remaining balance on a loan 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float loan_balance, monthly_payment; 15 | float interest_rate, monthly_interest_rate; 16 | int i, number_of_payments; 17 | 18 | handle_eclipse_terminal_emulator_bug(); 19 | 20 | printf("Enter amount of loan: "); 21 | scanf("%f", &loan_balance); 22 | 23 | printf("Enter interest rate: "); 24 | scanf("%f", &interest_rate); 25 | monthly_interest_rate = interest_rate / (100 * 12); 26 | 27 | printf("Enter monthly payment: "); 28 | scanf("%f", &monthly_payment); 29 | 30 | printf("Enter the number of payments: "); 31 | scanf("%d", &number_of_payments); 32 | 33 | printf("\n"); 34 | 35 | for(i = 1; i <= number_of_payments; i++) 36 | { 37 | loan_balance = loan_balance - monthly_payment + loan_balance * monthly_interest_rate; 38 | printf("Balance remaining after payment %d: $%.2f\n", i, loan_balance); 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ch11_Pointers/ch11_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch11_prog_proj_01.c 3 | * 4 | * Created on: Nov 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 1: Smallest number of bills 9 | 10 | #include 11 | 12 | void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones); 13 | 14 | int main(void) 15 | { 16 | int USD_amount, _20_USD = 0, _10_USD = 0, _5_USD = 0, _1_USD = 0; 17 | 18 | printf("Enter a dollar amount: "); 19 | scanf("%d", &USD_amount); 20 | 21 | pay_amount(USD_amount, &_20_USD, &_10_USD, &_5_USD, &_1_USD); 22 | 23 | printf("$20 bills: %d\n$10 bills: %d\n $5 bills: %d\n $1 bills: %d", 24 | _20_USD, _10_USD, _5_USD, _1_USD); 25 | 26 | return 0; 27 | } 28 | 29 | void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones) 30 | { 31 | if(dollars >= 20) 32 | { 33 | *twenties = dollars / 20; 34 | dollars %= 20; 35 | } 36 | 37 | if(dollars >= 10) 38 | { 39 | *tens = dollars / 10; 40 | dollars %= 10; 41 | } 42 | 43 | if(dollars >= 5) 44 | { 45 | *fives = dollars / 5; 46 | dollars %= 5; 47 | } 48 | 49 | *ones = dollars; 50 | } 51 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_06/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 | // (Section 19.4, Page 496) 11 | 12 | /* 13 | * stackADT.h 14 | * 15 | * Created on: May 10, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | #ifndef STACKADT_H_ 20 | #define STACKADT_H_ 21 | 22 | #include // C99 23 | 24 | typedef int Item; 25 | typedef struct stack_type *Stack; 26 | 27 | Stack create(void); 28 | void destroy(Stack s); 29 | void make_empty(Stack s); 30 | bool is_empty(Stack s); 31 | bool is_full(Stack s); 32 | 33 | void push(Stack s, Item i); 34 | Item pop(Stack s); 35 | Item peek(Stack s); 36 | 37 | #endif /* STACKADT_H_ */ 38 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_06/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 | // (Section 19.4, Page 496) 11 | 12 | /* 13 | * stackADT.h 14 | * 15 | * Created on: May 10, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | #ifndef STACKADT_H_ 20 | #define STACKADT_H_ 21 | 22 | #include // C99 23 | 24 | typedef int Item; 25 | typedef struct stack_type *Stack; 26 | 27 | Stack create(void); 28 | void destroy(Stack s); 29 | void make_empty(Stack s); 30 | bool is_empty(Stack s); 31 | bool is_full(Stack s); 32 | 33 | void push(Stack s, Item i); 34 | Item pop(Stack s); 35 | Item peek(Stack s); 36 | 37 | #endif /* STACKADT_H_ */ 38 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_03/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 | // (Section 19.4, Page 496) 11 | 12 | /* 13 | * stackADT.h 14 | * 15 | * Created on: May 11, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | #ifndef STACKADT_H_ 20 | #define STACKADT_H_ 21 | 22 | #include // C99 23 | 24 | typedef int Item; 25 | typedef struct stack_type *Stack; 26 | 27 | Stack create(void); 28 | void destroy(Stack s); 29 | void make_empty(Stack s); 30 | bool is_empty(Stack s); 31 | bool is_full(Stack s); 32 | 33 | void push(Stack s, Item i); 34 | Item pop(Stack s); 35 | int length(Stack s); 36 | 37 | #endif /* STACKADT_H_ */ 38 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_13.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 13: Caesar Cipher 9 | 10 | #include 11 | 12 | void encrypt(char *message, int shift); 13 | 14 | int main(void) 15 | { 16 | char input_ch, plain[80]; 17 | int msg_len = 0, key; 18 | 19 | printf("Enter message to be encrypted: "); 20 | 21 | while((input_ch = getchar()) != '\n') 22 | { 23 | plain[msg_len++] = input_ch; 24 | } 25 | plain[msg_len] = '\0'; // Turn the plain array into a string 26 | 27 | printf("Enter shift amount (1-25): "); 28 | scanf("%d", &key); 29 | 30 | encrypt(plain, key); 31 | printf("Encrypted message: %s\n", plain); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | void encrypt(char *message, int shift) 38 | { 39 | while(*message) 40 | { 41 | if(*message >= 'A' && *message <= 'Z') 42 | { 43 | *message = ((*message - 'A') + shift) % 26 + 'A'; 44 | } 45 | else if(*message > 'Z' && *message <= 'z') 46 | { 47 | *message = ((*message - 'a') + shift) % 26 + 'a'; 48 | } 49 | message++; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_13.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 13: Average word length for a sentence 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int total_len = 0, word_len = 0, word_count = 0; 15 | char input_ch; 16 | float avg; 17 | 18 | printf("Enter a sentence: "); 19 | 20 | while ((input_ch = getchar()) == ' '); // skip leading whitespaces 21 | word_len++; // first word found 22 | 23 | while((input_ch = getchar()) != '\n') 24 | { 25 | if(input_ch != ' ') // calculating word length 26 | word_len++; 27 | else if(word_len != 0) // count all mid spaces as one space only 28 | { 29 | total_len += word_len; 30 | word_count++; 31 | word_len = 0; 32 | } 33 | } 34 | 35 | if (word_len != 0) // detect last word and disregard spaces at the end 36 | { 37 | word_count++; 38 | total_len += word_len; 39 | } 40 | 41 | avg = ((float) total_len) / word_count; 42 | printf("Average word length: %.1f\n", avg); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_05.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Computing Interest (modified) 9 | 10 | #include 11 | 12 | #define NUM_RATES ((int) (sizeof(value) / sizeof(value[0]))) 13 | #define INITIAL_BALANCE 100.00 14 | 15 | int main(void) 16 | { 17 | int i, low_rate, num_years, year, month; 18 | double value[5]; 19 | 20 | printf("Enter interest rate: "); 21 | scanf("%d", &low_rate); 22 | 23 | printf("Enter number of years: "); 24 | scanf("%d", &num_years); 25 | 26 | printf("\nYears"); 27 | for (i = 0; i < NUM_RATES; i++) 28 | { 29 | printf("%6d%%", low_rate + i); 30 | value[i] = INITIAL_BALANCE; 31 | } 32 | printf("\n"); 33 | 34 | for (year = 1; year <= num_years; year++) 35 | { 36 | printf("%3d ", year); 37 | for (i = 0; i < NUM_RATES; i++) 38 | { 39 | for (month = 1; month <= 12; month++) 40 | value[i] += (low_rate + i) / (12 * 100.0) * value[i]; 41 | 42 | printf("%7.2f", value[i]); 43 | } 44 | printf("\n"); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_07.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 7: Largest and smallest integer 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int num1, num2, num3, num4, max12, min12, max34, min34; 16 | int largest_num, smallest_num; 17 | 18 | printf("Enter four integers: "); 19 | scanf("%d %d %d %d", &num1, &num2, &num3, &num4); 20 | 21 | if (num1 > num2) 22 | { 23 | max12 = num1; 24 | min12 = num2; 25 | } 26 | else 27 | { 28 | max12 = num2; 29 | min12 = num1; 30 | } 31 | 32 | if (num3 > num4) 33 | { 34 | max34 = num3; 35 | min34 = num4; 36 | } 37 | else 38 | { 39 | max34 = num4; 40 | min34 = num3; 41 | } 42 | 43 | if (max12 > max34) 44 | largest_num = max12; 45 | else 46 | largest_num = max34; 47 | 48 | if (min12 < min34) 49 | smallest_num = min12; 50 | else 51 | smallest_num = min34; 52 | 53 | 54 | printf("Largest: %d\n", largest_num); 55 | printf("Smallest: %d\n", smallest_num); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_17.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 17: Palindrome 9 | 10 | #include 11 | #include 12 | #include 13 | #define LEN 100 14 | 15 | bool is_palindrome(const char *message); 16 | 17 | int main(void) 18 | { 19 | char input, message[LEN], *msg_ptr = message; 20 | 21 | // Reading the message 22 | printf("Enter a message: "); 23 | while ((input = getchar()) != '\n' && msg_ptr < message + LEN) 24 | if (isalpha(input)) 25 | *msg_ptr++ = tolower(input); 26 | 27 | *msg_ptr = '\0'; 28 | 29 | // Palindrome Detection 30 | if (is_palindrome(message)) 31 | printf("Palindrome\n"); 32 | else 33 | printf("Not a palindrome\n"); 34 | 35 | return 0; 36 | } 37 | 38 | bool is_palindrome(const char *message) 39 | { 40 | const char *head = message, *tail = message; 41 | 42 | while (*tail++); 43 | tail -= 2; // Go to last character 44 | 45 | while (head <= tail) 46 | if (*head++ != *tail--) 47 | return false; 48 | 49 | return true; 50 | } 51 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_07.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 | // (Section 6.3, Pages 110-111) 11 | 12 | /* 13 | * ch6_prog_proj_07.c 14 | * 15 | * Created on: Mar 12, 2024 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // Programming Project 7: Table of squares using an odd method 20 | 21 | #include 22 | 23 | int main(void) 24 | { 25 | int i, n, odd, square; 26 | 27 | printf("This program prints a table of squares.\n"); 28 | printf("Enter number of entries in table: "); 29 | scanf("%d", &n); 30 | 31 | odd = 3; 32 | for(i = 1, square = 1; i <= n; odd += 2, ++i) 33 | { 34 | printf("%10d%10d\n", i, square); 35 | 36 | square += odd; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_06.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 6: UPC (modified) 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5; 16 | int first_sum, second_sum, total; 17 | int check_digit, computed_check_digit; 18 | 19 | printf("Enter the first (single) digit: "); 20 | scanf("%1d", &d); 21 | 22 | printf("Enter first group of five digits: "); 23 | scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); 24 | 25 | printf("Enter second group of five digits: "); 26 | scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); 27 | 28 | printf("Enter the last (single) digit: "); 29 | scanf("%1d", &check_digit); 30 | 31 | first_sum = d + i2 + i4 + j1 + j3 + j5; 32 | second_sum = i1 + i3 + i5 + j2 + j4; 33 | total = 3 * first_sum + second_sum; 34 | 35 | computed_check_digit = 9 - ((total - 1) % 10); 36 | 37 | if (check_digit == computed_check_digit) 38 | { 39 | printf("VALID\n"); 40 | } 41 | else 42 | { 43 | printf("NOT VALID\n"); 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_01.c 3 | * 4 | * Created on: Dec 4, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Message Reversal 9 | 10 | // (a) 11 | /* 12 | #include 13 | #define LEN 100 14 | 15 | int main(void) 16 | { 17 | char input, message[LEN]; 18 | int i, msg_len = 0; 19 | 20 | // Reading the message 21 | printf("Enter a message: "); 22 | while ((input = getchar()) != '\n' && msg_len < LEN) 23 | message[msg_len++] = input; 24 | 25 | // Reversing the message 26 | printf("Reversal is: "); 27 | for (i = msg_len - 1; i >= 0; i--) 28 | putchar(message[i]); 29 | 30 | return 0; 31 | } 32 | */ 33 | 34 | // (b) 35 | /* 36 | #include 37 | #define LEN 100 38 | 39 | int main(void) 40 | { 41 | char input, message[LEN], *msg_ptr = message; 42 | 43 | // Reading the message 44 | printf("Enter a message: "); 45 | while ((input = getchar()) != '\n' && msg_ptr < message + LEN) 46 | *msg_ptr++ = input; 47 | 48 | // Reversing the message 49 | printf("Reversal is: "); 50 | while (msg_ptr > message) 51 | putchar(*--msg_ptr); 52 | 53 | return 0; 54 | } 55 | */ 56 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch6_prog_proj_10.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 10: Comparing Dates 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int input_mm, input_dd, input_yy; 15 | int earliest_mm, earliest_dd, earliest_yy; 16 | 17 | handle_eclipse_terminal_emulator_bug(); 18 | 19 | printf("Enter a date (mm/dd/yy): "); 20 | scanf("%d /%d /%d", &earliest_mm, &earliest_dd, &earliest_yy); 21 | 22 | do 23 | { 24 | printf("Enter a date (mm/dd/yy): "); 25 | scanf("%d /%d /%d", &input_mm, &input_dd, &input_yy); 26 | 27 | // Naive condition (assuming 0/0/0 is the only wrong input) 28 | if(input_mm == 0 && input_dd == 0 && input_yy == 0) 29 | break; 30 | 31 | if((input_yy < earliest_yy) || 32 | (input_yy == earliest_yy && input_mm < earliest_mm) || 33 | (input_yy == earliest_yy && input_mm == earliest_mm && input_dd < earliest_dd)) 34 | { 35 | earliest_yy = input_yy; 36 | earliest_mm = input_mm; 37 | earliest_dd = input_dd; 38 | } 39 | 40 | } while(1); 41 | 42 | printf("%d/%d/%02d is the earliest date\n", earliest_mm, earliest_dd, earliest_yy); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_14.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 14: Reverse words in a sentence (New implementation) 9 | 10 | #include 11 | #define MAX_LEN 100 12 | 13 | int main(void) 14 | { 15 | char sentence[MAX_LEN], input_ch; 16 | int len = 0, word_start, word_end; 17 | 18 | printf("Enter a sentence: "); 19 | while ((input_ch = getchar()) != '\n') 20 | { 21 | if (input_ch == '.' || input_ch == '?' || input_ch == '!') 22 | break; 23 | else 24 | sentence[len++] = input_ch; 25 | } 26 | 27 | printf("Reversal of sentence: "); 28 | for (word_start = len - 1; word_start > 0;) 29 | { 30 | while (sentence[word_start - 1] != ' ' && word_start > 0) 31 | word_start--; 32 | 33 | for (word_end = word_start; word_end < len && sentence[word_end] != ' '; word_end++) 34 | putchar(sentence[word_end]); 35 | 36 | while (sentence[word_start - 1] == ' ' && word_start > 0) 37 | { 38 | putchar(' '); 39 | word_start--; 40 | } 41 | } 42 | 43 | putchar(input_ch); // Print the terminating character 44 | putchar('\n'); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_09.c 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 9: Comparing Dates 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int mm1, dd1, yy1, mm2, dd2, yy2, earlier_date = 0; 15 | 16 | printf("Enter first date (mm/dd/yy): "); 17 | scanf(" %d / %d / %d", &mm1, &dd1, &yy1); 18 | 19 | printf("Enter second date (mm/dd/yy): "); 20 | scanf(" %d / %d / %d", &mm2, &dd2, &yy2); 21 | 22 | if(yy1 < yy2) 23 | earlier_date = 1; 24 | else if(yy1 > yy2) 25 | earlier_date = 2; 26 | else 27 | { 28 | if(mm1 < mm2) 29 | earlier_date = 1; 30 | else if(mm1 > mm2) 31 | earlier_date = 2; 32 | else 33 | { 34 | if(dd1 < dd2) 35 | earlier_date = 1; 36 | else if(dd1 > dd2) 37 | earlier_date = 2; 38 | } 39 | } 40 | 41 | 42 | if(earlier_date == 1) 43 | { 44 | printf("%d/%d/%02d is earlier than %d/%d/%02d\n", mm1, dd1, yy1, mm2, dd2, yy2); 45 | } 46 | else if(earlier_date == 2) 47 | { 48 | printf("%d/%d/%02d is earlier than %d/%d/%02d\n", mm2, dd2, yy2, mm1, dd1, yy1); 49 | } 50 | else 51 | { 52 | printf("Both dates are the same\n"); 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_03.c 3 | * 4 | * Created on: Dec 18, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 3: Dealing random hand of cards(8.2 deal.c) 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define NUM_SUITS 4 16 | #define NUM_RANKS 13 17 | 18 | int main(void) 19 | { 20 | bool in_hand[NUM_SUITS][NUM_RANKS] = {{false}}; 21 | int num_cards, rank, suit; 22 | 23 | const char *rank_code[] = 24 | { 25 | "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", 26 | "Ten", "Jack", "Queen", "King", "Ace" 27 | }; 28 | 29 | const char *suit_code[] = {"clubs", "diamonds", "hearts", "spaces"}; 30 | 31 | srand((unsigned) time(NULL)); // Setting a seed 32 | 33 | printf("Enter number of cards in hand: "); 34 | scanf("%d", &num_cards); 35 | 36 | printf("You hand:\n"); 37 | 38 | while(num_cards > 0) 39 | { 40 | suit = rand() % NUM_SUITS; 41 | rank = rand() % NUM_RANKS; 42 | 43 | if(!in_hand[suit][rank]) 44 | { 45 | in_hand[suit][rank] = true; 46 | num_cards--; 47 | printf("%s of %s\n", rank_code[rank], suit_code[suit]); 48 | } 49 | } 50 | printf("\n"); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_01.c 3 | * 4 | * Created on: Oct 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 1: Selection sort 10 | #include 11 | 12 | void selection_sort(int a[], int n); 13 | 14 | int main(void) 15 | { 16 | int i, n; 17 | 18 | printf("Enter the number of integers to be entered: "); 19 | scanf("%d", &n); 20 | 21 | int a[n]; 22 | 23 | printf("Enter the list of numbers: "); 24 | 25 | for (i = 0; i < n; i++) 26 | scanf("%d", &a[i]); 27 | 28 | selection_sort(a, n); 29 | 30 | printf("Array after sorting:"); 31 | 32 | for (i = 0; i < n; i++) 33 | printf(" %d", a[i]); 34 | 35 | putchar('\n'); 36 | 37 | return 0; 38 | } 39 | 40 | 41 | void selection_sort(int a[], int n) 42 | { 43 | int i, largest_val, largest_i; 44 | 45 | if (n == 1) 46 | return; 47 | 48 | largest_val = a[0], largest_i = 0; 49 | for (i = 1; i < n; i++) 50 | { 51 | if (a[i] > largest_val) 52 | { 53 | largest_val = a[i]; 54 | largest_i = i; 55 | } 56 | } 57 | 58 | // Moving to last position 59 | a[largest_i] = a[n - 1]; 60 | a[n - 1] = largest_val; 61 | 62 | selection_sort(a, n - 1); 63 | } 64 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_12.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 12: Reverse words in a sentence 9 | 10 | #include 11 | #include 12 | 13 | #define WORDS_NUM 30 14 | #define WORDS_LEN 20 15 | 16 | bool end_with_special_ch(char *str, char *special_ch); 17 | 18 | int main(void) 19 | { 20 | char sentence[WORDS_NUM][WORDS_LEN + 1], special_ch; 21 | int i; 22 | 23 | printf("Enter a sentence: "); 24 | for (i = 0; i < WORDS_NUM; i++) 25 | { 26 | scanf("%s", sentence[i]); 27 | if (end_with_special_ch(sentence[i], &special_ch)) 28 | break; 29 | } 30 | 31 | if (i == WORDS_NUM) 32 | i--; 33 | 34 | // Printing the reversed sentence 35 | printf("Reversal of sentence: "); 36 | while (i > 0) 37 | printf("%s ", sentence[i--]); 38 | 39 | printf("%s%c\n", sentence[i], special_ch); 40 | 41 | return 0; 42 | } 43 | 44 | bool end_with_special_ch(char *str, char *special_ch) 45 | { 46 | while (*str++); 47 | str -= 2; // Go back to the last character 48 | 49 | if (*str == '.' || *str == '!' || *str == '?') 50 | { 51 | *special_ch = *str; 52 | *str = '\0'; 53 | return true; 54 | } 55 | 56 | return false; 57 | } 58 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_07.c 3 | * 4 | * Created on: Dec 20, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 7: English word for a two-digit number 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int units, tens, num; 15 | char *tens_str[] = 16 | { 17 | "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" 18 | }; 19 | 20 | char *units_str[] = 21 | { 22 | "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" 23 | }; 24 | 25 | char *special_num_str[] = 26 | { 27 | "ten", "eleven", "twelve", "thirteen", "fourteen", 28 | "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" 29 | }; 30 | 31 | printf("Enter a two-digit number: "); 32 | scanf("%d", &num); 33 | 34 | tens = num / 10; 35 | units = num % 10; 36 | 37 | printf("You entered the number "); 38 | 39 | if (num < 10) 40 | printf("%s", units_str[num]); 41 | else if (num >= 10 && num <= 19) 42 | printf("%s", special_num_str[units]); 43 | else 44 | { 45 | if (tens) 46 | printf("%s", tens_str[tens - 2]); 47 | 48 | if (units && num > 19) 49 | printf("-%s", units_str[units]); 50 | } 51 | 52 | printf(".\n"); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_09.c 3 | * 4 | * Created on: Dec 21, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 9: Number of Vowels 9 | 10 | #include 11 | #include 12 | #define SENT_LEN 100 13 | 14 | int compute_vowel_count(const char *sentence); 15 | void read_sentence(char *sentence, int sent_len); 16 | 17 | int main(void) 18 | { 19 | char sentence[SENT_LEN]; 20 | int num_vowels; 21 | 22 | printf("Enter a sentence: "); 23 | read_sentence(sentence, SENT_LEN); 24 | 25 | num_vowels = compute_vowel_count(sentence); 26 | 27 | printf("Your sentence contains %d vowels\n", num_vowels); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | int compute_vowel_count(const char *sentence) 34 | { 35 | int vowels_count = 0; 36 | 37 | while(*sentence) 38 | { 39 | switch(toupper(*sentence++)) 40 | { 41 | case 'A': case 'E': case 'I': case 'O': case 'U': 42 | vowels_count++; break; 43 | } 44 | } 45 | 46 | return vowels_count; 47 | } 48 | 49 | void read_sentence(char *sentence, int sent_len) 50 | { 51 | int count = 0; 52 | while((*sentence++ = getchar()) != '\n' && count++ < sent_len); 53 | *--sentence = '\0'; 54 | 55 | if(count > sent_len) 56 | while(getchar() != '\n'); 57 | } 58 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_06.c 3 | * 4 | * Created on: Apr 24, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: B1FF Filter 9 | 10 | #include 11 | #include 12 | 13 | #define LEN 50 // Arbitrary for max array length 14 | #define EXCL_COUNT 10 // Arbitrary for max number of exclamation marks 15 | 16 | int main(void) 17 | { 18 | char message[LEN]; 19 | int i, msg_len = 0; 20 | 21 | printf("Enter message: "); 22 | for (i = 0; i < LEN; i++) 23 | { 24 | message[i] = getchar(); 25 | 26 | if (message[i] == '\n') 27 | break; 28 | 29 | msg_len++; 30 | } 31 | 32 | printf("In B1FF-speak: "); 33 | for (i = 0; i < msg_len; i++) 34 | { 35 | switch(message[i]) 36 | { 37 | case 'A': case 'a': putchar('4'); break; 38 | case 'B': case 'b': putchar('8'); break; 39 | case 'E': case 'e': putchar('3'); break; 40 | case 'I': case 'i': putchar('1'); break; 41 | case 'O': case 'o': putchar('0'); break; 42 | case 'S': case 's': putchar('5'); break; 43 | default: putchar(toupper(message[i])); break; 44 | } 45 | } 46 | 47 | for (i = 0; i < EXCL_COUNT; i++) 48 | putchar('!'); 49 | 50 | putchar('\n'); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Ch04_Expressions/ch4_prog_proj_05.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 | // (Section 4.1, Pages 57-58) 11 | 12 | /* 13 | * ch4_prog_proj_05.c 14 | * 15 | * Created on: Apr 1, 2024 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // Programming Project 5: Computing a UPC check digit (Rewritten) 20 | 21 | #include 22 | 23 | int main(void) 24 | { 25 | int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, 26 | first_sum, second_sum, total; 27 | 28 | printf("Enter the first 11 digits of a UPC: "); 29 | scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", 30 | &d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5); 31 | 32 | first_sum = d + i2 + i4 + j1 + j3 + j5; 33 | second_sum = i1 + i3 + i5 + j2 + j4; 34 | total = 3 * first_sum + second_sum; 35 | 36 | printf("Check digit: %d\n", 9 - ((total - 1) % 10)); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_06/planet.c: -------------------------------------------------------------------------------- 1 | /* 2 | * planet.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Checking Planet Names (Improved) 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define NUM_PLANETS 9 16 | 17 | bool same_letters(char *word1, char *word2); 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | char *planets[] = 22 | { 23 | "Mercury", "Venus", "Earth", 24 | "Mars", "Jupiter", "Saturn", 25 | "Uranus", "Neptune", "Pluto" 26 | }; 27 | 28 | 29 | int i, j; 30 | 31 | for(i = 1; i < argc; i++) 32 | { 33 | for(j = 0; j < NUM_PLANETS; j++) 34 | { 35 | if(same_letters(argv[i], planets[j])) 36 | { 37 | printf("%s is planet %d\n", argv[i], j + 1); 38 | break; 39 | } 40 | } 41 | 42 | if(j == NUM_PLANETS) 43 | printf("%s is not a planet\n", argv[i]); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | 50 | bool same_letters(char *word1, char *word2) 51 | { 52 | while(*word1 && *word2) 53 | { 54 | if(toupper(*word1++) != toupper(*word2++)) // Words don't match 55 | return false; 56 | } 57 | 58 | if(*word1 != *word2) // One word is shorter/longer than the other 59 | return false; 60 | 61 | return true; 62 | } 63 | -------------------------------------------------------------------------------- /Ch02_C_Fundamentals/ch2_prog_proj_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch2_prog_proj_08.c 3 | * 4 | * Created on: Mar 13, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Project 8: Remaining balance on a loan 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | float loan_balance, monthly_payment, interest_rate; 15 | float monthly_interest_rate, remaining_balance; 16 | 17 | printf("Enter amount of loan: "); 18 | scanf("%f", &loan_balance); 19 | 20 | printf("Enter interest rate: "); 21 | scanf("%f", &interest_rate); 22 | monthly_interest_rate = interest_rate / (100 * 12); 23 | 24 | printf("Enter monthly payment: "); 25 | scanf("%f", &monthly_payment); 26 | 27 | remaining_balance = loan_balance - monthly_payment + loan_balance * monthly_interest_rate; 28 | 29 | printf("\nBalance remaining after first payment: $%.2f\n", remaining_balance); 30 | 31 | loan_balance = remaining_balance; 32 | remaining_balance = loan_balance - monthly_payment + loan_balance * monthly_interest_rate; 33 | 34 | printf("Balance remaining after second payment: $%.2f\n", remaining_balance); 35 | 36 | loan_balance = remaining_balance; 37 | remaining_balance = loan_balance - monthly_payment + loan_balance * monthly_interest_rate; 38 | 39 | printf("Balance remaining after third payment: $%.2f\n", remaining_balance); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_01.c 3 | * 4 | * Created on: Dec 17, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: Smallest & Largest words 9 | 10 | #include 11 | #include 12 | #define WORD_LEN 20 13 | #define STOP_LEN 4 14 | 15 | void read_word(char *word, int word_len) 16 | { 17 | int count = 0; 18 | 19 | while ((*word++ = getchar()) != '\n' && count++ < word_len); 20 | 21 | *--word = '\0'; 22 | 23 | if (count > word_len) 24 | while (getchar() != '\n'); 25 | } 26 | 27 | int main(void) 28 | { 29 | char input_word[WORD_LEN + 1], smallest_word[WORD_LEN + 1], largest_word[WORD_LEN + 1]; 30 | int i; 31 | 32 | for (i = 0; i < WORD_LEN + 1; i++) 33 | { 34 | smallest_word[i] = 'z'; // largest value possible 35 | largest_word[i] = 'A'; // smallest value possible 36 | } 37 | 38 | do 39 | { 40 | printf("Enter word: "); 41 | read_word(input_word, WORD_LEN); 42 | 43 | if (strcmp(smallest_word, input_word) > 0) 44 | strcpy(smallest_word, input_word); 45 | 46 | if (strcmp(largest_word, input_word) < 0) 47 | strcpy(largest_word, input_word); 48 | 49 | } while(strlen(input_word) != STOP_LEN); 50 | 51 | printf("\nSmallest word: %s\n", smallest_word); 52 | printf("Largest word: %s\n", largest_word); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_06.c 3 | * 4 | * Created on: Aug 5, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 6: File Content Display 10 | 11 | #include 12 | #include 13 | #include 14 | #define OFFSET_VAL 10 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | FILE *fp; 19 | unsigned char bytes[OFFSET_VAL]; 20 | int len, i, byte_offset = 0; 21 | 22 | if (argc != 2) 23 | { 24 | fprintf(stderr, "Usage: ./program filename\n"); 25 | exit(EXIT_FAILURE); 26 | } 27 | 28 | if ((fp = fopen(argv[1], "rb")) == NULL) 29 | { 30 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 31 | exit(EXIT_FAILURE); 32 | } 33 | 34 | printf("Offset%14sBytes%14sCharacters\n", "", ""); 35 | printf("------ ----------------------------- ----------\n"); 36 | 37 | while (!feof(fp)) 38 | { 39 | len = fread(bytes, 1, OFFSET_VAL, fp); 40 | printf("%6d ", byte_offset); 41 | 42 | for (i = 0; i < len; i++) 43 | printf("%02X ", bytes[i]); 44 | 45 | for ( ; i < OFFSET_VAL; i++) 46 | printf(" "); 47 | 48 | putchar(' '); 49 | 50 | for (i = 0; i < len; i++) 51 | printf("%c", isprint(bytes[i]) ? bytes[i] : '.'); 52 | 53 | putchar('\n'); 54 | byte_offset += OFFSET_VAL; 55 | } 56 | 57 | fclose(fp); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_02.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 2: Time conversion 10 | 11 | // Solution 1 (Mine) 12 | #include 13 | 14 | #define AM 1 15 | #define PM 2 16 | 17 | int main(void) 18 | { 19 | int hr_24form, mins, hr_12form, meridiem; 20 | 21 | printf("Enter a 24-hour time: "); 22 | scanf("%d :%d", &hr_24form, &mins); 23 | 24 | // AM Part: 00:00/12:00 AM -> 11:59/11:59 AM 25 | // PM Part: 12:00/12:00 PM -> 23:59/11:59 PM 26 | 27 | if (hr_24form < 12) // AM: 00:00 -> 11:59 28 | { 29 | meridiem = AM; 30 | 31 | // [01]:00->[11]:59 (same as 24-hour), [00]:00->[00]:59 (+12) 32 | hr_12form = (hr_24form) ? hr_24form : 12; 33 | } 34 | else // PM: 12:00 -> 23:59 35 | { 36 | meridiem = PM; 37 | 38 | // [13]:00->[23]:59 (-12), [12]:00->[12]:59 (same as 24-hour) 39 | hr_12form = (hr_24form > 12) ? hr_24form - 12 : 12; 40 | } 41 | 42 | printf("Equivalent 12-hour time: %d:%.2d ", hr_12form, mins); 43 | 44 | switch(meridiem) 45 | { 46 | case AM: printf("AM\n"); break; 47 | case PM: printf("PM\n"); break; 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | // Solution 2 (Author) 54 | // http://knking.com/books/c2/answers/c5.html 55 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_02/stackADT.c: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT.c 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Stack ADT (Fixed-length array representation) 9 | 10 | #include 11 | #include 12 | #include "stackADT.h" 13 | 14 | #define STACK_SIZE 100 15 | 16 | struct stack_type 17 | { 18 | Item contents[STACK_SIZE]; 19 | int top; 20 | }; 21 | 22 | static void terminate(const char *message) 23 | { 24 | printf("%s\n", message); 25 | exit(EXIT_FAILURE); 26 | } 27 | 28 | Stack create(void) 29 | { 30 | Stack s = malloc(sizeof(struct stack_type)); 31 | 32 | if (s == NULL) 33 | terminate("Error in create: stack could not be created."); 34 | 35 | s->top = 0; 36 | 37 | return s; 38 | } 39 | 40 | void destroy(Stack s) 41 | { 42 | free(s); 43 | } 44 | 45 | void make_empty(Stack s) 46 | { 47 | s->top = 0; 48 | } 49 | 50 | bool is_empty(Stack s) 51 | { 52 | return s->top == 0; 53 | } 54 | 55 | bool is_full(Stack s) 56 | { 57 | return s->top == STACK_SIZE; 58 | } 59 | 60 | void push(Stack s, Item i) 61 | { 62 | if (is_full(s)) 63 | terminate("Expression is too complex"); 64 | 65 | s->contents[s->top++] = i; 66 | } 67 | 68 | Item pop(Stack s) 69 | { 70 | if (is_empty(s)) 71 | terminate("Not enough operands in expression"); 72 | 73 | return s->contents[--s->top]; 74 | } 75 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_05.c 3 | * 4 | * Created on: Jul 30, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 5: XOR Encryption (modified) 10 | 11 | #include 12 | #include 13 | #define KEY '&' 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | FILE *in_fp, *out_fp; 18 | int orig_char; 19 | 20 | if (argc != 3) 21 | { 22 | fprintf(stderr, "Usage: xor orig_file new_file\n"); 23 | exit(EXIT_FAILURE); 24 | } 25 | 26 | if ((in_fp = fopen(argv[1], "rb")) == NULL) 27 | { 28 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 29 | exit(EXIT_FAILURE); 30 | } 31 | 32 | if ((out_fp = fopen(argv[2], "wb")) == NULL) 33 | { 34 | fprintf(stderr, "%s cannot be opened\n", argv[2]); 35 | fclose(in_fp); 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | while ((orig_char = getc(in_fp)) != EOF) 40 | putc(orig_char ^ KEY, out_fp); 41 | 42 | fclose(out_fp); 43 | return 0; 44 | } 45 | 46 | /* 47 | * Additional Hints: 48 | * How to verify your program? 49 | * Create msg file 50 | * run the program: ./xor msg newmsg 51 | * run again: ./xor newmsg origmsg 52 | * Check that msh and origmsg are the same. How? 53 | * Run this command in your terminal: diff msg origmsg 54 | * Windows guys: Use a terminal like cygwin to run the command 55 | * 56 | */ 57 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_07.c 3 | * 4 | * Created on: Apr 21, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 7: Adding Fractions (modified) 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int num1, denom1, num2, denom2, result_num, result_denom; 15 | char operator; 16 | 17 | printf("Enter two fractions separated by an operator(+, -, *, or /): "); 18 | scanf("%d /%d %c%d /%d", &num1, &denom1, &operator, &num2, &denom2); 19 | 20 | // Note: "%d /%d %c%d /%d" is better than "%d/%d%c%d/%d" 21 | // as we are allowed to insert spaces before and after 22 | // each character like in ch3/ex6 23 | 24 | switch (operator) 25 | { 26 | case '+': 27 | result_num = num1 * denom2 + num2 * denom1; 28 | result_denom = denom1 * denom2; 29 | break; 30 | case '-': 31 | result_num = num1 * denom2 - num2 * denom1; 32 | result_denom = denom1 * denom2; 33 | break; 34 | case '*': 35 | result_num = num1 * num2; 36 | result_denom = denom1 * denom2; 37 | break; 38 | case '/': 39 | result_num = num1 * denom2; 40 | result_denom = denom1 * num2; 41 | break; 42 | default: printf("Invalid operator!\n"); return 0; 43 | } 44 | 45 | printf("The result is %d/%d\n", result_num, result_denom); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_01/stackADT.c: -------------------------------------------------------------------------------- 1 | /* 2 | * stackADT.c 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Stack ADT (Fixed-length array representation) 9 | 10 | #include 11 | #include 12 | #include "stackADT.h" 13 | 14 | #define STACK_SIZE 100 15 | 16 | struct stack_type 17 | { 18 | Item contents[STACK_SIZE]; 19 | int top; 20 | }; 21 | 22 | static void terminate(const char *message) 23 | { 24 | printf("%s\n", message); 25 | exit(EXIT_FAILURE); 26 | } 27 | 28 | Stack create(void) 29 | { 30 | Stack s = malloc(sizeof(struct stack_type)); 31 | 32 | if (s == NULL) 33 | terminate("Error in create: stack could not be created."); 34 | 35 | s->top = 0; 36 | 37 | return s; 38 | } 39 | 40 | void destroy(Stack s) 41 | { 42 | free(s); 43 | } 44 | 45 | void make_empty(Stack s) 46 | { 47 | s->top = 0; 48 | } 49 | 50 | bool is_empty(Stack s) 51 | { 52 | return s->top == 0; 53 | } 54 | 55 | bool is_full(Stack s) 56 | { 57 | return s->top == STACK_SIZE; 58 | } 59 | 60 | void push(Stack s, Item i) 61 | { 62 | if (is_full(s)) 63 | terminate("Stack overflow"); 64 | 65 | s->contents[s->top++] = i; 66 | } 67 | 68 | Item pop(Stack s) 69 | { 70 | if (is_empty(s)) 71 | terminate("Stack underflow\nParentheses/braces are not matched"); 72 | 73 | return s->contents[--s->top]; 74 | } 75 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_04/remind.c: -------------------------------------------------------------------------------- 1 | /* 2 | * remind.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: One-month remainder list 9 | 10 | #include 11 | #include 12 | #include "readline.h" 13 | #define MAX_REMIND 50 // maximum number of reminders 14 | #define MSG_LEN 60 // max length of reminder message 15 | 16 | int main(void) 17 | { 18 | char reminders[MAX_REMIND][MSG_LEN + 3]; 19 | char day_str[3], msg_str[MSG_LEN + 1]; 20 | int day, i, j, num_remind = 0; 21 | 22 | for(;;) 23 | { 24 | if(num_remind == MAX_REMIND) 25 | { 26 | printf("-- No space left --\n"); 27 | break; 28 | } 29 | 30 | printf("Enter day and reminder: "); 31 | scanf("%2d", &day); 32 | 33 | if(day == 0) 34 | { 35 | break; 36 | } 37 | 38 | sprintf(day_str, "%2d", day); 39 | read_line(msg_str, MSG_LEN); 40 | 41 | for(i = 0; i < num_remind; i++) 42 | { 43 | if(strcmp(day_str, reminders[i]) < 0) 44 | break; 45 | } 46 | 47 | for(j = num_remind; j > i; j--) 48 | strcpy(reminders[j], reminders[j - 1]); 49 | 50 | strcpy(reminders[i], day_str); 51 | strcat(reminders[i], msg_str); 52 | num_remind++; 53 | } 54 | 55 | printf("\nDay Reminder\n"); 56 | for(i = 0; i < num_remind; i++) 57 | printf(" %s\n", reminders[i]); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_02/rpn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rpn.c 3 | * 4 | * Created on: May 11, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: RPN expressions 9 | 10 | #include 11 | #include 12 | #include 13 | #include "stackADT.h" 14 | 15 | int main(void) 16 | { 17 | char ch; 18 | int op1 = 0, op2 = 0, result = 0; 19 | Stack s = create(); 20 | 21 | printf("Enter an RPN expression: "); 22 | 23 | while (1) 24 | { 25 | scanf(" %c", &ch); 26 | 27 | if (ch >= '0' && ch <= '9') 28 | push(s, ch - '0'); 29 | else if (ch == '+') 30 | { 31 | op2 = pop(s); 32 | op1 = pop(s); 33 | result = op1 + op2; 34 | push(s, result); 35 | } 36 | else if (ch == '-') 37 | { 38 | op2 = pop(s); 39 | op1 = pop(s); 40 | result = op1 - op2; 41 | push(s, result); 42 | } 43 | else if (ch == '*') 44 | { 45 | op2 = pop(s); 46 | op1 = pop(s); 47 | result = op1 * op2; 48 | push(s, result); 49 | } 50 | else if (ch == '/') 51 | { 52 | op2 = pop(s); 53 | op1 = pop(s); 54 | result = op1 / op2; 55 | push(s, result); 56 | } 57 | else if (ch == '=') 58 | { 59 | result = pop(s); 60 | printf("Value of expression: %d\n", result); 61 | result = 0; 62 | printf("Enter an RPN expression: "); 63 | } 64 | else 65 | break; 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_04.c 3 | * 4 | * Created on: Oct 25, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 4: Anagrams 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | void read_word(int counts[26]); 15 | bool equal_array(int counts1[26], int counts2[26]); 16 | 17 | 18 | int main(void) 19 | { 20 | // Arrays to detect anograms 21 | int word1_count[26] = {0}, word2_count[26] = {0}; 22 | 23 | // Reading the first word 24 | printf("Enter first word: "); 25 | read_word(word1_count); 26 | 27 | // Reading the second word 28 | printf("Enter second word: "); 29 | read_word(word2_count); 30 | 31 | // Anagrams test 32 | if(equal_array(word1_count, word2_count)) 33 | { 34 | printf("The words are anagrams"); 35 | } 36 | else 37 | { 38 | printf("The words are not anagrams"); 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | void read_word(int counts[26]) 45 | { 46 | // Input char 47 | char c; 48 | 49 | do 50 | { 51 | c = getchar(); 52 | 53 | if(isalpha(c)) 54 | { 55 | c = tolower(c); 56 | counts[c - 'a']++; 57 | } 58 | 59 | }while(c != '\n'); 60 | } 61 | 62 | bool equal_array(int counts1[26], int counts2[26]) 63 | { 64 | for(int i = 0; i < 26; i++) 65 | { 66 | if(counts1[i] != counts2[i]) 67 | return false; 68 | } 69 | 70 | return true; 71 | } 72 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/line.c: -------------------------------------------------------------------------------- 1 | /* 2 | * line.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "line.h" 11 | 12 | #define MAX_LINE_LEN 60 13 | 14 | char line[MAX_LINE_LEN + 1]; 15 | int line_len = 0; 16 | int num_words = 0; 17 | 18 | void clear_line(void) 19 | { 20 | line[0] = '\0'; 21 | line_len = 0; 22 | num_words = 0; 23 | } 24 | 25 | void add_word(const char *word) 26 | { 27 | if(num_words > 0) 28 | { 29 | line[line_len] = ' '; 30 | line[line_len + 1] = '\0'; 31 | line_len++; 32 | } 33 | 34 | strcat(line, word); 35 | line_len += strlen(word); 36 | num_words++; 37 | } 38 | 39 | int space_remaining(void) 40 | { 41 | return MAX_LINE_LEN - line_len; 42 | } 43 | 44 | void write_line(void) 45 | { 46 | int extra_spaces, spaces_to_insert, i, j; 47 | 48 | extra_spaces = MAX_LINE_LEN - line_len; 49 | 50 | for(i = 0; i < line_len; i++) 51 | { 52 | if(line[i] != ' ') 53 | { 54 | putchar(line[i]); 55 | } 56 | else 57 | { 58 | spaces_to_insert = extra_spaces / (num_words - 1); 59 | 60 | for(j = 1; j <= spaces_to_insert + 1; j++) 61 | putchar(' '); 62 | 63 | extra_spaces -= spaces_to_insert; 64 | num_words--; 65 | } 66 | } 67 | putchar('\n'); 68 | } 69 | 70 | 71 | void flush_line(void) 72 | { 73 | if(line_len > 0) 74 | puts(line); 75 | } 76 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_11.c 3 | * 4 | * Created on: Apr 25, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 11: Alphabetic phone number (modified) 9 | 10 | #include 11 | #define MAX_LEN 15 12 | 13 | int main(void) 14 | { 15 | char ch, phone_numeric_form[MAX_LEN]; 16 | int i, len; 17 | 18 | printf("Enter phone number: "); 19 | 20 | for (i = 0; i < MAX_LEN; i++) 21 | { 22 | ch = getchar(); 23 | 24 | if (ch == '\n') 25 | break; 26 | 27 | switch (ch) 28 | { 29 | case 'A': case 'B': case 'C': phone_numeric_form[i] = '2'; break; 30 | case 'D': case 'E': case 'F': phone_numeric_form[i] = '3'; break; 31 | case 'G': case 'H': case 'I': phone_numeric_form[i] = '4'; break; 32 | case 'J': case 'K': case 'L': phone_numeric_form[i] = '5'; break; 33 | case 'M': case 'N': case 'O': phone_numeric_form[i] = '6'; break; 34 | case 'P': case 'R': case 'S': phone_numeric_form[i] = '7'; break; 35 | case 'T': case 'U': case 'V': phone_numeric_form[i] = '8'; break; 36 | case 'W': case 'X': case 'Y': phone_numeric_form[i] = '9'; break; 37 | default: phone_numeric_form[i] = ch; break; 38 | } 39 | } 40 | 41 | len = i; 42 | 43 | printf("In numeric form: "); 44 | for (i = 0; i < len; i++) 45 | putchar(phone_numeric_form[i]); 46 | 47 | putchar('\n'); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_06.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Quicksort algorithm 9 | 10 | #include 11 | #define N 10 12 | 13 | void quicksort(int a[], int *low, int *high); 14 | int *split(int a[], int *low, int *high); 15 | 16 | int main(void) 17 | { 18 | int a[N], i; 19 | 20 | printf("Enter %d numbers to be stored: ", N); 21 | 22 | for(i = 0; i < N; i++) 23 | scanf("%d", &a[i]); 24 | 25 | quicksort(a, &a[0], &a[N - 1]); 26 | 27 | printf("In sorted order: "); 28 | for(i = 0; i < N; i++) 29 | printf("%d ", a[i]); 30 | 31 | printf("\n"); 32 | 33 | return 0; 34 | } 35 | 36 | void quicksort(int a[], int *low, int *high) 37 | { 38 | int *middle; 39 | 40 | if(low >= high) 41 | return; 42 | 43 | middle = split(a, low, high); 44 | quicksort(a, low, middle - 1); 45 | quicksort(a, middle + 1, high); 46 | } 47 | 48 | int *split(int a[], int* low, int* high) 49 | { 50 | int part_element = *low; 51 | 52 | for(;;) 53 | { 54 | while(low < high && part_element <= *high) 55 | high--; 56 | 57 | if(low >= high) 58 | break; 59 | 60 | *low++ = *high; 61 | 62 | while(low < high && *low <= part_element) 63 | low++; 64 | 65 | if(low >= high) 66 | break; 67 | 68 | *high-- = *low; 69 | } 70 | 71 | *high = part_element; 72 | return high; 73 | } 74 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_16.c 3 | * 4 | * Created on: Oct 23, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 16: Anagrams 10 | 11 | #include 12 | #include 13 | 14 | int main(void) 15 | { 16 | // Counters to compute the length of each word 17 | int word_len = 0; 18 | 19 | // Input char 20 | char c; 21 | 22 | // Array to detect anagrams 23 | int anagrams[26] = {0}; 24 | 25 | // Reading the first word 26 | printf("Enter first word: "); 27 | 28 | do 29 | { 30 | c = getchar(); 31 | 32 | if(isalpha(c)) 33 | { 34 | word_len++; 35 | c = tolower(c); 36 | anagrams[c - 'a']++; 37 | } 38 | 39 | }while(c != '\n'); 40 | 41 | // Reading the second word 42 | printf("Enter second word: "); 43 | 44 | do 45 | { 46 | c = getchar(); 47 | 48 | if(isalpha(c)) 49 | { 50 | word_len--; 51 | c = tolower(c); 52 | anagrams[c - 'a']--; 53 | } 54 | 55 | }while(c != '\n'); 56 | 57 | // Anagrams test 1 58 | if(word_len != 0) 59 | { 60 | printf("The words are not anagrams"); 61 | return 0; 62 | } 63 | 64 | // Anagrams test 2 65 | for(int i = 0; i < 26; i++) 66 | { 67 | if(anagrams[i] != 0) 68 | { 69 | printf("The words are not anagrams"); 70 | return 0; 71 | } 72 | } 73 | 74 | printf("The words are anagrams"); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_04/stackclient.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 | // (Section 19.4, Page 494) 11 | 12 | /* 13 | * stackclient.c 14 | * 15 | * Created on: May 11, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // Programming Project 4: stackclient.c 20 | 21 | #include 22 | #include "stackADT.h" 23 | 24 | int main(void) 25 | { 26 | Stack s1, s2; 27 | char *n; 28 | 29 | s1 = create(); 30 | s2 = create(); 31 | 32 | push(s1, "Hello"); 33 | push(s1, "World"); 34 | 35 | n = pop(s1); 36 | printf("Popped %s from s1\n", n); 37 | push(s2, n); 38 | n = pop(s1); 39 | printf("Popped %s from s1\n", n); 40 | push(s2, n); 41 | 42 | destroy(s1); 43 | 44 | while(!is_empty(s2)) 45 | printf("Popped %s from s2\n", (char *)pop(s2)); 46 | 47 | push(s2, "Hello world"); 48 | make_empty(s2); 49 | 50 | if(is_empty(s2)) 51 | printf("s2 is empty\n"); 52 | else 53 | printf("s2 is not empty\n"); 54 | 55 | destroy(s2); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_05/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 30, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: RPN expressions 9 | 10 | #include 11 | #include "stack.h" 12 | 13 | int main(void) 14 | { 15 | char ch; 16 | int op1 = 0, op2 = 0, result = 0; 17 | 18 | printf("Enter an RPN expression: "); 19 | 20 | while(1) 21 | { 22 | ch = getchar(); 23 | 24 | if(ch == ' ') 25 | { 26 | continue; 27 | } 28 | else if(ch >= '0' && ch <= '9') 29 | { 30 | push(ch - '0'); 31 | } 32 | else if(ch == '+') 33 | { 34 | op2 = pop(); 35 | op1 = pop(); 36 | result = op1 + op2; 37 | push(result); 38 | } 39 | else if(ch == '-') 40 | { 41 | op2 = pop(); 42 | op1 = pop(); 43 | result = op1 - op2; 44 | push(result); 45 | } 46 | else if(ch == '*') 47 | { 48 | op2 = pop(); 49 | op1 = pop(); 50 | result = op1 * op2; 51 | push(result); 52 | } 53 | else if(ch == '/') 54 | { 55 | op2 = pop(); 56 | op1 = pop(); 57 | result = op1 / op2; 58 | push(result); 59 | } 60 | else if(ch == '=') 61 | { 62 | result = pop(); 63 | } 64 | else if(ch == '\n') 65 | { 66 | printf("Value of expression: %d\n", result); 67 | result = 0; 68 | op1 = 0; 69 | op2 = 0; 70 | printf("Enter an RPN expression: "); 71 | fflush(stdout); fflush(stdin); 72 | } 73 | else 74 | { 75 | break; 76 | } 77 | } 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Ch03_Formatted_Input_Output/ch3_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch3_prog_proj_05.c 3 | * 4 | * Created on: Mar 19, 2024 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Magic Square 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10; 15 | int n11, n12, n13, n14, n15, n16; 16 | int sum_r1, sum_r2, sum_r3, sum_r4; 17 | int sum_c1, sum_c2, sum_c3, sum_c4; 18 | int sum_d1, sum_d2; 19 | 20 | printf("Enter the numbers from 1 to 16 in any order:\n"); 21 | 22 | scanf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &n1, &n2, &n3, &n4, &n5, 23 | &n6, &n7, &n8, &n9, &n10, &n11, &n12, &n13, &n14, &n15, &n16); 24 | 25 | printf("%2d %2d %2d %2d\n", n1, n2, n3, n4); 26 | printf("%2d %2d %2d %2d\n", n5, n6, n7, n8); 27 | printf("%2d %2d %2d %2d\n", n9, n10, n11, n12); 28 | printf("%2d %2d %2d %2d\n", n13, n14, n15, n16); 29 | 30 | sum_r1 = n1 + n2 + n3 + n4; 31 | sum_r2 = n5 + n6 + n7 + n8; 32 | sum_r3 = n9 + n10 + n11 + n12; 33 | sum_r4 = n13 + n14 + n15 + n16; 34 | 35 | sum_c1 = n1 + n5 + n9 + n13; 36 | sum_c2 = n2 + n6 + n10 + n14; 37 | sum_c3 = n3 + n7 + n11 + n15; 38 | sum_c4 = n4 + n8 + n12 + n16; 39 | 40 | sum_d1 = n1 + n6 + n11 + n16; 41 | sum_d2 = n4 + n7 + n10 + n13; 42 | 43 | printf("\nRow sums: %d %d %d %d\n", sum_r1, sum_r2, sum_r3, sum_r4); 44 | printf("Column sums: %d %d %d %d\n", sum_c1, sum_c2, sum_c3, sum_c4); 45 | printf("Diagonal sums: %d %d\n", sum_d1, sum_d2); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_05/queue.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.c 3 | * 4 | * Created on: May 10, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Queue type (Fixed-length array representation) 9 | 10 | #include 11 | #include 12 | #include "queue.h" 13 | 14 | #define PUBLIC 15 | #define PRIVATE static 16 | 17 | PRIVATE void terminate(const char *message) 18 | { 19 | printf("%s\n", message); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | PUBLIC void enqueue(Queue *q, int i) 24 | { 25 | if (q->qsize == QUEUE_SIZE) 26 | terminate("Error in enqueue: queue is full."); 27 | 28 | q->buffer[q->rear++] = i; 29 | q->rear %= QUEUE_SIZE; // Keep rear within bounds 30 | q->qsize++; 31 | } 32 | 33 | PUBLIC int dequeue(Queue *q) 34 | { 35 | int i; 36 | 37 | if (is_empty(q)) 38 | terminate("Error in dequeue: queue is empty."); 39 | 40 | i = q->buffer[q->front++]; 41 | q->front %= QUEUE_SIZE; // Keep front within bounds 42 | q->qsize--; 43 | 44 | return i; 45 | } 46 | 47 | PUBLIC int front_item(const Queue *q) 48 | { 49 | if (is_empty(q)) 50 | terminate("Error in front_item: queue is empty."); 51 | 52 | return q->buffer[q->front]; 53 | } 54 | 55 | PUBLIC int rear_item(const Queue *q) 56 | { 57 | if (is_empty(q)) 58 | terminate("Error in rear_item: queue is empty."); 59 | 60 | return (q->rear ? q->buffer[q->rear - 1] : q->buffer[QUEUE_SIZE - 1]); 61 | } 62 | 63 | PUBLIC bool is_empty(const Queue *q) 64 | { 65 | return q->qsize == 0; // or q->front == q->rear; 66 | } 67 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_05.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Reverse words in a sentence (New implementation) 9 | 10 | #include 11 | #define MAX_LEN 100 12 | 13 | int main(void) 14 | { 15 | char sentence[MAX_LEN], input_ch; 16 | char *sent_ptr = sentence, *word_start, *word_end; 17 | 18 | printf("Enter a sentence: "); 19 | while ((input_ch = getchar()) != '\n') 20 | { 21 | if (input_ch == '.' || input_ch == '?' || input_ch == '!') 22 | break; 23 | else 24 | *sent_ptr++ = input_ch; 25 | } 26 | 27 | printf("Reversal of sentence: "); 28 | for (word_start = sent_ptr - 1; word_start > sentence;) 29 | { 30 | // Detect the first character of the word to be printed 31 | while (*(word_start - 1) != ' ' && word_start > sentence) 32 | word_start--; 33 | 34 | // Print the word and make sure we don't go out of: 35 | // 1) sentence bound (the end of the sentence array) 36 | // 2) word bound (space) 37 | for (word_end = word_start; word_end < sent_ptr && *word_end != ' '; word_end++) 38 | putchar(*word_end); 39 | 40 | // Detect the last character in the next word to be printed 41 | while (*(word_start - 1) == ' ' && word_start > sentence) 42 | { 43 | putchar(' '); 44 | word_start--; 45 | } 46 | } 47 | 48 | putchar(input_ch); // Print the terminating character 49 | putchar('\n'); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_11.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 11: Average word length for a sentence 9 | 10 | #include 11 | #include 12 | #define SENT_LEN 99 13 | 14 | double compute_average_word_length(char *sentence); 15 | void read_sentence(char *sentence, int sent_len); 16 | 17 | int main(void) 18 | { 19 | char sentence[SENT_LEN + 1]; 20 | float avg; 21 | 22 | printf("Enter a sentence: "); 23 | read_sentence(sentence, SENT_LEN); 24 | 25 | avg = compute_average_word_length(sentence); 26 | printf("Average word length: %.1f\n", avg); 27 | 28 | return 0; 29 | } 30 | 31 | double compute_average_word_length(char *sentence) 32 | { 33 | int total_len = 0, word_len = 0, word_count = 0; 34 | float avg; 35 | 36 | while (*sentence) 37 | { 38 | if (isspace(*sentence)) 39 | { 40 | total_len += word_len; 41 | word_count++; 42 | word_len = 0; 43 | 44 | while (isspace(*++sentence)); // skip more spaces between words 45 | word_len++; // counting for new word 46 | } 47 | else 48 | word_len++; 49 | 50 | sentence++; 51 | } 52 | 53 | word_count++; 54 | total_len += word_len; 55 | avg = ((float) total_len) / word_count; 56 | 57 | return avg; 58 | } 59 | 60 | void read_sentence(char *sentence, int sent_len) 61 | { 62 | int count = 0; 63 | while ((*sentence++ = getchar()) != '\n' && count++ < sent_len); 64 | *--sentence = '\0'; 65 | 66 | if (count > sent_len) 67 | while (getchar() != '\n'); 68 | } 69 | -------------------------------------------------------------------------------- /Ch06_Loops/ch6_prog_proj_04.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 | // (Section 5.2, Page 81) 11 | 12 | /* 13 | * ch6_prog_proj_04.c 14 | * 15 | * Created on: Mar 12, 2024 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // Programming Project 4: Broker's commission 20 | 21 | #include 22 | 23 | int main(void) 24 | { 25 | float commission, value; 26 | 27 | for(;;) 28 | { 29 | printf("Enter value of trade: "); 30 | scanf("%f", &value); 31 | 32 | if(value <= 0.0f) 33 | break; 34 | 35 | if(value < 2500.00f) 36 | commission = 30.00f + .017f * value; 37 | else if(value < 6250.00f) 38 | commission = 56.00f + .0066f * value; 39 | else if(value < 20000.00f) 40 | commission = 76.00f + .0034f * value; 41 | else if(value < 50000.00f) 42 | commission = 100.00f + .0022f * value; 43 | else if(value < 500000.00f) 44 | commission = 155.00f + .0011f * value; 45 | else 46 | commission = 255.00f + .0009f * value; 47 | 48 | if(commission < 39.00f) 49 | commission = 39.00f; 50 | 51 | printf("Commission: $%.2f\n\n", commission); 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_14.c 3 | * 4 | * Created on: Dec 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 14: Anagrams 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #define WORD_LEN 100 15 | 16 | bool are_anagrams(const char *word1, const char *word2); 17 | 18 | int main(void) 19 | { 20 | char word1[WORD_LEN + 1] = {0}, word2[WORD_LEN + 1] = {0}; 21 | char input_ch, *word_ptr; 22 | 23 | // Reading the first word 24 | printf("Enter first word: "); 25 | word_ptr = word1; 26 | do 27 | { 28 | input_ch = getchar(); 29 | 30 | if (isalpha(input_ch)) 31 | *word_ptr++ = tolower(input_ch); 32 | } while (input_ch != '\n'); 33 | 34 | // Reading the second word 35 | printf("Enter second word: "); 36 | word_ptr = word2; 37 | do 38 | { 39 | input_ch = getchar(); 40 | 41 | if (isalpha(input_ch)) 42 | *word_ptr++ = tolower(input_ch); 43 | 44 | } while(input_ch != '\n'); 45 | 46 | if (are_anagrams(word1, word2)) 47 | printf("The words are anagrams\n"); 48 | else 49 | printf("The words are not anagrams\n"); 50 | 51 | return 0; 52 | } 53 | 54 | bool are_anagrams(const char *word1, const char *word2) 55 | { 56 | int i, anagrams[26] = {0}; 57 | 58 | while (*word1 && *word2) 59 | { 60 | anagrams[*word1++ - 'a']++; 61 | anagrams[*word2++ - 'a']--; 62 | } 63 | 64 | if (*word1 != *word2) 65 | return false; 66 | 67 | for (i = 0; i < 26; i++) 68 | if (anagrams[i]) 69 | return false; 70 | 71 | return true; 72 | } 73 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/line.c: -------------------------------------------------------------------------------- 1 | /* 2 | * line.c 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "line.h" 11 | #include 12 | 13 | #define MAX_LINE_LEN 60 14 | 15 | char line[MAX_LINE_LEN + 1]; 16 | int line_len = 0; 17 | int num_words = 0; 18 | bool alternate_more_gap = true; 19 | 20 | void clear_line(void) 21 | { 22 | line[0] = '\0'; 23 | line_len = 0; 24 | num_words = 0; 25 | } 26 | 27 | void add_word(const char *word) 28 | { 29 | if(num_words > 0) 30 | { 31 | line[line_len] = ' '; 32 | line[line_len + 1] = '\0'; 33 | line_len++; 34 | } 35 | 36 | strcat(line, word); 37 | line_len += strlen(word); 38 | num_words++; 39 | } 40 | 41 | int space_remaining(void) 42 | { 43 | return MAX_LINE_LEN - line_len; 44 | } 45 | 46 | void write_line(void) 47 | { 48 | int extra_spaces, spaces_to_insert, i, j; 49 | 50 | extra_spaces = MAX_LINE_LEN - line_len; 51 | 52 | for(i = 0; i < line_len; i++) 53 | { 54 | if(line[i] != ' ') 55 | { 56 | putchar(line[i]); 57 | } 58 | else 59 | { 60 | spaces_to_insert = extra_spaces / (num_words - 1); 61 | 62 | if(extra_spaces && alternate_more_gap) 63 | { 64 | spaces_to_insert++; 65 | } 66 | alternate_more_gap = !alternate_more_gap; 67 | 68 | for(j = 1; j <= spaces_to_insert + 1; j++) 69 | putchar(' '); 70 | 71 | extra_spaces -= spaces_to_insert; 72 | num_words--; 73 | } 74 | } 75 | putchar('\n'); 76 | } 77 | 78 | 79 | void flush_line(void) 80 | { 81 | if(line_len > 0) 82 | puts(line); 83 | } 84 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_17.c 3 | * 4 | * Created on: Oct 24, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 17: Magic square (New implementation) 9 | 10 | #include 11 | 12 | #define NUM_ROWS 99 13 | #define NUM_COLS 99 14 | 15 | int main(void) 16 | { 17 | int n, row, col, number = 1, square_size; 18 | 19 | int magic_square[NUM_ROWS][NUM_COLS] = {0}; // Compatible with C89/C90 20 | 21 | // Reading the magic number 22 | printf("This program creates a magic square of a specified size\n"); 23 | printf("The size must be an odd number between 1 and 99\n"); 24 | printf("Enter size of magic square: "); 25 | scanf("%d", &n); 26 | 27 | // Alternative way to build the magic square in C99 28 | // but first delete this: int magic_square[NUM_ROWS][NUM_COLS] = {0}; 29 | /* 30 | int magic_square[n][n]; 31 | for(row = 0; row < n; row++) 32 | for(col = 0; col < n; col++) 33 | magic_square[row][col] = 0; 34 | */ 35 | 36 | row = 0, col = n / 2, square_size = n * n; 37 | 38 | magic_square[row][col] = number; 39 | 40 | 41 | for (number = 2; number <= square_size; number++) 42 | { 43 | if (magic_square[(row - 1 + n) % n][(col + 1) % n] == 0) 44 | { 45 | row = (row - 1 + n) % n; 46 | col = (col + 1) % n; 47 | } 48 | else 49 | row++; 50 | 51 | magic_square[row][col] = number; 52 | } 53 | 54 | for (row = 0; row < n; row++) 55 | { 56 | for (col = 0; col < n; col++) 57 | printf("%5d", magic_square[row][col]); 58 | 59 | putchar('\n'); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_03.c 3 | * 4 | * Created on: Apr 13, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 3: Broker's Commission (modified) 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | float commission, value; 16 | float rival_commission, price_per_share; 17 | int shares; 18 | 19 | //printf("Enter value of trade: "); 20 | //scanf("%f", &value); 21 | 22 | printf("Enter the number of shares: "); 23 | scanf("%d", &shares); 24 | 25 | printf("Enter the price per share: "); 26 | scanf("%f", &price_per_share); 27 | 28 | value = shares * price_per_share; 29 | 30 | // Computing original broker's commission 31 | if (value < 2500.00f) 32 | commission = 30.00f + .017f * value; 33 | else if (value < 6250.00f) 34 | commission = 56.00f + .0066f * value; 35 | else if (value < 20000.00f) 36 | commission = 76.00f + .0034f * value; 37 | else if (value < 50000.00f) 38 | commission = 100.00f + .0022f * value; 39 | else if (value < 500000.00f) 40 | commission = 155.00f + .0011f * value; 41 | else 42 | commission = 255.00f + .0009f * value; 43 | 44 | if (commission < 39.00f) 45 | commission = 39.00f; 46 | 47 | // Computing rival broker's commission: 48 | if(shares < 2000) 49 | { 50 | rival_commission = 33.00f + .03f * shares; 51 | } 52 | else 53 | { 54 | rival_commission = 33.00f + .02f * shares; 55 | } 56 | 57 | printf("Original broker's commission: $%.2f\n", commission); 58 | printf("Rival broker's commission: $%.2f\n", rival_commission); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_10.c 3 | * 4 | * Created on: Dec 21, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 10: First name and Last name 9 | 10 | #include 11 | #include 12 | #include 13 | #define NAME_LEN 99 // You will not have to write a bio for that name :D 14 | 15 | void reverse_name(char *name); 16 | void read_name(char *name, int name_len); 17 | 18 | int main(void) 19 | { 20 | char name[NAME_LEN + 1]; 21 | 22 | printf("Enter a first and last name: "); 23 | read_name(name, NAME_LEN); 24 | reverse_name(name); 25 | 26 | printf("%s\n", name); 27 | 28 | return 0; 29 | } 30 | 31 | void reverse_name(char *name) 32 | { 33 | char first_name[4] = ", "; 34 | first_name[2] = *name; 35 | char *last_name = name; 36 | 37 | while (*last_name++ != ' '); // Searching for the first letter in last name 38 | 39 | strcat(strcpy(name, last_name), first_name); 40 | } 41 | 42 | void read_name(char *name, int name_len) 43 | { 44 | int i = 0; 45 | 46 | while (isspace(name[i] = getchar())); // Skipping initial spaces 47 | i++; 48 | 49 | // Read first name 50 | while (i < name_len) 51 | if (isspace(name[i++] = getchar())) 52 | break; 53 | 54 | while (isspace(name[i] = getchar())); // Skipping intermediate spaces 55 | i++; 56 | 57 | // Read last name 58 | while (i < name_len) 59 | if (isspace(name[i++] = getchar())) 60 | break; 61 | 62 | // Detect end of line and skip white-spaces at the end 63 | if (name[--i] != '\n') // last written white-space character 64 | while (getchar() != '\n'); 65 | 66 | name[i] = '\0'; // convert to string 67 | } 68 | -------------------------------------------------------------------------------- /Ch10_Program_Organization/ch10_exercises.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch10_exercises.c 3 | * 4 | * Created on: Nov 2, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Q1 9 | /* 10 | * a) The f function 11 | * Variables : a (external), c (local) 12 | * Parameters: b (local) 13 | * 14 | * b) The g function 15 | * Variables : a (external), d (local) 16 | * Parameters: - 17 | * 18 | * c) The block in which e is declared 19 | * Variables : a (external), d (local to g), e (local) 20 | * Parameters: - 21 | * 22 | * d) The main function 23 | * Variables : a (external), f (local) 24 | * Parameters: - 25 | * 26 | */ 27 | 28 | //---------------------------------- 29 | // Q2 30 | /* 31 | * a) The f function 32 | * Variables : c (external), b (local), d (local) 33 | * Parameters: - 34 | * 35 | * b) The g function 36 | * Variables : b (external), c (local) 37 | * Parameters: a (local) 38 | * 39 | * c) The block in which a and d are declared 40 | * Variables : b (external), c (local to g), a (local), d (local) 41 | * Parameters: - 42 | * 43 | * d) The main function 44 | * Variables : b (external), c (local), d (local) 45 | * Parameters: - 46 | * 47 | */ 48 | 49 | //---------------------------------- 50 | // Q3 51 | 52 | /* 53 | * Given enough memory, there is no limit to the number of blocks 54 | * that can be defined in the main function that hence no limit to the 55 | * number of i's that can be created in these blocks (one per block). 56 | * 57 | * However, it's certain that there can be one external variable & 58 | * one local variable to the main function scope. 59 | * 60 | */ 61 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch16_prog_proj_06.c 3 | * 4 | * Created on: Mar 13, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Comparing Dates 9 | 10 | #include 11 | 12 | struct date { int month, day, year; }; 13 | 14 | int compare_dates(struct date d1, struct date d2); 15 | 16 | int main(void) 17 | { 18 | struct date date1, date2; 19 | int earlier_date = 0; 20 | 21 | printf("Enter first date (mm/dd/yy): "); 22 | scanf(" %d /%d /%d", &date1.month, &date1.day, &date1.year); 23 | 24 | printf("Enter second date (mm/dd/yy): "); 25 | scanf(" %d /%d /%d", &date2.month, &date2.day, &date2.year); 26 | 27 | earlier_date = compare_dates(date1, date2); 28 | 29 | if (earlier_date == -1) 30 | printf("%d/%d/%02d is earlier than %d/%d/%02d\n", 31 | date1.month, date1.day, date1.year, date2.month, date2.day, date2.year); 32 | else if (earlier_date == 1) 33 | printf("%d/%d/%02d is earlier than %d/%d/%02d\n", 34 | date2.month, date2.day, date2.year, date1.month, date1.day, date1.year); 35 | else 36 | printf("Both dates are the same\n"); 37 | 38 | return 0; 39 | } 40 | 41 | 42 | int compare_dates(struct date d1, struct date d2) 43 | { 44 | if (d1.year > d2.year) 45 | return 1; 46 | else if (d1.year < d2.year) 47 | return -1; 48 | else 49 | { 50 | if (d1.month > d2.month) 51 | return 1; 52 | else if (d1.month < d2.month) 53 | return -1; 54 | else 55 | { 56 | if (d1.day > d2.day) 57 | return 1; 58 | else if (d1.day < d2.day) 59 | return -1; 60 | } 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Ch07_Basic_Types/ch7_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch7_prog_proj_05.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Scrabble 9 | 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | char input_ch; 16 | int score = 0; 17 | 18 | printf("Enter a word: "); 19 | 20 | while((input_ch = getchar()) != '\n') 21 | { 22 | switch(toupper(input_ch)) 23 | { 24 | case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R': 25 | case 'S': case 'T': case 'U': score++; break; 26 | 27 | case 'D': case 'G': score += 2; break; 28 | 29 | case 'B': case 'C': case 'M': case 'P': score += 3; break; 30 | 31 | case 'F': case 'H': case 'V': case 'W': case 'Y': score += 4; break; 32 | 33 | case 'K': score += 5; break; 34 | 35 | case 'J': case 'X': score += 8; break; 36 | 37 | case 'Q': case 'Z': score += 10; break; 38 | 39 | } 40 | } 41 | 42 | printf("Scrabble value: %d\n", score); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | // Faster implementation using arrays (to be studied in ch8) 49 | // (Tradeoff between memory and speed) 50 | /* 51 | #include 52 | #include 53 | 54 | int main(void) 55 | { 56 | int face_value[26] = 57 | { 58 | 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 59 | }; 60 | 61 | char input_ch; 62 | int score = 0; 63 | 64 | printf("Enter a word: "); 65 | 66 | while((input_ch = getchar()) != '\n') 67 | { 68 | if(input_ch >= 'A' && input_ch <= 'z') 69 | score += face_value[toupper(input_ch) - 'A']; 70 | } 71 | 72 | printf("Scrabble value: %d\n", score); 73 | 74 | return 0; 75 | } 76 | */ 77 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_03/a/queue1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queue1.c 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "queue.h" 11 | 12 | #define PUBLIC 13 | #define PRIVATE static 14 | #define QUEUE_SIZE 100 15 | 16 | PRIVATE int buffer[QUEUE_SIZE]; // Queue array 17 | PRIVATE int rear = 0; // Position of the first empty slot in the array 18 | PRIVATE int front = 0; // Position of the next item to be removed 19 | PRIVATE int qsize = 0; // Number of items in the queue 20 | 21 | PRIVATE void terminate(const char *message) 22 | { 23 | printf("%s\n", message); 24 | exit(EXIT_FAILURE); 25 | } 26 | 27 | PUBLIC void enqueue(int i) 28 | { 29 | if (qsize == QUEUE_SIZE) 30 | terminate("Error in enqueue: queue is full."); 31 | 32 | buffer[rear++] = i; 33 | rear %= QUEUE_SIZE; // Keep rear within bounds 34 | qsize++; 35 | } 36 | 37 | PUBLIC int dequeue(void) 38 | { 39 | int i; 40 | 41 | if (is_empty()) 42 | terminate("Error in dequeue: queue is empty."); 43 | 44 | i = buffer[front++]; 45 | front %= QUEUE_SIZE; // Keep front within bounds 46 | qsize--; 47 | 48 | return i; 49 | } 50 | 51 | PUBLIC int front_item(void) 52 | { 53 | if (is_empty()) 54 | terminate("Error in front_item: queue is empty."); 55 | 56 | return buffer[front]; 57 | } 58 | 59 | PUBLIC int rear_item(void) 60 | { 61 | if (is_empty()) 62 | terminate("Error in rear_item: queue is empty."); 63 | 64 | return (rear ? buffer[rear - 1] : buffer[QUEUE_SIZE - 1]); 65 | } 66 | 67 | PUBLIC bool is_empty(void) 68 | { 69 | return qsize == 0; // or return front == rear; 70 | } 71 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_01/line.h: -------------------------------------------------------------------------------- 1 | /* 2 | * line.h 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef LINE_H_ 9 | #define LINE_H_ 10 | 11 | /**************************************************************** 12 | * clear_line: Clears the current line. * 13 | ****************************************************************/ 14 | void clear_line(void); 15 | 16 | /**************************************************************** 17 | * add_word: Adds word to the end of the current line. * 18 | * If this is not the first word on the line, * 19 | * puts on space before word. * 20 | ****************************************************************/ 21 | void add_word(const char *word); 22 | 23 | /********************************************************************************* 24 | * space_remaining: Returns the number of characters left in the current line. * 25 | *********************************************************************************/ 26 | int space_remaining(void); 27 | 28 | /********************************************************************************* 29 | * write_line: Writes the current line with justification. * 30 | *********************************************************************************/ 31 | void write_line(void); 32 | 33 | /********************************************************************************* 34 | * flush_line: Writes the current line without justification. * 35 | *********************************************************************************/ 36 | void flush_line(void); 37 | 38 | #endif /* LINE_H_ */ 39 | -------------------------------------------------------------------------------- /Ch15_Writing_Large_Programs/ch15_prog_proj_02/line.h: -------------------------------------------------------------------------------- 1 | /* 2 | * line.h 3 | * 4 | * Created on: Dec 29, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef LINE_H_ 9 | #define LINE_H_ 10 | 11 | /**************************************************************** 12 | * clear_line: Clears the current line. * 13 | ****************************************************************/ 14 | void clear_line(void); 15 | 16 | /**************************************************************** 17 | * add_word: Adds word to the end of the current line. * 18 | * If this is not the first word on the line, * 19 | * puts on space before word. * 20 | ****************************************************************/ 21 | void add_word(const char *word); 22 | 23 | /********************************************************************************* 24 | * space_remaining: Returns the number of characters left in the current line. * 25 | *********************************************************************************/ 26 | int space_remaining(void); 27 | 28 | /********************************************************************************* 29 | * write_line: Writes the current line with justification. * 30 | *********************************************************************************/ 31 | void write_line(void); 32 | 33 | /********************************************************************************* 34 | * flush_line: Writes the current line without justification. * 35 | *********************************************************************************/ 36 | void flush_line(void); 37 | 38 | #endif /* LINE_H_ */ 39 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_04/line.h: -------------------------------------------------------------------------------- 1 | /* 2 | * line.h 3 | * 4 | * Created on: Apr 8, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #ifndef LINE_H_ 9 | #define LINE_H_ 10 | 11 | /**************************************************************** 12 | * clear_line: Clears the current line. * 13 | ****************************************************************/ 14 | void clear_line(void); 15 | 16 | /**************************************************************** 17 | * add_word: Adds word to the end of the current line. * 18 | * If this is not the first word on the line, * 19 | * puts on space before word. * 20 | ****************************************************************/ 21 | void add_word(const char *word); 22 | 23 | /********************************************************************************* 24 | * space_remaining: Returns the number of characters left in the current line. * 25 | *********************************************************************************/ 26 | int space_remaining(void); 27 | 28 | /********************************************************************************* 29 | * write_line: Writes the current line with justification. * 30 | *********************************************************************************/ 31 | void write_line(void); 32 | 33 | /********************************************************************************* 34 | * flush_line: Writes the current line without justification. * 35 | *********************************************************************************/ 36 | void flush_line(void); 37 | 38 | #endif /* LINE_H_ */ 39 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_02.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 2: Capitalize All Letters of a File 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | FILE *fp; 18 | int ch; 19 | 20 | if (argc != 2) 21 | { 22 | fprintf(stderr, "Usage: program filename\n"); 23 | exit(EXIT_FAILURE); 24 | } 25 | 26 | if ((fp = fopen(argv[1], "r")) == NULL) 27 | { 28 | fprintf(stderr, "%s can't be opened\n", argv[1]); 29 | exit(EXIT_FAILURE); 30 | } 31 | 32 | while ((ch = getc(fp)) != EOF) 33 | putc(toupper(ch), stdout); 34 | 35 | fclose(fp); 36 | return 0; 37 | } 38 | 39 | 40 | /* 41 | * Additional Hints: 42 | * 43 | * 1) fprintf(stderr, "Usage: program filename\n"); 44 | * fprintf(stderr, "%s can't be opened\n", argv[1]); 45 | * 46 | * These lines can be printf too without any context of the chosen 47 | * output stream, but I wrote it this way to emphasize good practice 48 | * 49 | * 50 | * 2) putc(toupper(ch), stdout); 51 | * 52 | * You can also use putchar, but I am following the same approach 53 | * as I did when writing fprintf instead of printf 54 | * 55 | * 56 | * 3) You can just write putc(toupper(ch), stdout); 57 | * as the only line in the body of while without testing 58 | * any conditions because if ch is not an alpha character, 59 | * toupper returns ch as is (we will learn this in Ch23), 60 | * but you can figure it on your own for two reasons: 61 | * 62 | * - The author had already explained it in Section 7.3 (P.139) 63 | * - If you use an IDE, it often shows you the function doc 64 | * when you hover over it 65 | * 66 | * 67 | */ 68 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_03.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 3: Files Concatenation 10 | 11 | #include 12 | #include 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | FILE *fp; 17 | int i, ch; 18 | 19 | if (argc < 2) 20 | { 21 | fprintf(stderr, "Usage: fcat filename1 [filenames...]\n"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | for (i = 1; i < argc; i++) 26 | { 27 | if ((fp = fopen(argv[i], "r")) == NULL) 28 | { 29 | fprintf(stderr, "%s can't be opened\n", argv[i]); 30 | continue; 31 | } 32 | 33 | while ((ch = getc(fp)) != EOF) 34 | putc(ch, stdout); 35 | 36 | fclose(fp); 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | 43 | /* 44 | * Additional Hints: 45 | * 46 | * 1) The condition "if (argc < 2)" is just an assumption. 47 | * The question doesn't really enforce how fcat should behave 48 | * 49 | * For example: 50 | * I require it to print the content of at least one file, 51 | * hence the condition (argc < 2). Others may require two 52 | * files at least (argc < 3). 53 | * 54 | * There is a unix/linux tool named cat that does the same thing 55 | * and requires no files at all! 56 | * 57 | * 58 | * 2) The original unix/linux cat tool uses binary streams, 59 | * hence it uses the mode "rb". If you are working on a Unix/Linux 60 | * environment, you will see no difference in outputs. 61 | * But if you are working on Windows, lines end with \n\r, so this 62 | * will cause additional line breaks in the output. 63 | * 64 | * Text mode is the same for both windows and linux, this is why 65 | * I chose this portable solution. Learn the lesson and choose yours. 66 | * 67 | * 68 | */ 69 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_04c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_04c.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 4: Text File Statistics 10 | // (c) Number of Lines 11 | 12 | #define CH22_Q4C_SOL1 13 | //#define CH22_Q4C_SOL2 14 | 15 | #ifdef CH22_Q4C_SOL1 16 | 17 | #include 18 | #include 19 | #define LEN 400 // Arbitrary 20 | 21 | int main(int argc, char *argv[]) 22 | { 23 | FILE *fp; 24 | unsigned int count; 25 | char line[LEN]; 26 | 27 | if (argc != 2) 28 | { 29 | fprintf(stderr, "Usage: ./program filename.txt\n"); 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | if ((fp = fopen(argv[1], "r")) == NULL) 34 | { 35 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | for (count = 0; fgets(line, LEN, fp) != NULL; count++); 40 | 41 | fclose(fp); 42 | fprintf(stdout, "Number of lines = %u\n", count); 43 | 44 | return 0; 45 | } 46 | 47 | #elif defined(CH22_Q4C_SOL2) 48 | 49 | #include 50 | #include 51 | 52 | int main(int argc, char *argv[]) 53 | { 54 | FILE *fp; 55 | unsigned int count; 56 | int ch, prev_ch = EOF; 57 | 58 | if (argc != 2) 59 | { 60 | fprintf(stderr, "Usage: ./program filename.txt\n"); 61 | exit(EXIT_FAILURE); 62 | } 63 | 64 | if ((fp = fopen(argv[1], "r")) == NULL) 65 | { 66 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | for (count = 0; (ch = getc(fp)) != EOF; prev_ch = ch) 71 | if (ch == '\n') 72 | count++; 73 | 74 | if (prev_ch != '\n') // Check for last unterminated line before EOF 75 | count++; 76 | 77 | fclose(fp); 78 | fprintf(stdout, "Number of lines = %u\n", count); 79 | 80 | return 0; 81 | } 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_04b.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_04b.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 4: Text File Statistics 10 | // (b) Number of Words 11 | #define CH22_PP4B_SOL1 12 | //#define CH22_PP4B_SOL2 13 | 14 | #ifdef CH22_PP4B_SOL1 15 | 16 | #include 17 | #include 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | FILE *fp; 22 | unsigned int count; 23 | 24 | if (argc != 2) 25 | { 26 | fprintf(stderr, "Usage: ./program filename.txt\n"); 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | if ((fp = fopen(argv[1], "r")) == NULL) 31 | { 32 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 33 | exit(EXIT_FAILURE); 34 | } 35 | 36 | for (count = 0; fscanf(fp, "%*s") != EOF; count++); 37 | 38 | fclose(fp); 39 | fprintf(stdout, "Number of words = %u\n", count); 40 | 41 | return 0; 42 | } 43 | 44 | #elif defined(CH22_PP4B_SOL2) 45 | 46 | #include 47 | #include 48 | #include 49 | #include 50 | 51 | int main(int argc, char *argv[]) 52 | { 53 | FILE *fp; 54 | unsigned int count = 0; 55 | int ch; 56 | bool word_sequence = false; 57 | 58 | if (argc != 2) 59 | { 60 | fprintf(stderr, "Usage: ./program filename.txt\n"); 61 | exit(EXIT_FAILURE); 62 | } 63 | 64 | if ((fp = fopen(argv[1], "r")) == NULL) 65 | { 66 | fprintf(stderr, "%s cannot be opened\n", argv[1]); 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | while ((ch = getc(fp)) != EOF) 71 | { 72 | if (isspace(ch)) 73 | word_sequence = false; 74 | else if (!word_sequence) 75 | { 76 | word_sequence = true; 77 | count++; 78 | } 79 | } 80 | 81 | fclose(fp); 82 | fprintf(stdout, "Number of words = %u\n", count); 83 | 84 | return 0; 85 | } 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /Ch22_Input_Output/ch22_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch22_prog_proj_01.c 3 | * 4 | * Created on: Jul 27, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | 9 | // Programming Project 1: canopen (modified) 10 | 11 | #include 12 | #include 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | FILE *fp; 17 | int i, num_opened = 0; 18 | 19 | if (argc < 2) 20 | { 21 | printf("usage: canopen filename [filenames...]\n"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | for (i = 1; i < argc; i++) 26 | { 27 | if ((fp = fopen(argv[i], "r")) == NULL) 28 | printf("%s can't be opened\n", argv[i]); 29 | else 30 | { 31 | printf("%s can be opened\n", argv[i]); 32 | num_opened++; 33 | fclose(fp); 34 | } 35 | } 36 | 37 | // Extra 1 for the program name (canopen) 38 | if (argc != num_opened + 1) 39 | exit(EXIT_FAILURE); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | /* 46 | * Additional Hints: 47 | * 48 | * 49 | * 1) printf("usage: canopen filename [filenames...]\n"); 50 | * 51 | * In a more practical sense, this line should be: 52 | * printf("usage: %s filename [filenames...]\n", argv[0]); 53 | * 54 | * This way, the terminal will tell you how to properly invoke your 55 | * program for your specific OS and terminal. 56 | * Examples: canopen, ./canopen, ./canopen.exe, ...etc 57 | * 58 | * 2) filename [filenames...] means there should be at least one 59 | * filename with additional filenames as optional 60 | * You will see these square brackets a lot in the language of 61 | * documentations or linux manual to indicate optional arguments 62 | * 63 | * 64 | * 3) You also use a bool variable like failure_flag that would be set 65 | * if any file can't be opened, then check that flag at the end 66 | * instead of num_opened and make proper adjustments to your program 67 | * accordingly. 68 | * 69 | */ 70 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_02/stack2.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 | // stack2.c (Section 19.2, Page 489) 11 | 12 | /* 13 | * stack2.c 14 | * 15 | * Created on: May 9, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | #include 20 | #include 21 | #include "stack.h" 22 | 23 | #define PUBLIC /* empty */ 24 | #define PRIVATE static 25 | 26 | struct node 27 | { 28 | int data; 29 | struct node *next; 30 | }; 31 | 32 | PRIVATE struct node *top = NULL; 33 | 34 | PRIVATE void terminate(const char *message) 35 | { 36 | printf("%s\n", message); 37 | exit(EXIT_FAILURE); 38 | } 39 | 40 | PUBLIC void make_empty(void) 41 | { 42 | while(!is_empty()) 43 | pop(); 44 | } 45 | 46 | PUBLIC bool is_empty(void) 47 | { 48 | return top == NULL; 49 | } 50 | 51 | PUBLIC bool is_full(void) 52 | { 53 | return false; 54 | } 55 | 56 | PUBLIC void push(int i) 57 | { 58 | struct node *new_node = malloc(sizeof(struct node)); 59 | if(new_node == NULL) 60 | terminate("Error in push: stack is full."); 61 | 62 | new_node->data = i; 63 | new_node->next = top; 64 | top = new_node; 65 | } 66 | 67 | 68 | PUBLIC int pop(void) 69 | { 70 | struct node *old_top; 71 | int i; 72 | 73 | if(is_empty()) 74 | terminate("Error in pop: stack is empty."); 75 | 76 | old_top = top; 77 | i = top->data; 78 | top = top->next; 79 | free(old_top); 80 | 81 | return i; 82 | } 83 | -------------------------------------------------------------------------------- /Ch09_Functions/ch9_prog_proj_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch9_prog_proj_05.c 3 | * 4 | * Created on: Oct 25, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 5: Magic square (New implementation) 9 | 10 | #include 11 | 12 | void create_magic_square(int n, char magic_square[n][n]); 13 | void print_magic_square(int n, char magic_square[n][n]); 14 | 15 | int main(void) 16 | { 17 | int n; 18 | 19 | // Reading the magic number 20 | printf("This program creates a magic square of a specified size\n"); 21 | printf("The size must be an odd number between 1 and 99\n"); 22 | printf("Enter size of magic square: "); 23 | scanf("%d", &n); 24 | 25 | char magic_square[n][n]; 26 | 27 | // Building the magic square 28 | create_magic_square(n, magic_square); 29 | 30 | // Printing the magic square (with correct padding) 31 | print_magic_square(n, magic_square); 32 | 33 | return 0; 34 | } 35 | 36 | void create_magic_square(int n, char magic_square[n][n]) 37 | { 38 | int row, col, number = 1, square_size; 39 | 40 | for(row = 0; row < n; row++) 41 | for(col = 0; col < n; col++) 42 | magic_square[row][col] = 0; 43 | 44 | row = 0, col = n / 2, square_size = n * n; 45 | 46 | magic_square[row][col] = number; 47 | 48 | 49 | for (number = 2; number <= square_size; number++) 50 | { 51 | if (magic_square[(row - 1 + n) % n][(col + 1) % n] == 0) 52 | { 53 | row = (row - 1 + n) % n; 54 | col = (col + 1) % n; 55 | } 56 | else 57 | row++; 58 | 59 | magic_square[row][col] = number; 60 | } 61 | 62 | } 63 | 64 | void print_magic_square(int n, char magic_square[n][n]) 65 | { 66 | int row, col; 67 | 68 | for (row = 0; row < n; row++) 69 | { 70 | for (col = 0; col < n; col++) 71 | printf("%5d", magic_square[row][col]); 72 | 73 | putchar('\n'); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_prog_proj_03/stackclient.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 | // (Section 19.4, Page 494) 11 | 12 | /* 13 | * stackclient.c 14 | * 15 | * Created on: May 11, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // Programming Project 3: stackclient.c 20 | 21 | #include 22 | #include "stackADT.h" 23 | 24 | int main(void) 25 | { 26 | Stack s1, s2; 27 | int n; 28 | 29 | s1 = create(); 30 | s2 = create(); 31 | 32 | push(s1, 1); 33 | printf("Length of s1 = %d\n", length(s1)); 34 | 35 | push(s1, 2); 36 | printf("Length of s1 = %d\n", length(s1)); 37 | 38 | n = pop(s1); 39 | printf("Popped %d from s1\n", n); 40 | printf("Length of s1 = %d\n", length(s1)); 41 | 42 | push(s2, n); 43 | printf("Length of s2 = %d\n", length(s2)); 44 | 45 | n = pop(s1); 46 | printf("Popped %d from s1\n", n); 47 | printf("Length of s1 = %d\n", length(s1)); 48 | 49 | push(s2, n); 50 | printf("Length of s2 = %d\n", length(s2)); 51 | 52 | destroy(s1); 53 | 54 | while(!is_empty(s2)) 55 | { 56 | printf("Popped %d from s2\n", pop(s2)); 57 | printf("Length of s2 = %d\n", length(s2)); 58 | } 59 | 60 | push(s2, 3); 61 | printf("Length of s2 = %d\n", length(s2)); 62 | 63 | make_empty(s2); 64 | printf("Length of s2 = %d\n", length(s2)); 65 | 66 | if(is_empty(s2)) 67 | printf("s2 is empty\n"); 68 | else 69 | printf("s2 is not empty\n"); 70 | 71 | destroy(s2); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_03/b/queue2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.c 3 | * 4 | * Created on: May 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | #include 9 | #include 10 | #include "queue.h" 11 | 12 | #define PUBLIC 13 | #define PRIVATE static 14 | 15 | struct node 16 | { 17 | int data; 18 | struct node *next; 19 | }; 20 | 21 | PRIVATE struct node *rear = NULL; // Position of the last added item 22 | PRIVATE struct node *front = NULL; // Position of the next item to be removed 23 | 24 | PRIVATE void terminate(const char *message) 25 | { 26 | printf("%s\n", message); 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | PUBLIC void enqueue(int i) 31 | { 32 | struct node *new_node = malloc(sizeof(struct node)); 33 | 34 | if (new_node == NULL) 35 | terminate("Error in enqueue: queue is full."); 36 | 37 | if (rear == NULL) // In case queue was empty 38 | front = new_node; 39 | else 40 | rear->next = new_node; 41 | 42 | new_node->data = i; 43 | new_node->next = NULL; 44 | rear = new_node; 45 | } 46 | 47 | PUBLIC int dequeue(void) 48 | { 49 | struct node *old_front; 50 | int i; 51 | 52 | if (is_empty()) 53 | terminate("Error in dequeue: queue is empty."); 54 | 55 | old_front = front; 56 | i = front->data; 57 | front = front->next; 58 | free(old_front); 59 | 60 | // If the queue became empty, rear would be NULL 61 | if (is_empty()) 62 | rear = NULL; 63 | 64 | return i; 65 | } 66 | 67 | PUBLIC int front_item(void) 68 | { 69 | if (is_empty()) 70 | terminate("Error in front_item: queue is empty."); 71 | 72 | return front->data; 73 | } 74 | 75 | PUBLIC int rear_item(void) 76 | { 77 | if (is_empty()) 78 | terminate("Error in rear_item: queue is empty."); 79 | 80 | return rear->data; 81 | } 82 | 83 | PUBLIC bool is_empty(void) 84 | { 85 | return front == NULL; 86 | } 87 | -------------------------------------------------------------------------------- /Ch12_Pointers_and_Arrays/ch12_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch12_prog_proj_02.c 3 | * 4 | * Created on: Dec 5, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: Palindrome 9 | 10 | // (a) 11 | /* 12 | #include 13 | #include 14 | #include 15 | #define LEN 100 16 | 17 | int main(void) 18 | { 19 | char input, message[LEN]; 20 | int left_index = 0, right_index = 0; 21 | bool palindrome = true; 22 | 23 | // Reading the message 24 | printf("Enter a message: "); 25 | while ((input = getchar()) != '\n' && right_index < LEN) 26 | if (isalpha(input)) 27 | message[right_index++] = tolower(input); 28 | 29 | // Go back to the message end 30 | right_index--; 31 | 32 | // Palindrome Detection 33 | while (left_index < right_index) 34 | { 35 | if (message[left_index++] != message[right_index--]) 36 | { 37 | palindrome = false; 38 | break; 39 | } 40 | } 41 | 42 | if (palindrome) 43 | printf("Palindrome"); 44 | else 45 | printf("Not a palindrome"); 46 | 47 | return 0; 48 | } 49 | */ 50 | 51 | // (b) 52 | /* 53 | #include 54 | #include 55 | #include 56 | #define LEN 100 57 | 58 | int main(void) 59 | { 60 | char input, message[LEN], *left_ch = message, *right_ch = message; 61 | bool palindrome = true; 62 | 63 | // Reading the message 64 | printf("Enter a message: "); 65 | while ((input = getchar()) != '\n' && right_ch < message + LEN) 66 | if (isalpha(input)) 67 | *right_ch++ = tolower(input); 68 | 69 | // Go back to the message end 70 | right_ch--; 71 | 72 | // Palindrome Detection 73 | while (left_ch < right_ch) 74 | { 75 | if (*left_ch++ != *right_ch--) 76 | { 77 | palindrome = false; 78 | break; 79 | } 80 | } 81 | 82 | if (palindrome) 83 | printf("Palindrome"); 84 | else 85 | printf("Not a palindrome"); 86 | 87 | return 0; 88 | } 89 | */ 90 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_11.c 3 | * 4 | * Created on: Dec 19, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 11: English word for a two-digit number 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int units, tens, num; 15 | 16 | printf("Enter a two-digit number: "); 17 | scanf("%d", &num); 18 | 19 | tens = num / 10; 20 | units = num % 10; 21 | 22 | printf("You entered the number "); 23 | 24 | if(num == 0) 25 | { 26 | printf("zero"); 27 | } 28 | else if(num >= 10 && num <= 19) 29 | { 30 | switch(num) 31 | { 32 | case 10: printf("ten"); break; 33 | case 11: printf("eleven"); break; 34 | case 12: printf("twelve"); break; 35 | case 13: printf("thirteen"); break; 36 | case 14: printf("fourteen"); break; 37 | case 15: printf("fifteen"); break; 38 | case 16: printf("sixteen"); break; 39 | case 17: printf("seventeen"); break; 40 | case 18: printf("eighteen"); break; 41 | case 19: printf("nineteen"); break; 42 | } 43 | } 44 | else 45 | { 46 | switch(tens) 47 | { 48 | case 2: printf("twenty"); break; 49 | case 3: printf("thirty"); break; 50 | case 4: printf("forty"); break; 51 | case 5: printf("fifty"); break; 52 | case 6: printf("sixty"); break; 53 | case 7: printf("seventy"); break; 54 | case 8: printf("eighty"); break; 55 | case 9: printf("ninety"); break; 56 | } 57 | 58 | //if(tens && units) 59 | //printf("-"); 60 | 61 | switch(units) 62 | { 63 | case 1: printf("-one"); break; 64 | case 2: printf("-two"); break; 65 | case 3: printf("-three"); break; 66 | case 4: printf("-four"); break; 67 | case 5: printf("-five"); break; 68 | case 6: printf("-six"); break; 69 | case 7: printf("-seven"); break; 70 | case 8: printf("-eight"); break; 71 | case 9: printf("-nine"); break; 72 | } 73 | } 74 | 75 | printf(".\n"); 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Ch13_Strings/ch13_prog_proj_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch13_prog_proj_02.c 3 | * 4 | * Created on: Dec 17, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 2: One-month remainder list 9 | 10 | #include 11 | #include 12 | #define MAX_REMIND 50 // maximum number of reminders 13 | #define MSG_LEN 60 // max length of reminder message 14 | 15 | int read_line(char str[], int n); 16 | 17 | int main(void) 18 | { 19 | char reminders[MAX_REMIND][MSG_LEN + 8]; 20 | char msg_str[MSG_LEN + 1], time_str[12]; // mm/dd hh:mm\0 21 | int month, day, i, j, num_remind = 0, hh, mm; 22 | int month_days[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 23 | 24 | for (;;) 25 | { 26 | if (num_remind == MAX_REMIND) 27 | { 28 | printf("-- No space left --\n"); 29 | break; 30 | } 31 | 32 | printf("Enter day, 24-hour time, and reminder: "); 33 | scanf("%2d/%2d", &month, &day); 34 | 35 | if (day == 0 || month == 0) 36 | break; 37 | else if (day < 0 || month < 0 || month > 12 || day > month_days[month - 1]) 38 | { 39 | printf("Invalid reminder (enter a valid day number)\n"); 40 | while (getchar() != '\n'); 41 | continue; 42 | } 43 | 44 | scanf("%2d:%2d", &hh, &mm); 45 | 46 | sprintf(time_str, "%02d/%02d %02d:%02d", month, day, hh, mm); 47 | read_line(msg_str, MSG_LEN); 48 | 49 | for (i = 0; i < num_remind; i++) 50 | if (strcmp(time_str, reminders[i]) < 0) 51 | break; 52 | 53 | for (j = num_remind; j > i; j--) 54 | strcpy(reminders[j], reminders[j - 1]); 55 | 56 | strcpy(reminders[i], time_str); 57 | strcat(reminders[i], msg_str); 58 | num_remind++; 59 | } 60 | 61 | printf("\nDate Time Reminder\n"); 62 | for (i = 0; i < num_remind; i++) 63 | printf("%s\n", reminders[i]); 64 | 65 | return 0; 66 | } 67 | 68 | int read_line(char str[], int n) 69 | { 70 | int ch, i = 0; 71 | 72 | while ((ch = getchar()) != '\n') 73 | if (i < n) 74 | str[i++] = ch; 75 | 76 | str[i] = '\0'; 77 | 78 | return i; 79 | } 80 | -------------------------------------------------------------------------------- /Ch19_Program_Design/ch19_exercise_06/a/stackADT.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 | // (Section 19.4, Page 495) 11 | 12 | /* 13 | * stackADT.c 14 | * 15 | * Created on: May 10, 2020 16 | * Author: Mahmoud Hamdy 17 | */ 18 | 19 | // a) Stack ADT (Fixed-length array representation) 20 | 21 | #include 22 | #include 23 | #include "stackADT.h" 24 | 25 | #define STACK_SIZE 100 26 | 27 | struct stack_type 28 | { 29 | Item contents[STACK_SIZE]; 30 | int top; 31 | }; 32 | 33 | static void terminate(const char *message) 34 | { 35 | printf("%s\n", message); 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | Stack create(void) 40 | { 41 | Stack s = malloc(sizeof(struct stack_type)); 42 | 43 | if(s == NULL) 44 | terminate("Error in create: stack could not be created."); 45 | 46 | s->top = 0; 47 | 48 | return s; 49 | } 50 | 51 | void destroy(Stack s) 52 | { 53 | free(s); 54 | } 55 | 56 | void make_empty(Stack s) 57 | { 58 | s->top = 0; 59 | } 60 | 61 | bool is_empty(Stack s) 62 | { 63 | return s->top == 0; 64 | } 65 | 66 | bool is_full(Stack s) 67 | { 68 | return s->top == STACK_SIZE; 69 | } 70 | 71 | void push(Stack s, Item i) 72 | { 73 | if(is_full(s)) 74 | terminate("Error in push: stack is full."); 75 | 76 | s->contents[s->top++] = i; 77 | } 78 | 79 | Item pop(Stack s) 80 | { 81 | if(is_empty(s)) 82 | terminate("Error in pop: stack is empty."); 83 | 84 | return s->contents[--s->top]; 85 | } 86 | 87 | Item peek(Stack s) 88 | { 89 | if(is_empty(s)) 90 | terminate("Error in peek: stack is empty."); 91 | 92 | return s->contents[s->top - 1]; 93 | } 94 | -------------------------------------------------------------------------------- /Ch05_Selection_Statements/ch5_prog_proj_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch5_prog_proj_08.c 3 | * 4 | * Created on: Nov 22, 2019 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 8: Daily Flights 9 | 10 | #include 11 | 12 | int main(void) 13 | { 14 | int hr_24form, mins, total_desired_time; 15 | 16 | // 8:00 AM, 9:43 AM 17 | int dep_time1 = 480, dep_time2 = 583; 18 | 19 | // 11:19 AM, 12:47 PM 20 | int dep_time3 = 679, dep_time4 = 767; 21 | 22 | // 2:00 PM, 3:45 PM 23 | int dep_time5 = 840, dep_time6 = 945; 24 | 25 | // 7:00 PM, 9:45 PM 26 | int dep_time7 = 1140, dep_time8 = 1305; 27 | 28 | printf("Enter a 24-hour time: "); 29 | scanf("%d :%d", &hr_24form, &mins); 30 | 31 | total_desired_time = hr_24form * 60 + mins; 32 | 33 | printf("Closest departure time is "); 34 | 35 | // Finding the closest departure time between each pair 36 | if (total_desired_time <= (dep_time1 + (dep_time2 - dep_time1) / 2)) 37 | { 38 | printf("8:00 a.m., arriving at 10:16 a.m.\n"); 39 | } 40 | else if (total_desired_time <= (dep_time2 + (dep_time3 - dep_time2) / 2)) 41 | { 42 | printf("9:43 a.m., arriving at 11:52 a.m.\n"); 43 | } 44 | else if (total_desired_time <= (dep_time3 + (dep_time4 - dep_time3) / 2)) 45 | { 46 | printf("11:19 a.m., arriving at 1:31 p.m.\n"); 47 | } 48 | else if (total_desired_time <= (dep_time4 + (dep_time5 - dep_time4) / 2)) 49 | { 50 | printf("12:47 p.m., arriving at 3:00 p.m.\n"); 51 | } 52 | else if (total_desired_time <= (dep_time5 + (dep_time6 - dep_time5) / 2)) 53 | { 54 | printf("2:00 p.m., arriving at 4:08 p.m.\n"); 55 | } 56 | else if (total_desired_time <= (dep_time6 + (dep_time7 - dep_time6) / 2)) 57 | { 58 | printf("3:45 p.m., arriving at 5:55 p.m.\n"); 59 | } 60 | else if (total_desired_time <= (dep_time7 + (dep_time8 - dep_time7) / 2)) 61 | { 62 | printf("7:00 p.m., arriving at 9:20 p.m.\n"); 63 | } 64 | else 65 | { 66 | printf("9:45 p.m., arriving at 11:58 p.m.\n"); 67 | } 68 | 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Ch08_Arrays/ch8_prog_proj_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch8_prog_proj_10.c 3 | * 4 | * Created on: Apr 25, 2025 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 10: Daily Flights (modified) 9 | 10 | #include 11 | 12 | #define NUM_TIMES 8 13 | 14 | int main(void) 15 | { 16 | int hr_24form, mins, total_desired_time, i; 17 | 18 | // 8:00 AM, 9:43 AM, 11:19 AM, 12:47 PM, 2:00 PM, 3:45 PM, 7:00 PM, 9:45 PM 19 | int dep_time[NUM_TIMES] = {480, 583, 679, 767, 840, 945, 1140, 1305}; 20 | 21 | // 10:16 AM, 11:52 AM, 1:31 PM, 3:00 PM, 4:08 PM, 5:55 PM, 9:20 PM, 11:58 PM 22 | int arv_time[NUM_TIMES] = {616, 712, 811, 900, 968, 1075, 1280, 1438}; 23 | 24 | int dep_hr, dep_min, arv_hr, arv_min; 25 | char dep_meridiem, arv_meridiem; 26 | 27 | printf("Enter a 24-hour time: "); 28 | scanf("%d :%d", &hr_24form, &mins); 29 | 30 | total_desired_time = hr_24form * 60 + mins; 31 | 32 | printf("Closest departure time is "); 33 | 34 | // Finding the closest departure time between each pair 35 | for (i = 0; i < NUM_TIMES - 1; i++) 36 | { 37 | if (total_desired_time <= (dep_time[i] + (dep_time[i + 1] - dep_time[i]) / 2)) 38 | break; 39 | } 40 | 41 | dep_min = dep_time[i] % 60; 42 | dep_hr = dep_time[i] / 60; 43 | 44 | if (dep_hr < 12 || dep_hr == 24) 45 | { 46 | dep_hr = (dep_hr == 24) ? 12 : dep_hr; 47 | dep_meridiem = 'a'; 48 | } 49 | else // dep_hr >= 12 and < 24 50 | { 51 | dep_hr = (dep_hr == 12) ? dep_hr : dep_hr - 12; 52 | dep_meridiem = 'p'; 53 | } 54 | 55 | arv_min = arv_time[i] % 60; 56 | arv_hr = arv_time[i] / 60; 57 | 58 | if (arv_hr < 12 || arv_hr == 24) 59 | { 60 | arv_hr = (arv_hr == 24) ? 12 : arv_hr; 61 | arv_meridiem = 'a'; 62 | } 63 | else // arv_hr >= 12 and < 24 64 | { 65 | arv_hr = (arv_hr == 12) ? arv_hr : arv_hr - 12; 66 | arv_meridiem = 'p'; 67 | } 68 | 69 | 70 | printf("%d:%.2d %c.m., arriving at %d:%.2d %c.m.\n", 71 | dep_hr, dep_min, dep_meridiem, arv_hr, arv_min, arv_meridiem); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Ch16_Structures_Unions_and_Enumerations/ch16_prog_proj_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch16_prog_proj_01.c 3 | * 4 | * Created on: Mar 12, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 1: International telephone calls 9 | 10 | #include 11 | #define NUM_CODES 36 12 | 13 | struct dialing_code 14 | { 15 | char *country; 16 | int code; 17 | }; 18 | 19 | const struct dialing_code country_codes[NUM_CODES] = 20 | { 21 | {"Argentina", 54}, {"Bangladesh", 889}, 22 | {"Brazil", 55}, {"Burma (Myanmar)", 95}, 23 | {"China", 86}, {"Colombia", 57}, 24 | {"Congo, Dem. Rep. of", 243}, {"Egypt", 20}, 25 | {"Ethiopia", 251}, {"France", 33}, 26 | {"Germany", 49}, {"India", 91}, 27 | {"Indonesia", 62}, {"Iran", 98}, 28 | {"Italy", 39}, {"Japan", 81}, 29 | {"Mexico", 52}, {"Nigeria", 234}, 30 | {"Pakistan", 92}, {"Philippines", 63}, 31 | {"Poland", 48}, {"Russia", 7}, 32 | {"South Africa", 27}, {"South Korea", 82}, 33 | {"Spain", 34}, {"Sudan", 249}, 34 | {"Thailand", 66}, {"Turkey", 90}, 35 | {"Ukraine", 380}, {"United Kingdom", 44}, 36 | {"United States", 1}, {"Vietnam", 84} 37 | }; 38 | 39 | int lookup_code(int code); 40 | 41 | int main(void) 42 | { 43 | int code, index; 44 | 45 | printf("Please, enter an international dialing code: "); 46 | scanf("%d", &code); 47 | 48 | index = lookup_code(code); 49 | 50 | if(index == -1) 51 | { 52 | printf("Invalid code!\n"); 53 | } 54 | else 55 | { 56 | printf("Corresponding country: %s\n", country_codes[index].country); 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | int lookup_code(int code) 63 | { 64 | for(int i = 0; i < NUM_CODES; i++) 65 | { 66 | if(country_codes[i].code == code) 67 | return i; 68 | } 69 | 70 | return -1; 71 | } 72 | -------------------------------------------------------------------------------- /Ch17_Advanced_Uses_of_Pointers/ch17_prog_proj_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ch17_prog_proj_06.c 3 | * 4 | * Created on: Apr 9, 2020 5 | * Author: Mahmoud Hamdy 6 | */ 7 | 8 | // Programming Project 6: Sort Words 9 | 10 | #include 11 | #include 12 | #include 13 | #define MAX_WORD_LEN 20 14 | 15 | int read_line(char str[], int n); 16 | int compare_words(const void *word1, const void *word2); 17 | 18 | int main(void) 19 | { 20 | char word[MAX_WORD_LEN + 1]; 21 | int i, num_words = 0, max_words = 10, word_len; 22 | 23 | char **words_ptr = malloc(max_words * sizeof(char *)); 24 | if (words_ptr == NULL) 25 | { 26 | printf("No enough memory is available\n"); 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | printf("Enter word: "); 31 | while ((word_len = read_line(word, MAX_WORD_LEN)) != 0) 32 | { 33 | if (num_words == max_words) 34 | { 35 | char **temp = realloc(words_ptr, max_words * 2 * sizeof(char *)); 36 | if (temp == NULL) 37 | { 38 | printf("Can't extend words array! Memory is full!\n"); 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | max_words *= 2; 43 | words_ptr = temp; 44 | } 45 | 46 | words_ptr[num_words] = malloc(word_len + 1); 47 | if (words_ptr[num_words] == NULL) 48 | { 49 | printf("Can't allocate space for a new word! Memory is full!\n"); 50 | exit(EXIT_FAILURE); 51 | } 52 | 53 | strcpy(words_ptr[num_words], word); 54 | num_words++; 55 | printf("Enter word: "); 56 | } 57 | 58 | qsort(words_ptr, num_words, sizeof(char *), compare_words); 59 | 60 | printf("\nIn sorted order: "); 61 | for (i = 0; i < num_words; i++) 62 | printf("%s ", words_ptr[i]); 63 | 64 | printf("\n"); 65 | 66 | return 0; 67 | } 68 | 69 | int read_line(char str[], int n) 70 | { 71 | int ch, i = 0; 72 | 73 | while ((ch = getchar()) != '\n') 74 | if (i < n) 75 | str[i++] = ch; 76 | str[i] = '\0'; 77 | 78 | return i; 79 | } 80 | 81 | int compare_words(const void *word1, const void *word2) 82 | { 83 | return strcmp(*(const char **)word1, *(const char **)word2); 84 | } 85 | --------------------------------------------------------------------------------