├── .gitignore ├── Exercises ├── Chapter_02 │ ├── Chapter_02.md │ ├── Exercise_01 │ │ ├── Program_02_01 │ │ │ └── program_02_01.c │ │ ├── Program_02_02 │ │ │ └── program_02_02.c │ │ ├── Program_02_03 │ │ │ └── program_02_03.c │ │ ├── Program_02_04 │ │ │ └── program_02_04.c │ │ ├── Program_02_05 │ │ │ └── program_02_05.c │ │ └── Program_02_06 │ │ │ └── program_02_06.c │ ├── Exercise_02 │ │ └── exercise_02_02.c │ ├── Exercise_03 │ │ └── exercise_02_03.c │ ├── Exercise_04 │ │ └── exercise_02_04.c │ ├── Exercise_05 │ │ └── exercise_02_05.c │ └── Exercise_06 │ │ └── exercise_02_06.c ├── Chapter_03 │ ├── Chapter_03.md │ ├── Exercise_01 │ │ ├── Program_03_01 │ │ │ └── program_03_01.c │ │ ├── Program_03_02 │ │ │ └── program_03_02.c │ │ ├── Program_03_03 │ │ │ └── program_03_03.c │ │ ├── Program_03_04 │ │ │ └── program_03_04.c │ │ └── Program_03_05 │ │ │ └── program_03_05.c │ ├── Exercise_02 │ │ └── exercise_03_02.c │ ├── Exercise_03 │ │ └── exercise_03_03.c │ ├── Exercise_04 │ │ └── exercise_03_04.c │ ├── Exercise_05 │ │ └── exercise_03_05.c │ ├── Exercise_06 │ │ └── exercise_03_06.c │ ├── Exercise_07 │ │ └── exercise_03_07.c │ └── Exercise_09 │ │ └── exercise_03_09.c ├── Chapter_04 │ ├── Chapter_04.md │ ├── Exercise_01 │ │ ├── Program_04_01 │ │ │ └── program_04_01.c │ │ ├── Program_04_02 │ │ │ └── program_04_02.c │ │ ├── Program_04_03 │ │ │ └── program_04_03.c │ │ ├── Program_04_04 │ │ │ └── program_04_04.c │ │ ├── Program_04_05 │ │ │ └── program_04_05.c │ │ ├── Program_04_06 │ │ │ └── program_04_06.c │ │ ├── Program_04_07 │ │ │ └── program_04_07.c │ │ ├── Program_04_08 │ │ │ └── program_04_08.c │ │ └── Program_04_09 │ │ │ └── program_04_09.c │ ├── Exercise_02 │ │ └── exercise_04_02.c │ ├── Exercise_03 │ │ └── exercise_04_03.c │ ├── Exercise_04 │ │ └── exercise_04_04.c │ └── Exercise_05 │ │ └── exercise_04_05.c ├── Chapter_05 │ ├── Chapter_05.md │ ├── Exercise_01 │ │ ├── Program_05_01 │ │ │ └── program_05_01.c │ │ ├── Program_05_02 │ │ │ └── program_05_02.c │ │ ├── Program_05_03 │ │ │ └── program_05_03.c │ │ ├── Program_05_04 │ │ │ └── program_05_04.c │ │ ├── Program_05_05 │ │ │ └── program_05_05.c │ │ ├── Program_05_06 │ │ │ └── program_05_06.c │ │ ├── Program_05_07 │ │ │ └── program_05_07.c │ │ ├── Program_05_08 │ │ │ └── program_05_08.c │ │ ├── Program_05_08A │ │ │ └── program_05_08A.c │ │ ├── Program_05_09 │ │ │ └── program_05_09.c │ │ ├── Program_05_10 │ │ │ └── program_05_10.c │ │ └── Program_05_10A │ │ │ └── program_05_10A.c │ ├── Exercise_02 │ │ └── exercise_05_02.c │ ├── Exercise_03 │ │ └── exercise_05_03.c │ ├── Exercise_04 │ │ └── exercise_05_04.c │ ├── Exercise_05 │ │ └── exercise_05_05.c │ ├── Exercise_06 │ │ └── exercise_05_06.c │ └── Exercise_07 │ │ └── exercise_05_07.c ├── Chapter_06 │ ├── Chapter_06.md │ ├── Exercise_01 │ │ ├── Program_06_01 │ │ │ └── program_06_01.c │ │ ├── Program_06_02 │ │ │ └── program_06_02.c │ │ ├── Program_06_03 │ │ │ └── program_06_03.c │ │ ├── Program_06_04 │ │ │ └── program_06_04.c │ │ ├── Program_06_05 │ │ │ └── program_06_05.c │ │ ├── Program_06_06 │ │ │ └── program_06_06.c │ │ ├── Program_06_07 │ │ │ └── program_06_07.c │ │ └── Program_06_08 │ │ │ └── program_06_08.c │ ├── Exercise_02 │ │ └── exercise_06_02.c │ ├── Exercise_03 │ │ └── exercise_06_03.c │ ├── Exercise_04 │ │ └── exercise_06_04.c │ ├── Exercise_05 │ │ └── exercise_06_05.c │ ├── Exercise_06 │ │ └── exercise_06_06.c │ ├── Exercise_07 │ │ └── exercise_06_07.c │ └── Exercise_08 │ │ └── exercise_06_08.c ├── Chapter_07 │ ├── Chapter_07.md │ ├── Exercise_01 │ │ ├── Program_07_01 │ │ │ └── program_07_01.c │ │ ├── Program_07_02 │ │ │ └── program_07_02.c │ │ ├── Program_07_03 │ │ │ └── program_07_03.c │ │ ├── Program_07_04 │ │ │ └── program_07_04.c │ │ ├── Program_07_05 │ │ │ └── program_07_05.c │ │ ├── Program_07_06 │ │ │ └── program_07_06.c │ │ ├── Program_07_07 │ │ │ └── program_07_07.c │ │ ├── Program_07_08 │ │ │ └── program_07_08.c │ │ ├── Program_07_09 │ │ │ └── program_07_09.c │ │ ├── Program_07_10 │ │ │ └── program_07_10.c │ │ ├── Program_07_11 │ │ │ └── program_07_11.c │ │ ├── Program_07_12 │ │ │ └── program_07_12.c │ │ ├── Program_07_13 │ │ │ └── program_07_13.c │ │ ├── Program_07_14 │ │ │ └── program_07_14.c │ │ ├── Program_07_15 │ │ │ └── program_07_15.c │ │ ├── Program_07_16 │ │ │ └── program_07_16.c │ │ └── Program_07_17 │ │ │ └── program_07_17.c │ └── Exercise_03 │ │ └── exercise_07_03.c ├── Chapter_08 │ ├── Chapter_08.md │ └── Exercise_01 │ │ ├── Program_08_01 │ │ └── program_08_01.c │ │ ├── Program_08_02 │ │ └── program_08_02.c │ │ ├── Program_08_03 │ │ └── program_08_03.c │ │ ├── Program_08_04 │ │ └── program_08_04.c │ │ ├── Program_08_05 │ │ └── program_08_05.c │ │ ├── Program_08_06 │ │ └── program_08_06.c │ │ └── Program_08_07 │ │ └── program_08_07.c ├── Chapter_09 │ ├── Chapter_09.md │ └── Exercise_01 │ │ ├── Program_09_02 │ │ └── program_09_02.c │ │ ├── Program_09_03 │ │ └── program_09_03.c │ │ ├── Program_09_04 │ │ └── program_09_04.c │ │ ├── Program_09_05 │ │ └── program_09_05.c │ │ └── Program_09_06 │ │ └── program_09_06.c ├── Chapter_10 │ ├── Chapter_10.md │ ├── Exercise_01 │ │ ├── Program_10_01 │ │ │ └── program_10_01.c │ │ ├── Program_10_02 │ │ │ └── program_10_02.c │ │ ├── Program_10_03 │ │ │ └── program_10_03.c │ │ ├── Program_10_04 │ │ │ └── program_10_04.c │ │ ├── Program_10_05 │ │ │ └── program_10_05.c │ │ ├── Program_10_06 │ │ │ └── program_10_06.c │ │ ├── Program_10_07 │ │ │ └── program_10_07.c │ │ ├── Program_10_08 │ │ │ └── program_10_08.c │ │ ├── Program_10_09 │ │ │ └── program_10_09.c │ │ ├── Program_10_10 │ │ │ └── program_10_10.c │ │ ├── Program_10_11 │ │ │ └── program_10_11.c │ │ ├── Program_10_12 │ │ │ └── program_10_12.c │ │ ├── Program_10_13 │ │ │ └── program_10_13.c │ │ ├── Program_10_14 │ │ │ └── program_10_14.c │ │ └── Program_10_15 │ │ │ └── program_10_15.c │ ├── Exercise_02 │ │ └── exercise_10_02.c │ ├── Exercise_03 │ │ └── exercise_10_03.c │ └── Exercise_04 │ │ └── exercise_10_04.c ├── Chapter_11 │ ├── Chapter_11.md │ └── Exercise_01 │ │ ├── Program_11_01 │ │ └── program_11_01.c │ │ ├── Program_11_02 │ │ └── program_11_02.c │ │ ├── Program_11_03 │ │ └── program_11_03.c │ │ └── Program_11_04 │ │ └── program_11_04.c ├── Chapter_12 │ ├── Chapter_12.md │ └── Exercise_01 │ │ ├── Program_12_01 │ │ └── program_12_01.c │ │ ├── Program_12_02 │ │ └── program_12_02.c │ │ └── Program_12_03 │ │ ├── metric.h │ │ └── program_12_03.c ├── Chapter_13 │ └── Chapter_13.md ├── Chapter_15 │ ├── Chapter_15.md │ └── Exercise_01 │ │ ├── Program_15_01 │ │ └── program_15_01.c │ │ └── Program_15_02 │ │ └── program_15_02.c └── Chapter_17 │ ├── Chapter_17.md │ └── Exercise_01 │ └── Program_17_01 │ └── program_17_01.c ├── license └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | # Eclipse workspace metadata 35 | *.metadata/ 36 | 37 | # The entire eclipse workspace 38 | # Will only upload source files and these are always linked into the work space. 39 | workspace/ 40 | 41 | # The folder with downloaded source code 42 | source code/ 43 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Chapter_02.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 2 # 2 | ## 1. ## 3 | Test run of the six programs presented in this chapter: 4 | - [Program 2.1](Exercise_01/Program_02_01/program_02_01.c) 5 | - [Program 2.2](Exercise_01/Program_02_02/program_02_02.c) 6 | - [Program 2.3](Exercise_01/Program_02_03/program_02_03.c) 7 | - [Program 2.4](Exercise_01/Program_02_04/program_02_04.c) 8 | - [Program 2.5](Exercise_01/Program_02_05/program_02_05.c) 9 | - [Program 2.6](Exercise_01/Program_02_06/program_02_06.c) 10 | 11 | ## 2. ## 12 | Program that prints some facts about C programming: 13 | - [Exercise 2.2](Exercise_02/exercise_02_02.c) 14 | 15 | ## 3. ## 16 | Below is the output from the [program](Exercise_03/exercise_02_03.c) in this exercise. 17 | ``` 18 | Testing.......1...2..3 19 | ``` 20 | 21 | ## 4. ## 22 | Program that subtracts the value 15 from 87 and displays the result: 23 | - [Exercise 2.4](Exercise_04/exercise_02_04.c) 24 | 25 | ## 5. ## 26 | Corrected version of the faulty program in this exercise: 27 | - [Exercise 2.5](Exercise_05/exercise_02_05.c) 28 | 29 | ## 6. ## 30 | Below is the output from the [program](Exercise_06/exercise_02_06.c) in this exercise. 31 | ``` 32 | The result is 95 33 | ``` 34 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_01/program_02_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_01.c - Writing Your First C Program 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | printf ("Programming is fun.\n"); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_02/program_02_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_02.c 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | printf ("Programming is fun.\n"); 9 | printf ("And programming in C is even more fun.\n"); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_03/program_02_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_03.c - Displaying Multiple Lines of Output 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | printf ("Testing...\n..1\n...2\n....3\n"); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_04/program_02_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_04.c - Displaying Variables 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int sum; 9 | 10 | sum = 50 + 25; 11 | printf ("The sum of 50 and 25 is %i\n", sum); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_05/program_02_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_05.c - Displaying Multiple Values 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int value1, value2, sum; 9 | 10 | value1 = 50; 11 | value2 = 25; 12 | sum = value1 + value2; 13 | printf ("The sum of %i and %i is %i\n", value1, value2, sum); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_01/Program_02_06/program_02_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_06.c - Using Comments in a Program 3 | */ 4 | 5 | /* This program adds two integer values and displays the result */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | // Declare variables 12 | int value1, value2, sum; 13 | 14 | // Assign values and calculate their sum 15 | value1 = 50; 16 | value2 = 25; 17 | sum = value1 + value2; 18 | 19 | // Display the result 20 | printf ("The sum of %i and %i is %i\n", value1, value2, sum); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_02/exercise_02_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_02_02.c 3 | * 4 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 5 | */ 6 | #include 7 | 8 | int main (void) 9 | { 10 | printf ("In C, lowercase letters are significant.\n"); 11 | printf ("main() is where program execution begins.\n"); 12 | printf ("Opening and closing braces enclose program statements in a routine.\n"); 13 | printf ("All program statements must be terminated by a semicolon.\n"); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_03/exercise_02_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_02_03.c 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | printf("Testing..."); 9 | printf("....1"); 10 | printf("...2"); 11 | printf("..3"); 12 | printf("\n"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_04/exercise_02_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_02_04.c 3 | * 4 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 5 | */ 6 | #include 7 | 8 | int main (void) 9 | { 10 | int value1, value2, difference; 11 | 12 | value1 = 87; 13 | value2 = 15; 14 | difference = value1 - value2; 15 | 16 | printf ("%i minus %i is %i\n", value1, value2, difference); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_05/exercise_02_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_02_05.c 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int sum; 9 | 10 | // compute result 11 | sum = 25 + 37 - 19; 12 | 13 | // display result 14 | printf("The answer is %i\n", sum); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Exercises/Chapter_02/Exercise_06/exercise_02_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_02_06.c 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int answer, result; 9 | 10 | answer = 100; 11 | result = answer - 10; 12 | printf ("The result is %i\n", result + 5); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Chapter_03.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 3 # 2 | ## 1. ## 3 | Test run of the five programs presented in this chapter: 4 | - [Program 3.1](Exercise_01/Program_03_01/program_03_01.c) 5 | - [Program 3.2](Exercise_01/Program_03_02/program_03_02.c) 6 | - [Program 3.3](Exercise_01/Program_03_03/program_03_03.c) 7 | - [Program 3.4](Exercise_01/Program_03_04/program_03_04.c) 8 | - [Program 3.5](Exercise_01/Program_03_05/program_03_05.c) 9 | 10 | ## 2. ## 11 | The following are valid variable names: 12 | ``` 13 | Int 14 | Calloc 15 | floating 16 | ReInitialize 17 | Xx 18 | _1312 19 | _ 20 | alpha_beta_routine 21 | z 22 | ``` 23 | The following are not valid variable names: 24 | ``` 25 | char // a keyword describing a type in C 26 | 6_05 // variablnames are not allowed to start with a digit 27 | A$ // character used in variable names can only be a-z, A-Z, 0-9 or _ 28 | ``` 29 | The last variable is not a valid name because of the dollar sign. It should however be noted that this variable name can often be used anyway. This is because of compiler extensions that extends the original C standard. 30 | 31 | ## 3. ## 32 | The following are invalid constants: 33 | ``` 34 | 0996 invalid digit "9" in octal constant 35 | 0x10.5 hexadecimal floating constants require an exponent 36 | 98.7U invalid suffix "U" on floating constant 37 | 1.2Fe-7 invalid suffix "Fe-7" on floating constant 38 | 0X0G1 invalid suffix "G1" on integer constant 39 | 1777s invalid suffix "s" on integer constant 40 | ``` 41 | 42 | ## 4. ## 43 | Program that converts 27 degrees Fahrenheit to Celsius: 44 | - [Exercise 3.4](Exercise_04/exercise_03_04.c) 45 | 46 | ## 5. ## 47 | The expected output from the [program](Exercise_05/exercise_03_05.c) in this exercise is: 48 | ``` 49 | d = d 50 | ``` 51 | 52 | ## 6. ## 53 | Program for that evaluates a polynomial: 54 | - [Exercise 3.6](Exercise_06/exercise_03_06.c) 55 | 56 | ## 7. ## 57 | Program that evaluates an expression with exponential numbers: 58 | - [Exercise 3.7](Exercise_07/exercise_03_07.c) 59 | 60 | ## 8. ## 61 | Nothing to do in this exercise. But contains instructions on how to calculate the next largest even multiple of another integer. This is useful to know when solving the next exercise. 62 | 63 | ## 9. ## 64 | Program that finds the next largest even multiple for some pairs of sample data: 65 | - [Exercise 3.9](Exercise_09/exercise_03_09.c) 66 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_01/Program_03_01/program_03_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_03_01.c - Using the Basic Data Types 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int integerVar = 100; 9 | float floatingVar = 331.79; 10 | double doubleVar = 8.44e+11; 11 | char charVar = 'W'; 12 | 13 | _Bool boolVar = 0; 14 | 15 | printf ("integerVar = %i\n", integerVar); 16 | printf ("floatingVar = %f\n", floatingVar); 17 | printf ("doubleVar = %e\n", doubleVar); 18 | printf ("doubleVar = %g\n", doubleVar); 19 | printf ("charVar = %c\n", charVar); 20 | 21 | printf ("boolVar = %i\n", boolVar); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_01/Program_03_02/program_03_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_03_02.c - Using the Arithmetic Operators 3 | */ 4 | 5 | // Illustrate the use of various arithmetic operators 6 | #include 7 | 8 | int main (void) 9 | { 10 | int a = 100; 11 | int b = 2; 12 | int c = 25; 13 | int d = 4; 14 | int result; 15 | 16 | result = a - b; // subtraction 17 | printf ("a - b = %i\n", result); 18 | 19 | result = b * c; // multiplication 20 | printf ("b * c = %i\n", result); 21 | 22 | result = a / c; // division 23 | printf ("a / c = %i\n", result); 24 | 25 | result = a + b * c; // precedence 26 | printf ("a + b * c = %i\n", result); 27 | 28 | printf ("a * b + c * d = %i\n", a * b + c * d); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_01/Program_03_03/program_03_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_03_03.c - More Examples with Arithmetic Operators 3 | */ 4 | 5 | // More arithmetic expressions 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int a = 25; 12 | int b = 2; 13 | 14 | float c = 25.0; 15 | float d = 2.0; 16 | 17 | printf("6 + a / 5 * b = %i\n", 6 + a / 5 * b); 18 | printf("a / b * b = %i\n", a / b * b); 19 | printf("c / d * d = %f\n", c / d * d ); 20 | printf("-a = %i\n", -a); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_01/Program_03_04/program_03_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_03_04.c - Illustrating the Modulus Operator 3 | */ 4 | 5 | // The modulus operator 6 | #include 7 | 8 | int main (void) 9 | { 10 | int a = 25, b = 5, c = 10, d = 7; 11 | 12 | printf ("a = %i, d = %i, c = %i, and d = %i\n", a, b, c, d); 13 | printf ("a %% b = %i\n", a % b); 14 | printf ("a %% c = %i\n", a % c); 15 | printf ("a %% d = %i\n", a % d); 16 | printf ("a / d * d + a %% d = %i\n", 17 | a / d * d + a % d); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_01/Program_03_05/program_03_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_03_05.c - Converting Between Integers and Floats 3 | */ 4 | 5 | // Basic conversions in C 6 | #include 7 | 8 | int main (void) 9 | { 10 | float f1 = 123.125, f2; 11 | int i1, i2 = -150; 12 | char c = 'a'; 13 | 14 | // floating to integer conversion 15 | i1 = f1; 16 | printf ("%f assigned to an int procedure %i\n", f1, i1); 17 | 18 | // integer to floating conversion 19 | f1 = i2; 20 | printf ("%i assigned to a float procedure %f\n", i2, f1); 21 | 22 | // integer divided by integer 23 | f1 = i2 / 100; 24 | printf ("%i divided by 100 produces %f\n", i2, f1); 25 | 26 | // integer divided by float 27 | f2 = i2 / 100.0; 28 | printf ("%i divided by 100.0 produces %f\n", i2, f2); 29 | 30 | // type cast operator 31 | f2 = (float) i2 / 100; 32 | printf ("(float) %i divided by 100 produces %f\n", i2, f2); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_02/exercise_03_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_02.c 3 | * 4 | * Test program to verify the answer for exercise 3.2. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | int main (void) 10 | { 11 | int Int; 12 | int Calloc; 13 | int floaitng; 14 | int ReInitialize; 15 | 16 | // int char; 17 | int Xx; 18 | int _1312; 19 | int _; 20 | 21 | // int 6_05; 22 | int alpha_beta_routine; 23 | int z; 24 | int A$; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_03/exercise_03_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_03.c 3 | * 4 | * Test program to verify the answer for exercise 3.2. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | int main (void) 9 | { 10 | 123.456; 11 | 0001; 12 | 0Xab05; 13 | 123.5e2; 14 | 98.6F; 15 | 0996; 16 | 1234uL; 17 | 1.234L; 18 | 0XABCDEFL; 19 | 20 | 0x10.5; 21 | 0xFFFF; 22 | 0L; 23 | .0001; 24 | 98.7U; 25 | -12E-12; 26 | 1.2Fe-7; 27 | 197u; 28 | 0xabcu; 29 | 30 | 0X0G1; 31 | 123L; 32 | -597.25; 33 | +12; 34 | 1777s; 35 | 07777; 36 | 15,000; 37 | 100U; 38 | +123; 39 | 40 | return 0; 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_04/exercise_03_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_04.c 3 | * 4 | * Converts from Fahrenheit to Celsius. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | #include 9 | 10 | int main (void) 11 | { 12 | float c; 13 | float f = 27.0; 14 | 15 | c = (f - 32) / 1.8; 16 | 17 | printf("%f degrees Fahrenheit equals %f Celsius", f, c); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_05/exercise_03_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_05.c 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | char c, d; 9 | 10 | c = 'd'; 11 | d = c; 12 | printf ("d = %c\n", d); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_06/exercise_03_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_06.c 3 | * 4 | * Evaluation of a polynomial. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | #include 9 | 10 | int main (void) 11 | { 12 | float result; 13 | float x = 2.55; 14 | 15 | result = 3 * x * x * x + 5 * x * x + 6; 16 | 17 | printf("result = %f", result); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_07/exercise_03_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_07.c 3 | * 4 | * Evaluation of an expression with exponential numbers. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | #include 9 | 10 | int main (void) 11 | { 12 | float result; 13 | 14 | result = (3.31e-8 * 2.01e-7) / (7.16e-6 * 2.01e-8); 15 | printf("result = %e", result); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_03/Exercise_09/exercise_03_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_03_09.c 3 | * 4 | * Finds the next largest even multiple for some pairs of sample data. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | #include 9 | 10 | int main (void) 11 | { 12 | printf("The next largest even multiple of %i and %i is %i.\n", 365, 7, 13 | 365 + 7 - 365 % 7); 14 | printf("The next largest even multiple of %i and %i is %i.\n", 12258, 23, 15 | 12258 + 23 - 12258 % 23); 16 | printf("The next largest even multiple of %i and %i is %i.\n", 996, 4, 17 | 996 + 4 - 996 % 4); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Chapter_04.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 4 # 2 | ## 1. ## 3 | Test run of the nine programs presented in this chapter. 4 | - [program_04_01.c](Exercise_01/Program_04_01/program_04_01.c) 5 | - [program_04_02.c](Exercise_01/Program_04_02/program_04_02.c) 6 | - [program_04_03.c](Exercise_01/Program_04_03/program_04_03.c) 7 | - [program_04_04.c](Exercise_01/Program_04_04/program_04_04.c) 8 | - [program_04_05.c](Exercise_01/Program_04_05/program_04_05.c) 9 | - [program_04_06.c](Exercise_01/Program_04_06/program_04_06.c) 10 | - [program_04_07.c](Exercise_01/Program_04_07/program_04_07.c) 11 | - [program_04_08.c](Exercise_01/Program_04_08/program_04_08.c) 12 | - [program_04_09.c](Exercise_01/Program_04_09/program_04_09.c) 13 | 14 | ## 2. ## 15 | Program that generates a table of squared numbers. 16 | - [exercise_04_02.c](Exercise_02/exercise_04_02.c) 17 | 18 | ## 3. ## 19 | Program that generates a table of triangular numbers. 20 | - [exercise_04_03.c](Exercise_03/exercise_04_03.c) 21 | 22 | ## 4. ## 23 | Program that generates a table of factorials. 24 | - [exercise_04_04.c](Exercise_04/exercise_04_04.c) 25 | 26 | ## 5. ## 27 | Reformatted version of the given program in this exercise. 28 | - [exercise_04_05.c](Exercise_05/exercise_04_05.c) 29 | 30 | 31 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_01/program_04_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_01.c - Calculating the Eighth Triangular Number 3 | * 4 | * Program to calculate the eighth triangular number. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int triangularNumber; 12 | 13 | triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8; 14 | 15 | printf ("The eighth triangular number is %i\n", triangularNumber); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_02/program_04_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_02.c - Calculating the 200th Triangular Number 3 | * 4 | * Program to calculate the 200th triangular number. 5 | * Introduction of the for statement. 6 | */ 7 | 8 | #include 9 | 10 | int main (void) 11 | { 12 | int n, triangularNumber; 13 | 14 | triangularNumber = 0; 15 | 16 | for (n = 1; n <= 200; n = n + 1) 17 | triangularNumber = triangularNumber + n; 18 | 19 | printf ("The 200th triangular number is %i\n", triangularNumber); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_03/program_04_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_03.c - Generating a table of Triangular Numbers 3 | * 4 | * Program to generate a table of triangular numbers. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int n, triangularNumber; 12 | 13 | printf ("TABLE OF TRIANGULAR NUMBERS\n\n"); 14 | printf (" n Sum from 1 to n\n"); 15 | printf ("--- ---------------\n"); 16 | 17 | triangularNumber = 0; 18 | 19 | for (n = 1; n <= 10; ++n) 20 | { 21 | triangularNumber += n; 22 | printf ("%2i %i\n", n, triangularNumber); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_04/program_04_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_04.c - Asking the user for input 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int n, number, triangularNumber; 10 | 11 | printf ("What triangular number do you want? "); 12 | scanf ("%i", &number); 13 | 14 | triangularNumber = 0; 15 | 16 | for (n = 1; n <= number; ++n) 17 | triangularNumber += n; 18 | 19 | printf("Triangular number %i is %i\n", number, triangularNumber); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_05/program_04_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_05.c - Using Nested for Loops 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int n, number, triangularNumber, counter; 10 | 11 | for (counter = 1; counter <= 5; ++counter) 12 | { 13 | printf ("What triangular number do you want? "); 14 | scanf ("%i", &number); 15 | 16 | triangularNumber = 0; 17 | 18 | for (n = 1; n <= number; ++n) 19 | triangularNumber += n; 20 | 21 | printf ("Triangular number %i is %i\n\n", number, triangularNumber); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_06/program_04_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_06.c - Introducing the while Statement 3 | * 4 | * Program to introduce the while statement. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int count = 1; 12 | 13 | while (count <= 5) 14 | { 15 | printf ("%i\n", count); 16 | ++count; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_07/program_04_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_07.c - Finding the Greatest Common Divisor 3 | * 4 | * Program to find the greatest common divisor of two nonnegative integer 5 | * values. 6 | */ 7 | 8 | #include 9 | 10 | int main (void) 11 | { 12 | int u, v, temp; 13 | 14 | printf ("Please type in two nonnegative integers.\n"); 15 | scanf ("%i%i", &u, &v); 16 | 17 | while (v != 0) { 18 | temp = u % v; 19 | u = v; 20 | v = temp; 21 | } 22 | 23 | printf ("Their greatest common divisor is %i\n", u); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_08/program_04_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_08.c - Reversing the Digits of a Number 3 | * 4 | * Program to reverse the digits of a number. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number, right_digit; 12 | 13 | printf("Enter your number.\n"); 14 | scanf ("%i", &number); 15 | 16 | while (number != 0) 17 | { 18 | right_digit = number % 10; 19 | printf ("%i", right_digit); 20 | number = number / 10; 21 | } 22 | 23 | printf("\n"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_01/Program_04_09/program_04_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_04_09.c - Revised Program to Reverse the Digits of a Number 3 | * 4 | * Program to reverse the digits of a number. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number, right_digit; 12 | 13 | printf ("Enter your number.\n"); 14 | scanf ("%i", &number); 15 | 16 | do 17 | { 18 | right_digit = number % 10; 19 | printf ("%i", right_digit); 20 | number = number / 10; 21 | } 22 | while (number != 0); 23 | 24 | printf ("\n"); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_02/exercise_04_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_04_02.c 3 | * 4 | * Program that generates a table of squared numbers. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int n; 14 | 15 | printf (" n n^2 \n"); 16 | printf ("----- -----\n"); 17 | 18 | for ( n = 1; n < 11; n++ ) 19 | { 20 | printf (" %2i %3i \n", n, n * n); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_03/exercise_04_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_04_03.c 3 | * 4 | * Program that generates a table of triangular numbers. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int n; 14 | 15 | printf (" n t \n"); 16 | printf ("----- -------\n"); 17 | 18 | for (n = 5; n <= 50 ; n = n + 5) 19 | { 20 | printf(" %2i %4i \n", n, n * (n + 1) / 2); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_04/exercise_04_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_04_04.c 3 | * 4 | * Program that generates a table of factorials. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int n, factorial; 14 | 15 | 16 | printf (" n n! \n"); 17 | printf ("----- ----------\n"); 18 | 19 | factorial = 1; 20 | 21 | for ( n = 1; n <= 10; n++ ) 22 | { 23 | factorial *= n; 24 | printf (" %2i %7i \n", n, factorial); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_04/Exercise_05/exercise_04_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_04_05.c 3 | * 4 | * Program that generates a table with powers of two. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int n, two_to_the_n; 14 | 15 | printf("TABLE OF POWERS OF TWO\n\n"); 16 | printf(" n 2 to the n\n"); 17 | printf("--- ---------------\n"); 18 | 19 | two_to_the_n = 1; 20 | for (n = 0; n <= 10; ++n) 21 | { 22 | printf("%2i %i\n", n, two_to_the_n); 23 | two_to_the_n *= 2; 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Chapter_05.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 5 # 2 | ## 1. ## 3 | Test run of the twelve programs presented in this chapter. 4 | - [program_05_01.c](Exercise_01/Program_05_01/program_05_01.c) 5 | - [program_05_02.c](Exercise_01/Program_05_02/program_05_02.c) 6 | - [program_05_03.c](Exercise_01/Program_05_03/program_05_03.c) 7 | - [program_05_04.c](Exercise_01/Program_05_04/program_05_04.c) 8 | - [program_05_05.c](Exercise_01/Program_05_05/program_05_05.c) 9 | - [program_05_06.c](Exercise_01/Program_05_06/program_05_06.c) 10 | - [program_05_07.c](Exercise_01/Program_05_07/program_05_07.c) 11 | - [program_05_08.c](Exercise_01/Program_05_08/program_05_08.c) 12 | - [program_05_08A.c](Exercise_01/Program_05_08A/program_05_08A.c) 13 | - [program_05_09.c](Exercise_01/Program_05_09/program_05_09.c) 14 | - [program_05_10.c](Exercise_01/Program_05_10/program_05_10.c) 15 | - [program_05_10A.c](Exercise_01/Program_05_10A/program_05_10A.c) 16 | 17 | ## 2. ## 18 | Program that checks if a given number is evenly divisible by another given number. 19 | - [exercise_05_02.c](Exercise_02/exercise_05_02.c) 20 | 21 | ## 3. ## 22 | Program that divides two given integers. 23 | - [exercise_05_03.c](Exercise_03/exercise_05_03.c) 24 | 25 | ## 4. ## 26 | Program that acts as a calculator. 27 | - [exercise_05_04.c](Exercise_04/exercise_05_04.c) 28 | 29 | ## 5. ## 30 | Program to reverse the digits of a number. 31 | - [exercise_05_05.c](Exercise_05/exercise_05_05.c) 32 | 33 | ## 6. ## 34 | Program that takes an integer entered by the user and extracts each digit into English. 35 | - [exercise_05_06.c](Exercise_06/exercise_05_06.c) 36 | 37 | ## 7. ## 38 | Program that generates some prime numbers. 39 | - [exercise_05_07.c](Exercise_07/exercise_05_07.c) 40 | 41 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_01/program_05_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_01.c - Calculating the Absolute Value of an Integer. 3 | * 4 | * Program to calculate the absolute value of an integer. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number; 12 | 13 | printf ("Type in your number: "); 14 | scanf ("%i", &number); 15 | 16 | if (number < 0) 17 | number = -number; 18 | 19 | printf ("The absolute value is %i\n", number); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_02/program_05_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_02.c 3 | * 4 | * Program to calculate the average of a set of grades and count the number of 5 | * failing grades. 6 | */ 7 | 8 | #include 9 | 10 | int main (void) 11 | { 12 | int numberOfGrades, i, grade; 13 | int gradeTotal = 0; 14 | int failureCount = 0; 15 | float average; 16 | 17 | printf ("How many grades will you be entering? "); 18 | scanf ("%i", &numberOfGrades); 19 | 20 | for (i = 1; i <= numberOfGrades; ++i) 21 | { 22 | printf ("Enter grade #%i", i); 23 | scanf ("%i", &grade); 24 | 25 | gradeTotal = gradeTotal + grade; 26 | 27 | if (grade < 65) 28 | ++failureCount; 29 | } 30 | 31 | average = (float) gradeTotal / numberOfGrades; 32 | 33 | printf ("\nGrade average = %.2f\n", average); 34 | printf ("Number of failures = %i\n", failureCount); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_03/program_05_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_03.c 3 | * 4 | * Program to determine if a number is even or odd. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number_to_test, remainder; 12 | 13 | printf ("Enter your number to be tested: "); 14 | scanf ("%i", &number_to_test); 15 | 16 | remainder = number_to_test % 2; 17 | 18 | if (remainder == 0) 19 | printf ("The number is even.\n"); 20 | 21 | if (remainder != 0) 22 | printf ("The number is odd.\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_04/program_05_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_04.c 3 | * 4 | * Program to determine if a number is even or odd (version 2). 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number_to_test, remainder; 12 | 13 | printf ("Enter your number to be tested: "); 14 | scanf ("%i", &number_to_test); 15 | 16 | remainder = number_to_test % 2; 17 | 18 | if (remainder == 0) 19 | printf ("The number is even.\n"); 20 | else 21 | printf ("The number is odd.\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_05/program_05_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_05.c 3 | * 4 | * Program to determine if a year is a leap year. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int year, rem_4, rem_100, rem_400; 12 | 13 | printf ("Enter the year to be tested: "); 14 | scanf ("%i", &year); 15 | 16 | rem_4 = year % 4; 17 | rem_100 = year % 100; 18 | rem_400 = year % 400; 19 | 20 | if ( (rem_4 == 0 && rem_100 != 0 ) || rem_400 == 0) 21 | printf ("It's a leap year.\n"); 22 | else 23 | printf ("Nope, it's not a leap year\n"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_06/program_05_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_06.c 3 | * 4 | * Program to implement the sign function. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int number, sign; 12 | 13 | printf ("Please type in a number: "); 14 | scanf ("%i", &number); 15 | 16 | if (number < 0) 17 | sign = -1; 18 | else if (number == 0) 19 | sign = 0; 20 | else 21 | sign = 1; 22 | 23 | printf ("Sign = %i\n", sign); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_07/program_05_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_07.c 3 | * 4 | * Program to categorize a single character that is entered at the terminal. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | char c; 12 | 13 | printf ("Enter a single character:\n"); 14 | scanf ("%c", &c); 15 | 16 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 17 | printf ("It's an alphabetic character.\n"); 18 | else if (c >= '0' && c <= '9') 19 | printf ("It's a digit.\n"); 20 | else 21 | printf ("It's a special character.\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_08/program_05_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_08.c 3 | * 4 | * Program to evaluate simple expressions of the form "number operator number" 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | float value1, value2; 12 | char operator; 13 | 14 | printf ("Type in your expression.\n"); 15 | scanf ("%f %c %f", &value1, &operator, &value2); 16 | 17 | if (operator == '+') 18 | printf ("%.2f\n", value1 + value2); 19 | else if (operator == '-') 20 | printf ("%.2f\n", value1 - value2); 21 | else if (operator == '*') 22 | printf ("%.2f\n", value1 * value2); 23 | else if (operator == '/') 24 | printf ("%.2f\n", value1 / value2); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_08A/program_05_08A.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_08A.c 3 | * 4 | * Program to evaluate simple expressions of the form "number operator number" 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | float value1, value2; 12 | char operator; 13 | 14 | printf ("Type in your expression.\n"); 15 | scanf ("%f %c %f", &value1, &operator, &value2); 16 | 17 | if (operator == '+') 18 | printf ("%.2f\n", value1 + value2); 19 | else if (operator == '-') 20 | printf ("%.2f\n", value1 - value2); 21 | else if (operator == '*') 22 | printf ("%.2f\n", value1 * value2); 23 | else if (operator == '/') 24 | if (value2 == 0) 25 | printf ("Division by zero."); 26 | else 27 | printf ("%.2f\n", value1 / value2); 28 | else 29 | printf ("Unknown operator.\n"); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_09/program_05_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_09.c 3 | * 4 | * Program to evaluate simple expressions of the form "number operator number" 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | float value1, value2; 12 | char operator; 13 | 14 | printf ("Type in your expression.\n"); 15 | scanf ("%f %c %f", &value1, &operator, &value2); 16 | 17 | switch (operator) 18 | { 19 | case '+': 20 | printf ("%.2f\n", value1 + value2); 21 | break; 22 | case '-': 23 | printf ("%.2f\n", value1 - value2); 24 | break; 25 | case '*': 26 | printf ("%.2f\n", value1 * value2); 27 | break; 28 | case '/': 29 | if (value2 == 0) 30 | printf ("Division by zero."); 31 | else 32 | printf ("%.2f\n", value1 / value2); 33 | break; 34 | default: 35 | printf ("Unknown operator.\n"); 36 | break; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_10/program_05_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_10.c 3 | * 4 | * Program to generate a table of prime numbers. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int p, d; 12 | _Bool isPrime; 13 | 14 | for (p = 2; p <= 50; ++p) 15 | { 16 | isPrime = 1; 17 | 18 | for (d = 2; d < p; ++d) 19 | if (p % d == 0) 20 | isPrime = 0; 21 | 22 | if (isPrime != 0) 23 | printf ("%i ", p); 24 | } 25 | 26 | printf ("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_01/Program_05_10A/program_05_10A.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_10A.c 3 | * 4 | * Program to generate a table of prime numbers. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main (void) 11 | { 12 | int p, d; 13 | bool isPrime; 14 | 15 | for (p = 2; p <= 50; ++p) 16 | { 17 | isPrime = true; 18 | 19 | for (d = 2; d < p; ++d) 20 | if (p % d == 0) 21 | isPrime = false; 22 | 23 | if (isPrime != false) 24 | printf ("%i ", p); 25 | } 26 | 27 | printf ("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_02/exercise_05_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_05_02.c 3 | * 4 | * Program that checks if two numbers are are evenly divisible. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int i1, i2; 14 | 15 | printf ("Enter two numbers to be analyzed: "); 16 | scanf("%i%i", &i1, &i2); 17 | 18 | if (i1 % i2 == 0) 19 | printf("%i is evenly divisible by %i", i1, i2); 20 | else 21 | printf("%i is not evenly divisible by %i", i1, i2); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_03/exercise_05_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_05_03.c 3 | * 4 | * Program that divides two given integers. 5 | * 6 | * The result is displayed with 3 decimals. 7 | * 8 | * There is a built in check to defend against division by zero. 9 | * 10 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 11 | */ 12 | 13 | #include 14 | 15 | int main (void) 16 | { 17 | 18 | int i1, i2; 19 | 20 | printf ("Enter two numbers to be used for division: "); 21 | scanf ("%i%i", &i1, &i2); 22 | 23 | if (i2 == 0) 24 | printf ("Incorrect input, the second number is not allowed to be " 25 | "zero."); 26 | else 27 | printf ("%i / %i = %.3f", i1, i2, (float) i1 / i2); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_04/exercise_05_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_05_04.c 3 | * 4 | * Program that acts as a calculator. 5 | * 6 | * Author: Henrik Samuelsson, henrk.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main (void) 13 | { 14 | double accumulator; 15 | double number; 16 | char operator; 17 | bool isCalculating; 18 | 19 | // The accumulator shall be 0 at startup 20 | accumulator = 0.0; 21 | 22 | // Set flag indicating that calculations are ongoing 23 | isCalculating = true; 24 | 25 | printf ("Begin Calculations\n"); 26 | 27 | // Run calculations until user indicates exit 28 | while (isCalculating) 29 | { 30 | // Get input from the user. Note the blank in the format string that 31 | // tells scanf to skip leading whitespace, and the first non-whitespace 32 | // character will be read with the %c conversion specifier. 33 | scanf ("%lf %c", &number, &operator); 34 | 35 | switch (operator) 36 | { 37 | case '+': 38 | accumulator += number; 39 | break; 40 | case '-': 41 | accumulator -= number; 42 | break; 43 | case '*': 44 | accumulator *= number; 45 | break; 46 | case '/': 47 | if (number == 0) 48 | printf ("ERROR: Division by 0 is not allowed!"); 49 | else 50 | accumulator /= number; 51 | break; 52 | case 'S': 53 | accumulator = number; 54 | break; 55 | case 'E': 56 | isCalculating = false; 57 | break; 58 | default: 59 | printf ("ERROR: Unknown operator!\n"); 60 | break; 61 | } 62 | 63 | printf ("= %f\n", accumulator); 64 | } 65 | 66 | printf ("End of Calculations"); 67 | 68 | return 0; 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_05/exercise_05_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_05.c 3 | * 4 | * Program to reverse the digits of a number. 5 | * 6 | * Can handle both positive and negative numbers. 7 | * 8 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 9 | */ 10 | 11 | #include 12 | 13 | int main (void) 14 | { 15 | int number, right_digit; 16 | 17 | printf ("Enter your number.\n"); 18 | scanf ("%i", &number); 19 | 20 | if (number < 0) 21 | { 22 | number *= -1; 23 | printf ("-"); 24 | } 25 | 26 | do 27 | { 28 | right_digit = number % 10; 29 | printf ("%i", right_digit); 30 | number = number / 10; 31 | } 32 | while (number != 0); 33 | 34 | printf ("\n"); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_06/exercise_05_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_05_06.c 3 | * 4 | * Program that takes an integer entered by the user and extracts each digit 5 | * into English. 6 | * 7 | * Note: The code would be way shorter if an array was used for storing the 8 | * number strings. But arrays have not been introduced in the book yet, hence 9 | * the switch statement. 10 | * 11 | * Thanks to Doug Tuttle for reporting bug in previous version of this code. 12 | * 13 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 14 | */ 15 | 16 | #include 17 | 18 | int main (void) 19 | { 20 | int input = 0, reversedInput = 0, digit; 21 | int numberOfDigits = 0; // The number of digits in integer input by user 22 | 23 | scanf ("%d", &input); 24 | 25 | // Start by reversing the input number 26 | do 27 | { 28 | reversedInput *= 10; 29 | reversedInput = reversedInput + input % 10; 30 | input /= 10; 31 | numberOfDigits++; 32 | 33 | } while (input != 0); 34 | 35 | // Extract each digit from the right and print it in English 36 | do 37 | { 38 | digit = reversedInput % 10; 39 | reversedInput /= 10; 40 | numberOfDigits--; 41 | 42 | switch (digit) 43 | { 44 | case 0: 45 | printf ("zero "); 46 | break; 47 | case 1: 48 | printf ("one "); 49 | break; 50 | case 2: 51 | printf ("two "); 52 | break; 53 | case 3: 54 | printf ("three "); 55 | break; 56 | case 4: 57 | printf ("four "); 58 | break; 59 | case 5: 60 | printf ("five "); 61 | break; 62 | case 6: 63 | printf ("six "); 64 | break; 65 | case 7: 66 | printf ("seven "); 67 | break; 68 | case 8: 69 | printf ("eight "); 70 | break; 71 | case 9: 72 | printf ("nine "); 73 | break; 74 | } 75 | 76 | } while (numberOfDigits != 0); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Exercises/Chapter_05/Exercise_07/exercise_05_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_05_07.c 3 | * 4 | * Program to generate a table of prime numbers. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main (void) 13 | { 14 | int p, d; 15 | bool isPrime; 16 | 17 | // print 2 as the first prime 18 | printf("2 "); 19 | 20 | for (p = 3; p <= 50; p += 2) 21 | { 22 | isPrime = true; 23 | 24 | // only loop through odd numbers as divisors 25 | for (d = 3; d < p && isPrime; d += 2) 26 | if (p % d == 0) 27 | isPrime = false; 28 | 29 | if (isPrime != false) 30 | printf ("%i ", p); 31 | } 32 | 33 | printf ("\n"); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Chapter_06.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 6 # 2 | ## 1. ## 3 | Test run of the eight programs presented in this chapter. 4 | - [program_06_01.c](Exercise_01/Program_06_01/program_06_01.c) 5 | - [program_06_02.c](Exercise_01/Program_06_02/program_06_02.c) 6 | - [program_06_03.c](Exercise_01/Program_06_03/program_06_03.c) 7 | - [program_06_04.c](Exercise_01/Program_06_04/program_06_04.c) 8 | - [program_06_05.c](Exercise_01/Program_06_05/program_06_05.c) 9 | - [program_06_06.c](Exercise_01/Program_06_06/program_06_06.c) 10 | - [program_06_07.c](Exercise_01/Program_06_07/program_06_07.c) 11 | - [program_06_08.c](Exercise_01/Program_06_08/program_06_08.c) 12 | 13 | ## 2. ## 14 | Program 6.1 again, but this time with an initial for loop that sets all values of the array to 0. 15 | - [exercise_06_02.c](Exercise_02/exercise_06_02.c) 16 | 17 | ## 3. ## 18 | Program 6.2 again, but there is no longer a fixed number of responses. The user can enter any number of responses and ends the input by typing in 999. 19 | - [exercise_06_03.c](Exercise_03/exercise_06_03.c) 20 | 21 | ## 4. ## 22 | Program to calculate the average of some values. 23 | - [exercise_06_04.c](Exercise_04/exercise_06_04.c) 24 | 25 | ## 5. ## 26 | The expected output from the [program](Exercise_05/exercise_06_05.c) in this exercise is 27 | ``` 28 | 1 1 2 4 8 16 32 64 128 256 29 | ``` 30 | The program is based around an array that holds ten integers. A given value in the array is set to the sum of all values before the given value, except for the very first value that is set to 1. 31 | 32 | ## 6. ## 33 | Program that calculates and prints the first 15 Fibonacci numbers. Only three variables are needed to store the numbers in this implementation. 34 | - [exercise_06_06.c](Exercise_06/exercise_06_06.c) 35 | 36 | ## 7. ## 37 | Program that implements a version of the Sieve of Eratosthenes to generate prime numbers. 38 | - [exercise_06_07.c](Exercise_07/exercise_06_07.c) 39 | 40 | ## 8. ## 41 | Program that calculates the dot product of two vectors by the use of variable length arrays. 42 | - [exercise_06_08.c](Exercise_08/exercise_06_08.c) 43 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_01/program_06_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_01.c - Working with an Array 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int values[10]; 10 | int index; 11 | 12 | values[0] = 197; 13 | values[2] = -100; 14 | values[5] = 350; 15 | values[3] = values[0] + values[5]; 16 | values[9] = values[5] / 10; 17 | --values[2]; 18 | 19 | for (index = 0; index < 10; ++index) 20 | printf ("values[%i] = %i\n", index, values[index]); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_02/program_06_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_02.c - Demonstrating an Array of Counters 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int ratingCounters[11], i, response; 10 | 11 | for (i = 1; i <= 10; ++i) 12 | ratingCounters[i] = 0; 13 | 14 | printf ("Enter your responses\n"); 15 | 16 | for (i = 1; i <= 20; ++i) 17 | { 18 | scanf ("%i", &response); 19 | 20 | if (response < 1 || response > 10) 21 | printf ("Bad response: %i\n", response); 22 | else 23 | ++ratingCounters[response]; 24 | } 25 | 26 | printf ("\n\n"); 27 | printf ("Rating Number of Responses\n"); 28 | printf ("------ -------------------\n"); 29 | 30 | for (i = 1; i <= 10; ++i) 31 | printf ("%4i%14i\n", i, ratingCounters[i]); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_03/program_06_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_03.c 3 | * 4 | * Program to generate the first 15 Fibonacci numbers. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int Fibonacci[15], i; 12 | 13 | Fibonacci[0] = 0; // by definition 14 | Fibonacci[1] = 1; // by definition 15 | 16 | for (i = 2; i < 15; ++i) 17 | Fibonacci[i] = Fibonacci[i - 2] + Fibonacci[i - 1]; 18 | 19 | for (i = 0; i < 15; ++i) 20 | printf ("%i\n", Fibonacci[i]); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_04/program_06_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_04.c - Revising the Program to Generate Prime Numbers, Version 2 3 | * 4 | * Program to generate prime numbers. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main (void) 11 | { 12 | int p, i, primes[50], primeIndex = 2; 13 | bool isPrime; 14 | 15 | primes[0] = 2; 16 | primes[1] = 3; 17 | 18 | for (p = 5; p <= 50; p = p + 2) 19 | { 20 | isPrime = true; 21 | 22 | for (i = 1; isPrime && p / primes[i] >= primes[i]; ++i) 23 | if (p % primes[i] == 0) 24 | isPrime = false; 25 | 26 | if (isPrime == true) 27 | { 28 | primes[primeIndex] = p; 29 | ++primeIndex; 30 | } 31 | } 32 | 33 | for (i = 0; i < primeIndex; ++i) 34 | printf ("%i ", primes[i]); 35 | 36 | printf ("\n"); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_05/program_06_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_05.c - Initializing Arrays 3 | * 4 | * Program that illustrates two types of array-initialization techniques. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int array_values[10] = { 0, 1, 4, 9, 16 }; 12 | int i; 13 | 14 | for (i = 5; i < 10; ++i) 15 | array_values[i] = i * i; 16 | 17 | for (i = 0; i < 10; ++i) 18 | printf("array_values[%i] = %i\n",i , array_values[i]); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_06/program_06_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_06.c - Introducing Character Arrays 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | char word[] = {'H', 'e', 'l', 'l', 'o', '!'}; 10 | int i; 11 | 12 | for (i = 0; i < 6; ++i) 13 | printf ("%c", word[i]); 14 | 15 | printf ("\n"); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_07/program_06_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_07.c 3 | * 4 | * Program to convert a positive integer to another base. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | const char baseDigits[16] = { 12 | '0', '1', '2', '3', '4','5', '6', '7', 13 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 14 | int convertedNumber[64]; 15 | long int numberToConvert; 16 | int nextDigit, base, index = 0; 17 | 18 | // get the number and the base 19 | printf ("Number to be converted? "); 20 | scanf ("%d", &numberToConvert); 21 | printf ("Base? "); 22 | scanf ("%d", &base); 23 | 24 | // convert to the indicated base 25 | do { 26 | convertedNumber[index] = numberToConvert % base; 27 | ++index; 28 | numberToConvert = numberToConvert / base; 29 | } while (numberToConvert != 0); 30 | 31 | // display the result in reverse order 32 | printf ("Converted number = "); 33 | for(--index; index >= 0; --index) { 34 | nextDigit = convertedNumber[index]; 35 | printf ("%c", baseDigits[nextDigit]); 36 | } 37 | printf ("\n"); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_01/Program_06_08/program_06_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_06_08.c 3 | * 4 | * Generate Fibonacci numbers using variable length arrays. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int i, numFibs; 12 | 13 | printf ("How many Fibonacci numbers do you want (between 1 and 75)? "); 14 | scanf ("%i", &numFibs); 15 | 16 | if (numFibs < 1 || numFibs > 75) 17 | { 18 | printf ("Bad number sorry!\n"); 19 | return 1; 20 | } 21 | 22 | unsigned long long int Fibonacci[numFibs]; 23 | 24 | Fibonacci[0] = 0; // by definition 25 | Fibonacci[1] = 1; // by definition 26 | 27 | for (i = 2; i < numFibs; ++i) 28 | Fibonacci[i] = Fibonacci[i - 2] + Fibonacci[i - 1]; 29 | 30 | for (i = 0; i < numFibs; ++i) 31 | printf ("%llu ", Fibonacci[i]); 32 | 33 | printf ("\n"); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_02/exercise_06_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_02.c - Working with an Array 3 | * 4 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | int values[10]; 12 | int index; 13 | 14 | for (index = 0; index < 10; ++index) 15 | values[index] = 0; 16 | 17 | values[0] = 197; 18 | values[2] = -100; 19 | values[5] = 350; 20 | values[3] = values[0] + values[5]; 21 | values[9] = values[5] / 10; 22 | --values[2]; 23 | 24 | for (index = 0; index < 10; ++index) 25 | printf ("values[%i] = %i\n", index, values[index]); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_03/exercise_06_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_03.c - Demonstrating an Array of Counters 3 | * 4 | * Based on program 6.2 but without fixed number of responses. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int ratingCounters[11], i, response; 14 | 15 | for (i = 1; i <= 10; ++i) 16 | ratingCounters[i] = 0; 17 | 18 | printf ("Enter your responses, enter 999 when done.\n"); 19 | 20 | for (i = 1; i <= 20; ++i) 21 | { 22 | scanf ("%i", &response); 23 | 24 | if (response == 999) 25 | break; 26 | else if (response < 1 || response > 10) 27 | printf ("Bad response: %i\n", response); 28 | else 29 | ++ratingCounters[response]; 30 | } 31 | 32 | printf ("\n\n"); 33 | printf ("Rating Number of Responses\n"); 34 | printf ("------ -------------------\n"); 35 | 36 | for (i = 1; i <= 10; ++i) 37 | printf ("%4i%14i\n", i, ratingCounters[i]); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_04/exercise_06_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_04.c 3 | * 4 | * Program that calculates the average of some values taken from an array. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | float values[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; 14 | float sum, average; 15 | int n; 16 | 17 | sum = 0.0; 18 | for (n = 0; n < 10; ++n) 19 | sum += values[n]; 20 | average = sum / 10; 21 | 22 | printf("%f", average); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_05/exercise_06_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_05.c 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 10 | int i, j; 11 | 12 | for ( j = 0; j < 10; ++j ) 13 | for ( i = 0; i < j; ++i ) 14 | numbers[j] += numbers[i]; 15 | 16 | for ( j = 0; j < 10; ++j ) 17 | printf("%i ", numbers[j]); 18 | 19 | printf("\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_06/exercise_06_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_03.c 3 | * 4 | * Program that calculates and prints the first 15 Fibonacci numbers. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int fibA, fibB, fibC, i; 14 | 15 | fibA = 0; // by definition 16 | printf("%i\n", fibA); 17 | 18 | fibB = 1; // by definition 19 | printf("%i\n", fibB); 20 | 21 | for (i = 2; i < 15; ++i) 22 | { 23 | // calculate and then print the next number 24 | fibC = fibA + fibB; 25 | printf("%i\n", fibC); 26 | 27 | // update the numbers as preparation for calculation of next number 28 | fibA = fibB; 29 | fibB = fibC; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_07/exercise_06_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_07.c 3 | * 4 | * Program that implements the Sieve of Eratosthenes to generate prime numbers. 5 | * 6 | * The steps are not quite the same as in the book because I did not fully 7 | * understand the explanation of the algorithm. Step 4 from the book should in 8 | * my opinion be the last step. 9 | * 10 | * Auhthor: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 11 | */ 12 | 13 | #include 14 | 15 | int main (void) 16 | { 17 | int n = 150; 18 | int i, j; 19 | 20 | // Step 1: Define an array of integers P and set all elements to 0. 21 | int P[n]; 22 | for(i = 0; i < n; i++) 23 | P[i] = 0; 24 | 25 | // Step 2: Set i to 2. 26 | i = 2; 27 | j = 2; 28 | 29 | // Step 3: The algorithm shall terminate when when i > n. 30 | while(i < n) 31 | { 32 | // Step 4: For all j such that i x j <= n set element number i * j in P 33 | // to 1. 34 | while(i * j <= n) 35 | { 36 | P[i * j] = 1; 37 | j++; 38 | } 39 | 40 | // Step 5: Add 1 to i and goto step 3 41 | i++; 42 | j = i; 43 | 44 | } 45 | 46 | // Step 6: If element i in P is 0 then i is prime, except for i = 0 and 47 | // i = 1. 48 | for (i = 2; i < n; i++) 49 | { 50 | if (P[i] == 0) 51 | printf("%d ", i); 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Exercises/Chapter_06/Exercise_08/exercise_06_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_06_08.c 3 | * 4 | * Program that calculates the dot product of two vectors. 5 | * 6 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 7 | */ 8 | 9 | #include 10 | 11 | int main (void) 12 | { 13 | int i, length, dotProduct; 14 | 15 | printf("Enter length of the vectors"); 16 | scanf("%d", &length); 17 | 18 | int vector1[length]; 19 | printf("Enter values for vector 1: "); 20 | for(i = 0; i < length; ++i) 21 | scanf("%d", &vector1[i]); 22 | 23 | int vector2[length]; 24 | printf("Enter values for vector 2: "); 25 | for(i = 0; i < length; ++i) 26 | scanf("%d", &vector2[i]); 27 | 28 | dotProduct = 0; 29 | for(i = 0; i < length; i++) 30 | dotProduct += vector1[i] * vector2[i]; 31 | 32 | printf("The dot product of the two vectors is %d", dotProduct); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Chapter_07.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 7 # 2 | ## 1. ## 3 | Test run of the seventeen programs presented in this chapter. 4 | - [program_07_01.c](Exercise_01/Program_07_01/program_07_01.c) 5 | - [program_07_02.c](Exercise_01/Program_07_02/program_07_02.c) 6 | - [program_07_03.c](Exercise_01/Program_07_03/program_07_03.c) 7 | - [program_07_04.c](Exercise_01/Program_07_04/program_07_04.c) 8 | - [program_07_05.c](Exercise_01/Program_07_05/program_07_05.c) 9 | - [program_07_06.c](Exercise_01/Program_07_06/program_07_06.c) 10 | - [program_07_07.c](Exercise_01/Program_07_07/program_07_07.c) 11 | - [program_07_08.c](Exercise_01/Program_07_08/program_07_08.c) 12 | - [program_07_09.c](Exercise_01/Program_07_09/program_07_09.c) 13 | - [program_07_10.c](Exercise_01/Program_07_10/program_07_10.c) 14 | - [program_07_11.c](Exercise_01/Program_07_11/program_07_11.c) 15 | - [program_07_12.c](Exercise_01/Program_07_12/program_07_12.c) 16 | - [program_07_13.c](Exercise_01/Program_07_13/program_07_13.c) 17 | - [program_07_14.c](Exercise_01/Program_07_14/program_07_14.c) 18 | - [program_07_15.c](Exercise_01/Program_07_15/program_07_15.c) 19 | - [program_07_16.c](Exercise_01/Program_07_16/program_07_16.c) 20 | - [program_07_17.c](Exercise_01/Program_07_17/program_07_17.c) 21 | 22 | ## 3. ## 23 | Variant of program 7.8, epsilon is given as an argument to the function. A couple of different epsilon values are tested and it can be seen that smaller values of epsilon gives higher accuracy in the square root calculations. 24 | - [exercise_07_03.c](Exercise_03/exercise_07_03.c) 25 | 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_01/program_07_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_01.c - Writing a Function in C 3 | */ 4 | 5 | #include 6 | 7 | void printMessage (void) 8 | { 9 | printf ("Programming is fun.\n"); 10 | } 11 | 12 | int main (void) 13 | { 14 | printMessage (); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_02/program_07_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_02.c - More on Calling Functions 3 | */ 4 | 5 | #include 6 | 7 | void printMessage (void) 8 | { 9 | printf ("Programming is fun.\n"); 10 | } 11 | 12 | int main (void) 13 | { 14 | printMessage (); 15 | printMessage (); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_03/program_07_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_03.c - More on Calling Functions 3 | */ 4 | 5 | #include 6 | 7 | void printMessage (void) 8 | { 9 | printf ("Programming is fun.\n"); 10 | } 11 | 12 | int main (void) 13 | { 14 | int i; 15 | 16 | for (i = 1; i <= 5; ++i) 17 | printMessage (); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_04/program_07_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_04.c - Calculating the nth Triangular Number 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to calculate nth triangular number. 9 | */ 10 | 11 | void calculateTriangularNumber (int n) 12 | { 13 | int i, triangularNumber = 0; 14 | 15 | for (i = 1; i <= n; ++i) 16 | triangularNumber += i; 17 | 18 | printf("Triangular number %i is %i\n", n, triangularNumber); 19 | } 20 | 21 | int main (void) 22 | { 23 | calculateTriangularNumber (10); 24 | calculateTriangularNumber (20); 25 | calculateTriangularNumber (50); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_05/program_07_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_05.c - Revising the Program to find the Greatest Common Divisor 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to find the greatest common divisor of two nonnegative integer 9 | * values. 10 | */ 11 | 12 | void gcd (int u, int v) 13 | { 14 | int temp; 15 | 16 | printf ("The gcd of %i and %i is ", u, v); 17 | 18 | while (v != 0) 19 | { 20 | temp = u % v; 21 | u = v; 22 | v = temp; 23 | } 24 | 25 | printf ("%i\n", u); 26 | } 27 | 28 | int main (void) 29 | { 30 | gcd (150, 35); 31 | gcd (1026, 405); 32 | gcd (83, 240); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_06/program_07_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_06.c - Finding the Greatest Common Divisor and Returning the 3 | * Results 4 | */ 5 | 6 | #include 7 | 8 | /* 9 | * Function to find the greatest common divisor of two nonnegative integer 10 | * values and to return the result. 11 | */ 12 | 13 | int gcd (int u, int v) 14 | { 15 | int temp; 16 | 17 | while (v != 0) 18 | { 19 | temp = u % v; 20 | u = v; 21 | v = temp; 22 | } 23 | 24 | return u; 25 | } 26 | 27 | int main (void) 28 | { 29 | int result; 30 | 31 | result = gcd (150, 35); 32 | printf("The gcd of 150 and 35 is %i\n", result); 33 | 34 | result = gcd (1026, 405); 35 | printf("The gcd of 1026 and 405 is %i\n", result); 36 | 37 | result = gcd (83, 240); 38 | printf("The gcd of 83 and 240 is %i\n", result); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_07/program_07_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_07.c - Calculating the Absolute Value 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to calculate the absolute value. 9 | */ 10 | 11 | float absoluteValue (float x) 12 | { 13 | if (x < 0) 14 | x = -x; 15 | 16 | return x; 17 | } 18 | 19 | int main (void) 20 | { 21 | float f1 = -15.5, f2 = 20.0, f3 = -5.0; 22 | int i1 = -716; 23 | float result; 24 | 25 | result = absoluteValue (f1); 26 | printf ("result = %.2f\n", result); 27 | printf ("f1 = %.2f\n", f1); 28 | 29 | result = absoluteValue (f2) + absoluteValue (f3); 30 | printf ("result = %.2f\n", result); 31 | 32 | result = absoluteValue ( (float) i1 ); 33 | printf ("result = %.2f\n", result); 34 | 35 | result = absoluteValue (i1); 36 | printf ("result = %.2f\n", result); 37 | 38 | printf ("%.2f\n", absoluteValue (-6.0) / 4); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_08/program_07_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_08.c - Calculating the Square Root of a Number 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to calculate the absolute value of a number. 9 | */ 10 | 11 | float absoluteValue (float x) 12 | { 13 | if (x < 0) 14 | x = -x; 15 | 16 | return x; 17 | } 18 | 19 | /* 20 | * Function to compute the square root of a number. 21 | */ 22 | 23 | float squareRoot (float x) 24 | { 25 | const float epsilon = .00001; 26 | float guess = 1.0; 27 | 28 | while (absoluteValue (guess * guess - x) >= epsilon) 29 | guess = (x / guess + guess) / 2.0; 30 | 31 | return guess; 32 | } 33 | 34 | int main (void) 35 | { 36 | printf ("squareRoot (2.0) = %f\n", squareRoot (2.0)); 37 | printf ("squareRoot (144.0) = %f\n", squareRoot (144.0)); 38 | printf ("squareRoot (17.5) = %f\n", squareRoot (17.5)); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_09/program_07_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_09.c - Finding the Minimum Value in an Array 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to find the minimum value in an array. 9 | */ 10 | 11 | int minimum (int values[10]) 12 | { 13 | int minValue, i; 14 | 15 | minValue = values[0]; 16 | 17 | for ( i = 1; i < 10; ++i ) 18 | if ( values[i] < minValue ) 19 | minValue = values[i]; 20 | 21 | return minValue; 22 | } 23 | 24 | int main (void) 25 | { 26 | int scores[10], i, minScore; 27 | int minimum (int values[10]); 28 | 29 | printf ("Enter 10 scores\n"); 30 | 31 | for ( i = 0; i < 10; ++i ) 32 | scanf ("%i", &scores[i]); 33 | 34 | minScore = minimum (scores); 35 | printf ("\nMinimum score is %i\n", minScore); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_10/program_07_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_10.c - Revising the Function to Find the Minimum Value in an Array 3 | */ 4 | 5 | #include 6 | 7 | /* 8 | * Function to find the minimum value in an array. 9 | */ 10 | 11 | int minimum (int values[], int numberOfElements) 12 | { 13 | int minValue, i; 14 | 15 | minValue = values[0]; 16 | 17 | for ( i = 1; i < numberOfElements; ++i ) 18 | if ( values[i] < minValue ) 19 | minValue = values[i]; 20 | 21 | return minValue; 22 | } 23 | 24 | int main (void) 25 | { 26 | int array1[5] = { 157, -28, -37, 26, 10 }; 27 | int array2[7] = { 12, 45, 1, 10, 5, 3, 22 }; 28 | int minimum (int values[], int numberOfElements); 29 | 30 | printf ("array1 minimum %i\n", minimum (array1, 5)); 31 | printf ("array1 minimum %i\n", minimum (array2, 7)); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_11/program_07_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_11.c - Changing Array Elements in Functions 3 | */ 4 | 5 | #include 6 | 7 | void multiplyBy2 (float array[], int n) 8 | { 9 | int i; 10 | 11 | for ( i = 0; i < n; ++i ) 12 | array[i] *= 2; 13 | } 14 | 15 | int main (void) 16 | { 17 | float floatVals[4] = { 1.2f, -3.7f, 6.2f, 8.55f }; 18 | int i; 19 | void multiplyBy2 (float array[], int n); 20 | 21 | multiplyBy2 (floatVals, 4); 22 | 23 | for ( i = 0; i < 4; ++i ) 24 | printf ("%.2f ", floatVals[i]); 25 | 26 | printf ("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_12/program_07_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_12.c - Sorting an Array of Integers into Ascending Order 3 | */ 4 | 5 | #include 6 | 7 | void sort (int a[], int n) 8 | { 9 | int i, j, temp; 10 | 11 | for (i = 0; i < n - 1; ++i) 12 | for (j = i + 1; j < n; ++j) 13 | if (a[i] > a[j]) 14 | { 15 | temp = a[i]; 16 | a[i] = a[j]; 17 | a[j] = temp; 18 | } 19 | } 20 | 21 | int main (void) 22 | { 23 | int i; 24 | int array[16] = 25 | { 34, -5, 6, 0, 12, 100, 56, 22, 44, -3, -9, 12, 17, 22, 6, 11 }; 26 | void sort (int a[], int n); 27 | 28 | printf ("The array before the sort:\n"); 29 | for (i = 0; i < 16; ++i) 30 | printf ("%i ", array[i]); 31 | printf ("\n"); 32 | 33 | sort (array, 16); 34 | 35 | printf ("The array after the sort:\n"); 36 | for (i = 0; i < 16; ++i) 37 | printf ("%i ", array[i]); 38 | printf ("\n"); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_13/program_07_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_13.c - Using Multidimensional Arrays and Functions 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | void scalarMultiply (int matrix[3][5], int scalar); 10 | void displayMatrix (int matrix [3][5]); 11 | int sampleMatrix[3][5] = 12 | { 13 | { 7, 16, 55, 13, 12}, 14 | { 12, 10, 52, 0, 7}, 15 | { -2, 1, 2, 4, 9} 16 | }; 17 | 18 | printf("Original matrix:\n"); 19 | displayMatrix(sampleMatrix); 20 | 21 | scalarMultiply(sampleMatrix, 2); 22 | 23 | printf("\nMultiplied by 2:\n"); 24 | displayMatrix(sampleMatrix); 25 | 26 | scalarMultiply(sampleMatrix, -1); 27 | 28 | printf("\nThen multiplied by -1:\n"); 29 | displayMatrix(sampleMatrix); 30 | 31 | return 0; 32 | } 33 | 34 | /* 35 | * Function to multiply a 3 x 5 array by a scalar. 36 | */ 37 | 38 | void scalarMultiply (int matrix[3][5], int scalar) 39 | { 40 | int row, column; 41 | 42 | for ( row = 0; row < 3; ++row ) 43 | for ( column = 0; column < 5; ++column ) 44 | matrix[row][column] *= scalar; 45 | } 46 | 47 | /* 48 | * Function to print content of a 3 x 5 array. 49 | */ 50 | 51 | void displayMatrix (int matrix[3][5]) 52 | { 53 | int row, column; 54 | 55 | for ( row = 0; row < 3; ++row ) 56 | { 57 | for ( column = 0; column < 5; ++column ) 58 | printf("%5i", matrix[row][column]); 59 | 60 | printf ("\n"); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_14/program_07_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_14.c - Multidimensional Variable-Length Arrays 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | void scalarMultiply (int nRows, int nCols, int matrix[nRows][nCols], 10 | int scalar); 11 | void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols]); 12 | int sampleMatrix[3][5] = 13 | { 14 | { 7, 16, 55, 13, 12 }, 15 | { 12, 10, 52, 0, 7 }, 16 | { -2, 1, 2, 4, 9 } 17 | }; 18 | 19 | printf("Original matrix:\n"); 20 | displayMatrix(3, 5, sampleMatrix); 21 | 22 | scalarMultiply(3, 5, sampleMatrix, 2); 23 | 24 | printf("\nMultiplied by 2:\n"); 25 | displayMatrix(3, 5, sampleMatrix); 26 | 27 | scalarMultiply(3, 5, sampleMatrix, -1); 28 | 29 | printf("\nThen multiplied by -1:\n"); 30 | displayMatrix(3, 5, sampleMatrix); 31 | 32 | return 0; 33 | } 34 | 35 | /* 36 | * Function to multiply a matrix by a scalar. 37 | */ 38 | 39 | void scalarMultiply (int nRows, int nCols, int matrix[nRows][nCols], 40 | int scalar) 41 | { 42 | int row, column; 43 | 44 | for ( row = 0; row < nRows; ++row ) 45 | for ( column = 0; column < nCols; ++column ) 46 | matrix[row][column] *= scalar; 47 | } 48 | 49 | /* 50 | * Function to print content of a two dimensional array. 51 | */ 52 | 53 | void displayMatrix (int nRows, int nCols, int matrix[nRows][nCols]) 54 | { 55 | int row, column; 56 | 57 | for ( row = 0; row < nRows; ++row ) 58 | { 59 | for ( column = 0; column < nCols; ++column ) 60 | printf("%5i", matrix[row][column]); 61 | 62 | printf ("\n"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_15/program_07_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_15.c - Converting a Positive Integer to Another Base 3 | */ 4 | 5 | #include 6 | 7 | int convertedNumber[64]; 8 | long int numberToConvert; 9 | int base; 10 | int digit = 0; 11 | 12 | void getNumberAndBase (void) 13 | { 14 | printf ("Number to be converted? "); 15 | scanf ("%li", &numberToConvert); 16 | 17 | printf ("Base? "); 18 | scanf ("%i", &base); 19 | 20 | if (base < 2 || base > 16) 21 | { 22 | printf ("Bad base - must be between 2 and 16\n"); 23 | base = 10; 24 | } 25 | } 26 | 27 | void convertNumber (void) 28 | { 29 | do 30 | { 31 | convertedNumber[digit] = numberToConvert % base; 32 | ++digit; 33 | numberToConvert /= base; 34 | } while (numberToConvert != 0); 35 | } 36 | 37 | void displayConvertedNumber (void) 38 | { 39 | const char baseDigits[16] = 40 | { '0', '1', '2', '3', '4', '5', '6', '7', 41 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; 42 | int nextDigit; 43 | 44 | printf ("Converted number = "); 45 | 46 | for ( --digit; digit >= 0; --digit ) 47 | { 48 | nextDigit = convertedNumber[digit]; 49 | printf ("%c", baseDigits[nextDigit]); 50 | } 51 | 52 | printf ("\n"); 53 | } 54 | 55 | int main (void) 56 | { 57 | void getNumberAndBase (void), convertNumber (void), 58 | displayConvertedNumber (void); 59 | 60 | getNumberAndBase(); 61 | convertNumber (); 62 | displayConvertedNumber (); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_16/program_07_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_16.c - Illustrating Static and Automatic Variables 3 | */ 4 | 5 | #include 6 | 7 | void auto_static (void) 8 | { 9 | int autoVar = 1; 10 | static int staticVar = 1; 11 | 12 | printf ("automatic = %i, static = %i\n", autoVar, staticVar); 13 | 14 | ++autoVar; 15 | ++staticVar; 16 | } 17 | 18 | int main (void) 19 | { 20 | 21 | int i; 22 | void auto_static (void); 23 | 24 | for ( i = 0; i < 5; ++i) 25 | auto_static (); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_01/Program_07_17/program_07_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_07_17.c - Calculating Factorials Recursively 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | unsigned int j; 10 | unsigned long int factorial (unsigned int n); 11 | 12 | for ( j = 0; j < 11; ++j ) 13 | printf( "%2u! = %lu\n", j, factorial (j)); 14 | 15 | return 0; 16 | } 17 | 18 | /* 19 | * Recursive function to calculate the factorial of a positive integer. 20 | */ 21 | 22 | unsigned long int factorial (unsigned int n) 23 | { 24 | unsigned long int result; 25 | 26 | if ( n == 0) 27 | result = 1; 28 | else 29 | result = n * factorial (n - 1); 30 | 31 | return result; 32 | } 33 | -------------------------------------------------------------------------------- /Exercises/Chapter_07/Exercise_03/exercise_07_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_07_03.c 3 | * 4 | * Variant of program 7.8, epsilon is now given as an argument to the function. 5 | * 6 | * Have also changed all numbers from float to double precision to better be 7 | * able to see the effects of different epsilon values. 8 | * 9 | * Author: Henrik Samuelsson, henrik.samuelsson(at)gmail.com 10 | */ 11 | 12 | #include 13 | 14 | /* 15 | * Function to calculate the absolute value of a number. 16 | */ 17 | 18 | double absoluteValue (double x) 19 | { 20 | if (x < 0) 21 | x = -x; 22 | 23 | return x; 24 | } 25 | 26 | /* 27 | * Function to compute the square root of a number. 28 | */ 29 | 30 | double squareRoot (double x, double epsilon) 31 | { 32 | double guess = 1.0; 33 | 34 | while (absoluteValue (guess * guess - x) >= epsilon) 35 | guess = (x / guess + guess) / 2.0; 36 | 37 | return guess; 38 | } 39 | 40 | int main (void) 41 | { 42 | printf ("squareRoot of 2.0 with epsilon set to 0.1 = %.16f\n", 43 | squareRoot (2.0, 0.1)); 44 | printf ("squareRoot of 2.0 with epsilon set to 0.001 = %.16f\n", 45 | squareRoot (2.0, 0.001)); 46 | printf ("squareRoot of 2.0 with epsilon set to 0.00001 = %.16f\n", 47 | squareRoot (2.0, 0.00001)); 48 | printf ("squareRoot of 2.0 with epsilon set to 0.0000001 = %.16f\n", 49 | squareRoot (2.0, 0.0000001)); 50 | printf ("squareRoot of 2.0 with epsilon set to 0.000000001 = %.16f\n", 51 | squareRoot (2.0, 0.000000001)); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Chapter_08.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 8 # 2 | ## 1. ## 3 | Test run of the seven programs presented in this chapter. 4 | - [program_08_01.c](Exercise_01/Program_08_01/program_08_01.c) 5 | - [program_08_02.c](Exercise_01/Program_08_02/program_08_02.c) 6 | - [program_08_03.c](Exercise_01/Program_08_03/program_08_03.c) 7 | - [program_08_04.c](Exercise_01/Program_08_04/program_08_04.c) 8 | - [program_08_05.c](Exercise_01/Program_08_05/program_08_05.c) 9 | - [program_08_06.c](Exercise_01/Program_08_06/program_08_06.c) 10 | - [program_08_07.c](Exercise_01/Program_08_07/program_08_07.c) 11 | 12 | 13 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_01/program_08_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_01.c - Illustrating a Structure 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct date 10 | { 11 | int month; 12 | int day; 13 | int year; 14 | }; 15 | 16 | struct date today; 17 | 18 | today.month = 9; 19 | today.day = 25; 20 | today.year = 2015; 21 | 22 | printf ("Today's date is %i/%i/%.2i.\n", today.month, today.day, 23 | today.year % 100); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_02/program_08_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_02.c - Determining Tomorrow's Date 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct date 10 | { 11 | int month; 12 | int day; 13 | int year; 14 | }; 15 | 16 | struct date today, tomorrow; 17 | 18 | const int daysPerMonth[12] = 19 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 20 | 21 | printf ("Enter today's data (mm dd yyyy): "); 22 | scanf ("%i%i%i", &today.month, &today.day, &today.year); 23 | 24 | if (today.day != daysPerMonth[today.month - 1]) 25 | { 26 | tomorrow.day = today.day + 1; 27 | tomorrow.month = today.month; 28 | tomorrow.year = today.year; 29 | } 30 | else if (today.month == 12) // end of year 31 | { 32 | tomorrow.day = 1; 33 | tomorrow.month = 1; 34 | tomorrow.year = today.year + 1; 35 | } 36 | else // end of month 37 | { 38 | tomorrow.day = 1; 39 | tomorrow.month = today.month + 1; 40 | tomorrow.year = today.year; 41 | } 42 | 43 | printf ("Tomorrow's date is %i/%i/%.2i.\n", tomorrow.month, tomorrow.day, 44 | tomorrow.year % 100); 45 | } 46 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_03/program_08_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_03.c - Revising the Program to Determine Tomorrow's Date 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | struct date 9 | { 10 | int month; 11 | int day; 12 | int year; 13 | }; 14 | 15 | int main (void) 16 | { 17 | struct date today, tomorrow; 18 | 19 | int numberOfDays (struct date d); 20 | 21 | printf ("Enter today's data (mm dd yyyy): "); 22 | scanf ("%i%i%i", &today.month, &today.day, &today.year); 23 | 24 | if (today.day != numberOfDays(today)) 25 | { 26 | tomorrow.day = today.day + 1; 27 | tomorrow.month = today.month; 28 | tomorrow.year = today.year; 29 | } 30 | else if (today.month == 12) // end of year 31 | { 32 | tomorrow.day = 1; 33 | tomorrow.month = 1; 34 | tomorrow.year = today.year + 1; 35 | } 36 | else // end of month 37 | { 38 | tomorrow.day = 1; 39 | tomorrow.month = today.month + 1; 40 | tomorrow.year = today.year; 41 | } 42 | 43 | printf ("Tomorrow's date is %i/%i/%.2i.\n", tomorrow.month, tomorrow.day, 44 | tomorrow.year % 100); 45 | } 46 | 47 | /* 48 | * Function to find the number of days in a month. 49 | */ 50 | 51 | int numberOfDays (struct date d) 52 | { 53 | int days; 54 | bool isLeapYear (struct date d); 55 | 56 | const int daysPerMonth[12] = 57 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 58 | 59 | if ( isLeapYear (d) == true && d.month == 2 ) 60 | days = 29; 61 | else 62 | days = daysPerMonth[d.month - 1]; 63 | 64 | return days; 65 | } 66 | 67 | /* 68 | * Function to determine if it's a leap year. 69 | */ 70 | 71 | bool isLeapYear (struct date d) 72 | { 73 | bool leapYearFlag; 74 | 75 | if ( ( d.year % 4 == 0 && d.year % 100 != 0 ) || d.year % 400 == 0 ) 76 | leapYearFlag = true; 77 | else 78 | leapYearFlag = false; 79 | 80 | return leapYearFlag; 81 | } 82 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_04/program_08_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_04.c - Revising the Program to Determine Tomorrow's Date, 3 | * Version 2 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | struct date 10 | { 11 | int month; 12 | int day; 13 | int year; 14 | }; 15 | 16 | /* 17 | * Function to calculate tomorrow's date 18 | */ 19 | 20 | struct date dateUpdate (struct date today) 21 | { 22 | struct date tomorrow; 23 | int numberOfDays (struct date d); 24 | 25 | if (today.day != numberOfDays (today)) 26 | { 27 | tomorrow.day = today.day + 1; 28 | tomorrow.month = today.month; 29 | tomorrow.year = today.year; 30 | } 31 | else if (today.month == 12) // end of year 32 | { 33 | tomorrow.day = 1; 34 | tomorrow.month = 1; 35 | tomorrow.year = today.year + 1; 36 | } 37 | else // end of month 38 | { 39 | tomorrow.day = 1; 40 | tomorrow.month = today.month + 1; 41 | tomorrow.year = today.year; 42 | } 43 | 44 | return tomorrow; 45 | } 46 | 47 | /* 48 | * Function to find the number of days in a month. 49 | */ 50 | 51 | int numberOfDays (struct date d) 52 | { 53 | int days; 54 | bool isLeapYear (struct date d); 55 | 56 | const int daysPerMonth[12] = 57 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 58 | 59 | if (isLeapYear (d) == true && d.month == 2) 60 | days = 29; 61 | else 62 | days = daysPerMonth[d.month - 1]; 63 | 64 | return days; 65 | } 66 | 67 | /* 68 | * Function to determine if it's a leap year. 69 | */ 70 | 71 | bool isLeapYear (struct date d) 72 | { 73 | bool leapYearFlag; 74 | 75 | if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0) 76 | leapYearFlag = true; 77 | else 78 | leapYearFlag = false; 79 | 80 | return leapYearFlag; 81 | } 82 | 83 | int main (void) 84 | { 85 | struct date dateUpdate (struct date today); 86 | struct date thisDay, nextDay; 87 | 88 | printf ("Enter today's data (mm dd yyyy): "); 89 | scanf ("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year); 90 | 91 | nextDay = dateUpdate (thisDay); 92 | 93 | printf ("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month, nextDay.day, 94 | nextDay.year % 100); 95 | } 96 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_05/program_08_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_05.c - Updating the Time by One Second 3 | */ 4 | 5 | #include 6 | 7 | struct time 8 | { 9 | int hour; 10 | int minutes; 11 | int seconds; 12 | }; 13 | 14 | int main (void) 15 | { 16 | struct time timeUpdate (struct time now); 17 | struct time currentTime, nextTime; 18 | 19 | printf ("Enter the time (hh:mm:ss): "); 20 | scanf ("%i:%i:%i", ¤tTime.hour, ¤tTime.minutes, 21 | ¤tTime.seconds); 22 | 23 | nextTime = timeUpdate (currentTime); 24 | 25 | printf ("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour, nextTime.minutes, 26 | nextTime.seconds); 27 | 28 | return 0; 29 | } 30 | 31 | /* 32 | * Function to update the time by one second. 33 | */ 34 | 35 | struct time timeUpdate (struct time now) 36 | { 37 | ++now.seconds; 38 | 39 | if ( now.seconds == 60 ) // if next minute 40 | { 41 | now.seconds = 0; 42 | ++now.minutes; 43 | 44 | if ( now.minutes == 60 ) // if next hour 45 | { 46 | now.minutes = 0; 47 | ++now.hour; 48 | 49 | if ( now.hour == 24 ) // if next day 50 | { 51 | now.hour = 0; 52 | } 53 | } 54 | } 55 | 56 | return now; 57 | } 58 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_06/program_08_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_06.c - Illustrating Arrays of Structures 3 | */ 4 | 5 | #include 6 | 7 | struct time 8 | { 9 | int hour; 10 | int minute; 11 | int second; 12 | }; 13 | 14 | int main (void) 15 | { 16 | struct time timeUpdate (struct time now); 17 | struct time testTime[5] = 18 | { 19 | { 11, 59, 59 }, 20 | { 12, 0, 0 }, 21 | { 1, 29, 59 }, 22 | { 23, 59, 59 }, 23 | { 19, 12, 27 } }; 24 | 25 | int i; 26 | 27 | for (i = 0; i < 5; ++i) 28 | { 29 | printf ("Time is %.2i:%.2i:%.2i", testTime[i].hour, testTime[i].minute, 30 | testTime[i].second); 31 | 32 | testTime[i] = timeUpdate (testTime[i]); 33 | 34 | printf (" ...one second later it's %.2i:%.2i:%.2i\n", testTime[i].hour, 35 | testTime[i].minute, testTime[i].second); 36 | } 37 | return 0; 38 | } 39 | 40 | /* 41 | * Function to update the time by one second. 42 | */ 43 | 44 | struct time timeUpdate (struct time now) 45 | { 46 | ++now.second; 47 | 48 | if (now.second == 60) // if next minute 49 | { 50 | now.second = 0; 51 | ++now.minute; 52 | 53 | if (now.minute == 60) // if next hour 54 | { 55 | now.minute = 0; 56 | ++now.hour; 57 | 58 | if (now.hour == 24) // if next day 59 | { 60 | now.hour = 0; 61 | } 62 | } 63 | } 64 | 65 | return now; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Exercises/Chapter_08/Exercise_01/Program_08_07/program_08_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_08_07.c - Illustrating Structures and Arrays 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int i; 10 | 11 | struct month 12 | { 13 | int numberOfDays; 14 | char name[3]; 15 | }; 16 | 17 | const struct month months[12] = 18 | { 19 | { 31, {'J', 'a', 'n'} }, 20 | { 28, {'F', 'e', 'b'} }, 21 | { 31, {'M', 'a', 'r'} }, 22 | { 30, {'A', 'p', 'r'} }, 23 | { 31, {'M', 'a', 'y'} }, 24 | { 30, {'J', 'u', 'n'} }, 25 | { 31, {'J', 'u', 'l'} }, 26 | { 31, {'A', 'u', 'g'} }, 27 | { 30, {'S', 'e', 'p'} }, 28 | { 31, {'O', 'c', 't'} }, 29 | { 30, {'N', 'o', 'v'} }, 30 | { 31, {'D', 'e', 'c'} }, 31 | }; 32 | 33 | printf("Month Number of Days\n"); 34 | printf("----- --------------\n"); 35 | 36 | for ( i = 0; i < 12; ++i ) 37 | printf (" %c%c%c %i\n", 38 | months[i].name[0], 39 | months[i].name[1], 40 | months[i].name[2], 41 | months[i].numberOfDays); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Chapter_09.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 9 # 2 | ## 1. ## 3 | Test run of the eleven programs presented in this chapter. 4 | - [program_09_02.c](Exercise_01/Program_09_02/program_09_02.c) 5 | - [program_09_03.c](Exercise_01/Program_09_03/program_09_03.c) 6 | - [program_09_04.c](Exercise_01/Program_09_04/program_09_04.c) 7 | - [program_09_05.c](Exercise_01/Program_09_05/program_09_05.c) 8 | - [program_09_06.c](Exercise_01/Program_09_06/program_09_06.c) 9 | 10 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Exercise_01/Program_09_02/program_09_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_09_02.c - Counting the Characters in a String 3 | */ 4 | 5 | #include 6 | 7 | int stringLength (const char string[]) 8 | { 9 | int count = 0; 10 | 11 | while ( string[count] != '\0' ) 12 | ++count; 13 | 14 | return count; 15 | } 16 | 17 | int main (void) 18 | { 19 | int stringLength (const char string[]); 20 | const char word1[] = {'a', 's', 't', 'e', 'r', '\0'}; 21 | const char word2[] = {'a', 't', '\0'}; 22 | const char word3[] = {'a', 'w', 'e', '\0'}; 23 | 24 | printf("%i %i %i\n", 25 | stringLength (word1), 26 | stringLength (word2), 27 | stringLength (word3)); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Exercise_01/Program_09_03/program_09_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_09_03.c - Concatenating Character Strings 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | void concat (char result[], const char str1[], const char str2[]); 10 | const char s1[] = { "Test " }; 11 | const char s2[] = { "works." }; 12 | char s3[20]; 13 | 14 | concat (s3, s1, s2); 15 | 16 | printf ("%s\n", s3); 17 | 18 | return 0; 19 | } 20 | 21 | /* 22 | * Function to concatenate two character strings. 23 | */ 24 | void concat (char result[], const char str1[], const char str2[]) 25 | { 26 | int i, j; 27 | 28 | // copy str1 to result 29 | for ( i = 0; str1[i] != '\0'; ++i) 30 | result[i] = str1[i]; 31 | 32 | // copy str2 to result 33 | for ( j = 0; str2[j] != '\0'; ++j) 34 | result[i + j] = str2[j]; 35 | 36 | // add a null character to the concatenated string to terminate it 37 | result[i + j] = '\0'; 38 | } 39 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Exercise_01/Program_09_04/program_09_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_09_04.c - Testing strings for equality 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | // Function to determine if two strings are equal 9 | bool equalStrings (const char s1[], const char s2[]) 10 | { 11 | int i = 0; 12 | bool areEqual; 13 | 14 | while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') 15 | ++i; 16 | 17 | if (s1[i] == '\0' && s2[i] == '\0') 18 | areEqual = true; 19 | else 20 | areEqual = false; 21 | 22 | return areEqual; 23 | } 24 | 25 | int main (void) 26 | { 27 | bool equalStrings (const char s1[], const char s2[]); 28 | const char stra[] = "string compare test"; 29 | const char strb[] = "string"; 30 | 31 | printf ("%i\n", equalStrings(stra, strb)); 32 | printf ("%i\n", equalStrings(stra, stra)); 33 | printf ("%i\n", equalStrings(strb, "string")); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Exercise_01/Program_09_05/program_09_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_09_05.c - Reading Strings with scanf() 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | char s1[81], s2[81], s3[81]; 10 | 11 | printf ("Enter text:\n"); 12 | 13 | scanf ("%s%s%s", s1, s2, s3); 14 | 15 | printf("\ns1 = %s\ns2 = %s\ns3 = %s", s1, s2, s3); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_09/Exercise_01/Program_09_06/program_09_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_09_06.c - Reading Lines of Data 3 | */ 4 | #include 5 | 6 | int main (void) 7 | { 8 | int i; 9 | char line[81]; 10 | void readLine (char buffer[]); 11 | 12 | for ( i = 0; i < 3; ++i ) 13 | { 14 | readLine (line); 15 | printf ("%s\n\n", line); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | /* 22 | * Function to read a line of text from the terminal. 23 | */ 24 | void readLine (char buffer[]) 25 | { 26 | char character; 27 | int i = 0; 28 | 29 | do 30 | { 31 | character = getchar (); 32 | buffer[i] = character; 33 | ++i; 34 | } 35 | while ( character != '\n' ); 36 | 37 | buffer[i - 1] = '\0'; 38 | } 39 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Chapter_10.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 10 # 2 | ## 1. ## 3 | Test run of the fifteen programs presented in this chapter: 4 | - [Program 10.1](Exercise_01/Program_10_01/program_10_01.c) 5 | - [Program 10.2](Exercise_01/Program_10_02/program_10_02.c) 6 | - [Program 10.3](Exercise_01/Program_10_03/program_10_03.c) 7 | - [Program 10.4](Exercise_01/Program_10_04/program_10_04.c) 8 | - [Program 10.5](Exercise_01/Program_10_05/program_10_05.c) 9 | - [Program 10.6](Exercise_01/Program_10_06/program_10_06.c) 10 | - [Program 10.7](Exercise_01/Program_10_07/program_10_07.c) 11 | - [Program 10.8](Exercise_01/Program_10_08/program_10_08.c) 12 | - [Program 10.9](Exercise_01/Program_10_09/program_10_09.c) 13 | - [Program 10.10](Exercise_01/Program_10_10/program_10_10.c) 14 | - [Program 10.11](Exercise_01/Program_10_11/program_10_11.c) 15 | - [Program 10.12](Exercise_01/Program_10_12/program_10_12.c) 16 | - [Program 10.13](Exercise_01/Program_10_13/program_10_13.c) 17 | - [Program 10.14](Exercise_01/Program_10_14/program_10_14.c) 18 | - [Program 10.15](Exercise_01/Program_10_15/program_10_15.c) 19 | 20 | ## 2. ## 21 | Implementation of a function that inserts an entry in a linked list and complementing code to test the function: 22 | - [Exercise 10.2](Exercise_02/exercise_10_02.c) 23 | 24 | ## 3. ## 25 | Implementation of a function that inserts an entry in a linked list. The list have a header entry that points to the first actual element in the list. This header makes it possible to use the add function to insert new entries anywhere in the list. 26 | 27 | The function and complementing code that tests the function: 28 | - [Exercise 10.3](Exercise_03/exercise_10_03.c) 29 | 30 | ## 4. ## 31 | Implementation of a function that removes an entry in a linked list. The argument to the function is the element *before* the element that shall be removed. The list is single linked so we can not use the element that shall be removed as argument since we do not know what links to the element to be removed. 32 | 33 | The function and complementing code that tests the function: 34 | - [Exercise 10.4](Exercise_04/exercise_10_04.c) 35 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_01/program_10_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_01.c - Illustrating Pointers 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int count = 10, x; 10 | int *int_pointer; 11 | 12 | int_pointer = &count; 13 | x = * int_pointer; 14 | 15 | printf ("count = %i, x = %i\n", count, x); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_02/program_10_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_02.c - More Pointer Basics 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | char c = 'Q'; 10 | char *char_pointer = &c; 11 | 12 | printf ("%c %c\n", c, *char_pointer); 13 | 14 | c = '/'; 15 | printf ("%c %c\n", c, *char_pointer); 16 | 17 | *char_pointer = '('; 18 | printf ("%c %c\n", c, *char_pointer); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_03/program_10_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_03.c - Using pointers in expressions 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | int i1, i2; 10 | int *p1, *p2; 11 | 12 | i1 = 5; 13 | p1 = &i1; 14 | i2 = *p1 / 2 + 10; 15 | p2 = p1; 16 | 17 | printf("i1 = %i, i2 = %i, *p1 = %i, *p2 = %i\n", i1, i2, *p1, *p2); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_04/program_10_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_04.c - Using Pointers to Structures 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct date 10 | { 11 | int month; 12 | int day; 13 | int year; 14 | }; 15 | 16 | struct date today, *datePtr; 17 | 18 | datePtr = &today; 19 | 20 | datePtr->month = 10; 21 | datePtr->day = 12; 22 | datePtr->year = 2015; 23 | 24 | printf ("Today's date is %i/%i/%.2i.\n", datePtr->month, datePtr->day, 25 | datePtr->year % 100); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_05/program_10_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_05.c - Using Structures Containing Pointers 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct intPtrs 10 | { 11 | int *p1; 12 | int *p2; 13 | }; 14 | 15 | struct intPtrs pointers; 16 | int i1 = 100, i2; 17 | 18 | pointers.p1 = &i1; 19 | pointers.p2 = &i2; 20 | *pointers.p2 = -97; 21 | 22 | printf ("i1 = %i, *pointers.p1 = %i\n", i1, *pointers.p1); 23 | printf ("i2 = %i, *pointers.p2 = %i\n", i2, *pointers.p2); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_06/program_10_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_06.c - Using Linked Lists 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct entry 10 | { 11 | int value; 12 | struct entry *next; 13 | }; 14 | 15 | struct entry n1, n2, n3; 16 | int i; 17 | 18 | n1.value = 100; 19 | n2.value = 200; 20 | n3.value = 300; 21 | 22 | n1.next = &n2; 23 | n2.next = &n3; 24 | 25 | i = n1.next->value; 26 | printf ("%i ", i); 27 | 28 | printf ("%i\n", n2.next->value); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_07/program_10_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_07.c - Traversing a Linked List 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | struct entry 10 | { 11 | int value; 12 | struct entry *next; 13 | }; 14 | 15 | struct entry n1, n2, n3; 16 | struct entry *list_pointer = &n1; 17 | 18 | n1.value = 100; 19 | n1.next = &n2; 20 | 21 | n2.value = 200; 22 | n2.next = &n3; 23 | 24 | n3.value = 300; 25 | n3.next = (struct entry *) 0; // mark list end with null pointer 26 | 27 | while ( list_pointer != (struct entry *) 0 ) 28 | { 29 | printf ("%i\n", list_pointer->value); 30 | list_pointer = list_pointer->next; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_08/program_10_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_08.c - Using Pointers and Functions 3 | */ 4 | 5 | #include 6 | 7 | void test (int *int_pointer) 8 | { 9 | *int_pointer = 100; 10 | } 11 | 12 | int main (void) 13 | { 14 | void test (int *int_pointer); 15 | int i = 50, *p = &i; 16 | 17 | printf ("Before the call to test i = %i\n", i); 18 | 19 | test (p); 20 | 21 | printf ("After the call to test i = %i\n", i); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_09/program_10_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_09.c - Using Pointers to Exchange Values 3 | */ 4 | 5 | #include 6 | 7 | void exchange (int * const pint1, int * const pint2) 8 | { 9 | int temp; 10 | 11 | temp = *pint1; 12 | *pint1 = *pint2; 13 | *pint2 = temp; 14 | } 15 | 16 | int main (void) 17 | { 18 | void exchange (int * const pint1, int * const pint2); 19 | int i1 = -5, i2 = 66, *p1 = &i1, *p2 = &i2; 20 | 21 | printf ("i1 = %i, i2 = %i\n", i1, i2); 22 | 23 | exchange (p1, p2); 24 | printf ("i1 = %i, i2 = %i\n", i1, i2); 25 | 26 | exchange (&i1, &i2); 27 | printf ("i1 = %i, i2 = %i\n", i1, i2); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_10/program_10_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_10.c - Returning a Pointer from a Function 3 | */ 4 | 5 | #include 6 | 7 | struct entry 8 | { 9 | int value; 10 | struct entry *next; 11 | }; 12 | 13 | struct entry *findEntry (struct entry *listPtr, int match) 14 | { 15 | while ( listPtr != (struct entry *) 0 ) 16 | if ( listPtr->value == match ) 17 | return (listPtr); 18 | else 19 | listPtr = listPtr->next; 20 | 21 | return (struct entry *) 0; 22 | } 23 | 24 | int main (void) 25 | { 26 | struct entry *findEntry (struct entry *listPtr, int match); 27 | struct entry n1, n2, n3; 28 | struct entry *listPtr, *listStart = &n1; 29 | 30 | int search; 31 | 32 | n1.value = 100; 33 | n1.next = &n2; 34 | 35 | n2.value = 200; 36 | n2.next = &n3; 37 | 38 | n3.value = 300; 39 | n3.next = (struct entry *) 0; // mark list end with null pointer 40 | 41 | printf ("Enter value to locate: "); 42 | scanf ("%i", &search); 43 | 44 | listPtr = findEntry (listStart, search); 45 | 46 | if( listPtr != (struct entry *) 0 ) 47 | printf ("Found %i.\n", listPtr->value); 48 | else 49 | printf ("Not found.\n"); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_11/program_10_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_11.c - Working with Pointers to Arrays 3 | */ 4 | 5 | #include 6 | 7 | int arraySum (int array[], const int n) 8 | { 9 | int sum = 0, *ptr; 10 | int * const arrayEnd = array + n; 11 | 12 | for ( ptr = array; ptr < arrayEnd; ++ptr) 13 | { 14 | sum += *ptr; 15 | } 16 | 17 | return sum; 18 | } 19 | 20 | int main (void) 21 | { 22 | int arraySum(int array[], const int n); 23 | int values[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5}; 24 | 25 | printf ("The sum is %i\n", arraySum (values, 10)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_12/program_10_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_12.c - Summing the Elements of an Array 3 | */ 4 | 5 | #include 6 | 7 | int arraySum (int *array, const int n) 8 | { 9 | int sum = 0; 10 | int * const arrayEnd = array + n; 11 | 12 | for ( ; array < arrayEnd; ++array) 13 | { 14 | sum += *array; 15 | } 16 | 17 | return sum; 18 | } 19 | 20 | int main (void) 21 | { 22 | int arraySum(int *array, const int n); 23 | int values[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5}; 24 | 25 | printf ("The sum is %i\n", arraySum (values, 10)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_13/program_10_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_13.c - Pointer Version of copyString() 3 | */ 4 | 5 | #include 6 | 7 | void copyString (char *to, char *from) 8 | { 9 | for ( ; *from != '\0'; ++from, ++to ) 10 | *to = *from; 11 | 12 | *to = '\0'; 13 | } 14 | 15 | int main (void) 16 | { 17 | void copyString (char *to, char *from); 18 | char string1[] = "A string to be copied."; 19 | char string2[50]; 20 | 21 | copyString (string2, string1); 22 | printf ("%s\n", string2); 23 | 24 | copyString (string2, "So is this."); 25 | printf ("%s\n", string2); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_14/program_10_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_14.c - Revised Version of the copyString() 3 | */ 4 | 5 | #include 6 | 7 | void copyString (char *to, char *from) 8 | { 9 | while ( *from ) 10 | *to++ = *from++; 11 | 12 | *to = '\0'; 13 | } 14 | 15 | int main (void) 16 | { 17 | void copyString (char *to, char *from); 18 | char string1[] = "A string to be copied."; 19 | char string2[50]; 20 | 21 | copyString (string2, string1); 22 | printf ("%s\n", string2); 23 | 24 | copyString (string2, "So is this."); 25 | printf ("%s\n", string2); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_01/Program_10_15/program_10_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_10_15.c - Using Pointers to Find the Length of a String 3 | */ 4 | 5 | #include 6 | 7 | int stringLength (const char *string) 8 | { 9 | const char *cptr = string; 10 | 11 | while ( *cptr ) 12 | ++cptr; 13 | return cptr - string; 14 | } 15 | 16 | int main (void) 17 | { 18 | int stringLength (const char *string); 19 | 20 | printf("%i ", stringLength ("stringLength test")); 21 | printf("%i ", stringLength ("")); 22 | printf("%i ", stringLength ("complete")); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_02/exercise_10_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_10_02.c 3 | */ 4 | 5 | #include 6 | 7 | struct entry 8 | { 9 | int value; 10 | struct entry *next; 11 | }; 12 | 13 | void insertEntry(struct entry *newEntry, struct entry *existingEntry) 14 | { 15 | newEntry->next = existingEntry->next; 16 | existingEntry->next = newEntry; 17 | } 18 | 19 | int main (void) 20 | { 21 | void insertEntry(struct entry *newEntry, struct entry *existingEntry); 22 | 23 | struct entry n1, n2, nE; 24 | struct entry *listPointer; 25 | 26 | n1.value = 100; 27 | n1.next = &n2; 28 | 29 | nE.value = 150; 30 | 31 | n2.value = 200; 32 | n2.next = (struct entry *) 0; // mark list end with null pointer 33 | 34 | listPointer = &n1; 35 | 36 | printf("Values in the list before insert of new entry: "); 37 | while ( listPointer != (struct entry *) 0 ) 38 | { 39 | printf ("%i ", listPointer->value); 40 | listPointer = listPointer->next; 41 | } 42 | printf("\n"); 43 | 44 | insertEntry(&nE, &n1); 45 | 46 | listPointer = &n1; 47 | 48 | printf("Values in the list after insert of new entry: "); 49 | while ( listPointer != (struct entry *) 0 ) 50 | { 51 | printf ("%i ", listPointer->value); 52 | listPointer = listPointer->next; 53 | } 54 | printf("\n"); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_03/exercise_10_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_10_03.c 3 | */ 4 | 5 | #include 6 | 7 | struct entry 8 | { 9 | int value; 10 | struct entry *next; 11 | }; 12 | 13 | void insertEntry(struct entry *newEntry, struct entry *existingEntry) 14 | { 15 | newEntry->next = existingEntry->next; 16 | existingEntry->next = newEntry; 17 | } 18 | 19 | int main (void) 20 | { 21 | void insertEntry(struct entry *newEntry, struct entry *existingEntry); 22 | 23 | struct entry head, n1, n2, nE; 24 | struct entry *listPointer; 25 | 26 | head.next = &n1; 27 | 28 | n1.value = 100; 29 | n1.next = &n2; 30 | 31 | n2.value = 200; 32 | n2.next = (struct entry *) 0; // mark list end with null pointer 33 | 34 | listPointer = head.next; 35 | 36 | printf("Values in the list before insert of new entry: "); 37 | while ( listPointer != (struct entry *) 0 ) 38 | { 39 | printf ("%i ", listPointer->value); 40 | listPointer = listPointer->next; 41 | } 42 | printf("\n"); 43 | 44 | nE.value = 50; 45 | insertEntry(&nE, &head); 46 | 47 | listPointer = head.next; 48 | 49 | printf("Values in the list after insert of new entry: "); 50 | while ( listPointer != (struct entry *) 0 ) 51 | { 52 | printf ("%i ", listPointer->value); 53 | listPointer = listPointer->next; 54 | } 55 | printf("\n"); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Exercises/Chapter_10/Exercise_04/exercise_10_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_10_04.c 3 | */ 4 | 5 | #include 6 | 7 | struct entry 8 | { 9 | int value; 10 | struct entry *next; 11 | }; 12 | 13 | void removeEntry(struct entry *entryToKeep) 14 | { 15 | entryToKeep->next = entryToKeep->next->next; 16 | } 17 | 18 | int main (void) 19 | { 20 | void insertEntry(struct entry *newEntry, struct entry *existingEntry); 21 | 22 | struct entry head, n1, n2, n3; 23 | struct entry *listPointer; 24 | 25 | head.next = &n1; 26 | n1.value = 100; 27 | n1.next = &n2; 28 | n2.value = 200; 29 | n2.next = &n3; 30 | n3.value = 300; 31 | n3.next = (struct entry *) 0; // mark list end with null pointer 32 | 33 | listPointer = head.next; 34 | 35 | printf("Values in the list before removal of second entry: "); 36 | while ( listPointer != (struct entry *) 0 ) 37 | { 38 | printf ("%i ", listPointer->value); 39 | listPointer = listPointer->next; 40 | } 41 | printf("\n"); 42 | 43 | 44 | removeEntry(&n1); 45 | 46 | listPointer = head.next; 47 | 48 | printf("Values in the list after removal of second entry: "); 49 | while ( listPointer != (struct entry *) 0 ) 50 | { 51 | printf ("%i ", listPointer->value); 52 | listPointer = listPointer->next; 53 | } 54 | printf("\n"); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Exercises/Chapter_11/Chapter_11.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 11 # 2 | ## 1. ## 3 | Test run of the four programs presented in this chapter. 4 | - [Program 11.1](Exercise_01/Program_11_01/program_11_01.c) 5 | - [Program 11.2](Exercise_01/Program_11_02/program_11_02.c) 6 | - [Program 11.3](Exercise_01/Program_11_03/program_11_03.c) 7 | - [Program 11.4](Exercise_01/Program_11_04/program_11_04.c) 8 | -------------------------------------------------------------------------------- /Exercises/Chapter_11/Exercise_01/Program_11_01/program_11_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_11_01.c - The Bitwise AND Operator 3 | * 4 | * Program to demonstrate the bitwise AND operator. 5 | */ 6 | 7 | #include 8 | 9 | int main (void) 10 | { 11 | unsigned int word1 = 077u, word2 = 0150u, word3 = 0210u; 12 | 13 | printf ("%o ", word1 & word2); 14 | printf ("%o ", word1 & word1); 15 | printf ("%o ", word1 & word2 & word3); 16 | printf ("%o\n", word1 & 1); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Exercises/Chapter_11/Exercise_01/Program_11_02/program_11_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * exercise_11_02.c - Illustrate Bitwise Operators 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | unsigned int w1 = 0525u, w2 = 0707u, w3 = 0122u; 10 | 11 | printf ("%o %o %o\n", w1 & w2, w1 | w2, w1 ^ w2); 12 | printf ("%o %o %o\n", ~w1, ~w2, ~w3); 13 | printf ("%o %o %o\n", w1 ^ w2, w1 & ~w2, w1 | w2 | w3); 14 | printf ("%o %o\n", w1 | w2 & w3, w1 | w2 & ~w3); 15 | printf ("%o %o \n", ~(~w1 & ~w2), ~(~w1 | ~w2)); 16 | 17 | w1 ^= w2; 18 | w2 ^= w1; 19 | w1 ^= w2; 20 | printf ("w1 = %o, w2 = %o\n", w1, w2); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Exercises/Chapter_11/Exercise_01/Program_11_03/program_11_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_11_03.c - Implementing a shift function 3 | */ 4 | 5 | #include 6 | 7 | unsigned int shift (unsigned int value, int n) 8 | { 9 | if ( n > 0 ) // left shift 10 | value <<= n; 11 | else // right shift 12 | value >>= -n; 13 | 14 | return value; 15 | } 16 | 17 | int main (void) 18 | { 19 | unsigned int w1 = 0177777u, w2 = 0444u; 20 | unsigned int shift (unsigned int value, int n); 21 | 22 | printf("%o\t%o\t\n", shift (w1, 5), w1 << 5); 23 | printf("%o\t%o\t\n", shift (w1, -6), w1 >> 6); 24 | printf("%o\t%o\t\n", shift (w2, 0), w2 >> 0); 25 | printf("%o\n", shift (shift (w1, -3), 3)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Exercises/Chapter_11/Exercise_01/Program_11_04/program_11_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_11_04.c - Implementing a Rotate Function 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | unsigned int w1 = 0xabcdef00u, w2 = 0xffff1122u; 10 | unsigned int rotate (unsigned int value, int n); 11 | 12 | printf ("%x\n", rotate(w1, 8)); 13 | printf ("%x\n", rotate(w1, -16)); 14 | printf ("%x\n", rotate(w2, 4)); 15 | printf ("%x\n", rotate(w2, -2)); 16 | printf ("%x\n", rotate(w1, 0)); 17 | printf ("%x\n", rotate(w1, 44)); 18 | 19 | return 0; 20 | } 21 | 22 | /* 23 | * Function to rotate an unsigned int left or right. 24 | */ 25 | unsigned int rotate (unsigned int value, int n) 26 | { 27 | unsigned int result, bits; 28 | 29 | // scale down the shift count to a defined range 30 | 31 | if ( n > 0 ) 32 | n = n % 32; 33 | else 34 | n = -(-n % 32); 35 | 36 | if ( n == 0 ) 37 | { 38 | result = value; 39 | } 40 | else if ( n > 0 ) 41 | { 42 | // shall left rotate 43 | bits = value >> (32 - n); 44 | result = value << n | bits; 45 | } 46 | else 47 | { 48 | // shall right rotate 49 | n = -n; 50 | bits = value << (32 - n); 51 | result = value >> n | bits; 52 | } 53 | 54 | return result; 55 | } 56 | -------------------------------------------------------------------------------- /Exercises/Chapter_12/Chapter_12.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 12 # 2 | ## 1. ## 3 | Test run of the three programs presented in this chapter. 4 | - [Program 12.1](Exercise_01/Program_12_01/program_12_01.c) 5 | - [Program 12.2](Exercise_01/Program_12_02/program_12_02.c) 6 | - [Program 12.3](Exercise_01/Program_12_03/program_12_03.c), [metric.h](Exercise_01/Program_12_03/metric.h) 7 | -------------------------------------------------------------------------------- /Exercises/Chapter_12/Exercise_01/Program_12_01/program_12_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_12_01.c - Introducing the #define Statement 3 | */ 4 | 5 | #include 6 | 7 | #define YES 1 8 | #define NO 0 9 | 10 | /* 11 | * Function to determine if an integer is even. 12 | */ 13 | 14 | int isEven (int number) 15 | { 16 | int answer; 17 | 18 | if ( number % 2 == 0 ) 19 | answer = YES; 20 | else 21 | answer = NO; 22 | 23 | return answer; 24 | } 25 | 26 | int main (void) 27 | { 28 | int isEven (int number); 29 | 30 | if ( isEven (17) == YES ) 31 | printf ("yes "); 32 | else 33 | printf ("no "); 34 | 35 | 36 | if ( isEven (20) == YES ) 37 | printf ("yes\n"); 38 | else 39 | printf ("no\n"); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Exercises/Chapter_12/Exercise_01/Program_12_02/program_12_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_12_02.c - More on working with Defines 3 | */ 4 | 5 | #include 6 | 7 | #define PI 3.141592654 8 | 9 | /* 10 | * Function to calculate the area of a circle. 11 | */ 12 | double area (double r) 13 | { 14 | return PI * r * r; 15 | } 16 | 17 | /* 18 | * Function to calculate the circumference of a circle. 19 | */ 20 | double circumference (double r) 21 | { 22 | return 2.0 * PI * r; 23 | } 24 | 25 | /* 26 | * Function to calculate the volume of a circle. 27 | */ 28 | double volume (double r) 29 | { 30 | return 4.0 / 3.0 * PI * r * r * r; 31 | } 32 | 33 | int main (void) 34 | { 35 | double area (double r); 36 | double circumference (double r); 37 | double volume (double r); 38 | 39 | printf ("radius = 1: %.4f %.4f %.4f\n", area (1.0), 40 | circumference (1.0), volume (1.0)); 41 | 42 | printf ("radius = 4.98: %.4f %.4f %.4f\n", area (4.98), 43 | circumference (4.98), volume (4.98)); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Exercises/Chapter_12/Exercise_01/Program_12_03/metric.h: -------------------------------------------------------------------------------- 1 | /* 2 | * metric.h 3 | */ 4 | 5 | #ifndef METRIC_H_ 6 | #define METRIC_H_ 7 | 8 | #define INCHES_PER_CENTIMETER 0.394 9 | #define CENTIMETER_PER_INCH 1 / INCHES_PER_CENTIMETER 10 | 11 | #define QUARTS_PER_LITER 1.057 12 | #define LITERS_PER_QUART 1 / LITERS_PER_QUART 13 | 14 | #define OUNCES_PER_GRAM 0.035 15 | #define GRAMS_PER_OUNCE 1 / GRAMS_PER_OUNCE 16 | 17 | #endif /* METRIC_H_ */ 18 | -------------------------------------------------------------------------------- /Exercises/Chapter_12/Exercise_01/Program_12_03/program_12_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_12_03.c - Using the #include Statement 3 | * 4 | * Program to illustrate the use of the #include statement. 5 | * 6 | * Note: This program assumes that definitions are set up in a file 7 | * called metrics.h. 8 | */ 9 | 10 | #include 11 | #include "metric.h" 12 | 13 | int main (void) 14 | { 15 | float liters, gallons; 16 | 17 | printf ("*** Liters to Gallons ***\n\n"); 18 | printf ("Enter the numbers of liters: "); 19 | scanf ("%f", &liters); 20 | 21 | gallons = liters * QUARTS_PER_LITER / 4.0; 22 | printf ("%g liters = %g gallons\n", liters, gallons); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/Chapter_13/Chapter_13.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 13 # 2 | ## 1. ## 3 | A type called FunctionPtr() that represents a pointer to a function that returns an int and that takes no arguments is defined in this way: 4 | 5 | `typedef int (*FunctionPtr) (void);` 6 | 7 | See this [question](http://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c-examples-hints-and-tips-ple) at stackoverflow.com to learn more. 8 | -------------------------------------------------------------------------------- /Exercises/Chapter_15/Chapter_15.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 15 # 2 | ## 1. ## 3 | Test run of the five programs presented in this chapter. 4 | - [Program 15.1](Exercise_01/Program_15_01/program_15_01.c) 5 | - [Program 15.2](Exercise_01/Program_15_02/program_15_02.c) 6 | 7 | Could not get Program 15.1 to work fully on my machine. The two last print statements did not work as intended. 8 | -------------------------------------------------------------------------------- /Exercises/Chapter_15/Exercise_01/Program_15_01/program_15_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_15_01.c - Illustrating the printf() Formats 3 | */ 4 | 5 | #include 6 | 7 | int main (void) 8 | { 9 | char c = 'X'; 10 | char s[] = "abcdefghijklmnopqrstuvwxyz"; 11 | int i = 425; 12 | short int j = 17; 13 | unsigned int u = 0xf179U; 14 | long int l = 75000L; 15 | long long int L = 0x1234567812345678LL; 16 | float f = 12.978F; 17 | double d = -97.4583; 18 | char *cp = &c; 19 | int *ip = &i; 20 | int c1, c2; 21 | 22 | printf ("Integers:\n"); 23 | printf ("%i %o %x %u\n", i ,i ,i, i); 24 | printf ("%x %X %#x %#X\n", i, i, i, i); 25 | printf ("%+i % i %07i %.7i\n", i, i, i, i); 26 | printf ("%i %o %x %u\n", j ,j ,j, j); 27 | printf ("%i %o %x %u\n", u ,u ,u, u); 28 | printf ("%ld %lo %lx %lu\n", l ,l ,l, l); 29 | printf ("%lli %llo %llx %llu\n", L ,L ,L, L); 30 | 31 | printf ("\nFloats and Doubles:\n"); 32 | printf ("%f %e %g\n", f, f, f); 33 | printf ("%.2f %.2e\n", f, f); 34 | printf ("%.0f %.0e\n", f, f); 35 | printf ("%7.2f %7.2e\n", f ,f); 36 | printf ("%f %e %g\n", d ,d ,d); 37 | printf ("%.*f\n", 3 ,d); 38 | printf ("%*.*f\n", 8 ,2 ,d); 39 | 40 | printf ("\nCharacters:\n"); 41 | printf ("%c\n", c); 42 | printf ("%3c%3c\n", c, c); 43 | printf ("%x\n", c); 44 | 45 | printf ("\nStrings:\n"); 46 | printf ("%s\n", s); 47 | printf ("%.5s\n", s); 48 | printf ("%30s\n", s); 49 | printf ("%20.5s\n", s); 50 | printf ("%-20.5s\n", s); 51 | 52 | printf ("\nPointers:\n"); 53 | printf ("%p %p\n\n", ip, cp); 54 | 55 | printf ("This%n is fun.%n\n", &c1, &c2); 56 | printf ("c1 = %i, c2 = %i\n", c1, c2); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Exercises/Chapter_15/Exercise_01/Program_15_02/program_15_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_15_02.c - A Simple Example 3 | * 4 | * Taking a single number and outputting several calculations. 5 | */ 6 | 7 | #include 8 | 9 | main () 10 | { 11 | float d = 6.5; 12 | float half, square, cube; 13 | 14 | half = d / 2; 15 | square = d * d; 16 | cube = d * d * d; 17 | 18 | printf("\nYour number is %.2f\n", d); 19 | printf("Half of it is %.2f\n", half); 20 | printf("Square it to get %.2f\n", square); 21 | printf("Cube it to get %.2f\n", cube); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Exercises/Chapter_17/Chapter_17.md: -------------------------------------------------------------------------------- 1 | # Exercise Solutions Chapter 17 # 2 | ## 1. ## 3 | Test run of the five programs presented in this chapter. 4 | - [Program 17.1](Exercise_01/Program_17_01/program_17_01.c) 5 | -------------------------------------------------------------------------------- /Exercises/Chapter_17/Exercise_01/Program_17_01/program_17_01.c: -------------------------------------------------------------------------------- 1 | /* 2 | * program_17_01.c - Adding Debug Statements with the Preprocessor 3 | */ 4 | 5 | #include 6 | 7 | #define DEBUG 8 | 9 | int process (int i, int j, int k) 10 | { 11 | return i + j + k; 12 | } 13 | 14 | int main (void) 15 | { 16 | int i, j, k, nread; 17 | 18 | nread = scanf ("%d %d %d", &i, &j, &k); 19 | 20 | #ifdef DEBUG 21 | fprintf (stderr, "Number of integers read = %i\n", nread); 22 | fprintf (stderr, "i = %i, j = %i, k = %i\n",i ,j ,k); 23 | #endif 24 | 25 | printf ("%i\n", process (i, j, k)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Henrik Samuelsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Programming in C 2 | Notes and exercises regarding the book Programming in C, 4th edition, written by Stephen G. Kochan. 3 | 4 | ## Exercise Solutions 5 | [Chapter 2](Exercises/Chapter_02/Chapter_02.md) - Compiling and Running Your First Program 6 | [Chapter 3](Exercises/Chapter_03/Chapter_03.md) - Variables, Data Types, and Arithmetic Expressions 7 | [Chapter 4](Exercises/Chapter_04/Chapter_04.md) - Program Looping 8 | [Chapter 5](Exercises/Chapter_05/Chapter_05.md) - Making Decisions 9 | [Chapter 6](Exercises/Chapter_06/Chapter_06.md) - Working with Arrays 10 | [Chapter 7](Exercises/Chapter_07/Chapter_07.md) - Working with Functions 11 | [Chapter 8](Exercises/Chapter_08/Chapter_08.md) - Working with Structures 12 | [Chapter 9](Exercises/Chapter_09/Chapter_09.md) - Character Strings 13 | [Chapter 10](Exercises/Chapter_10/Chapter_10.md) - Pointers 14 | [Chapter 11](Exercises/Chapter_11/Chapter_11.md) - Operations on Bits 15 | [Chapter 12](Exercises/Chapter_12/Chapter_12.md) - The Preprocessor 16 | [Chapter 13](Exercises/Chapter_13/Chapter_13.md) - Extending Data Types 17 | [Chapter 15](Exercises/Chapter_15/Chapter_15.md) - Input and Output Operations in C 18 | [Chapter 17](Exercises/Chapter_17/Chapter_17.md) - Debugging Programs 19 | 20 | ## Tools 21 | The exercises were done on a machine running Windows 7 with the following tool set. 22 | - [Mingw-w64](http://mingw-w64.org/doku.php/start) enables GCC compiler usage on Windows systems. 23 | - [CDT 8.7.0](https://eclipse.org/cdt/) an Integrated Development Environment for C and C++, based on the Eclipse platform. 24 | - [SourceTree](https://www.sourcetreeapp.com/) a GIT client. 25 | - [MarkdownPad](http://markdownpad.com/) a Markdown editor for Windows. 26 | --------------------------------------------------------------------------------