├── .gitignore ├── README.md └── exercises ├── exercise_10_1 ├── README.md └── exercise_10_1.c ├── exercise_10_2 ├── README.md └── exercise_10_2.c ├── exercise_11_1 ├── README.md └── exercise_11_1.c ├── exercise_11_2 ├── README.md └── exercise_11_2.c ├── exercise_11_3 ├── README.md └── exercise_11_3.c ├── exercise_12_1 ├── README.md └── exercise_12_1.c ├── exercise_12_2 ├── README.md └── exercise_12_2.c ├── exercise_13_1 ├── README.md └── exercise_13_1.c ├── exercise_13_2 ├── README.md └── exercise_13_2.c ├── exercise_1_1 ├── README.md └── exercise_1_1.c ├── exercise_2_1 ├── README.md └── exercise_2_1.c ├── exercise_2_2 ├── README.md └── exercise_2_2.c ├── exercise_3_1 ├── README.md └── exercise_3_1.c ├── exercise_3_2 ├── README.md └── exercise_3_2.c ├── exercise_4_1 ├── README.md └── exercise_4_1.c ├── exercise_4_2 ├── README.md └── exercise_4_2.c ├── exercise_5_1 ├── README.md └── exercise_5_1.c ├── exercise_5_2 ├── README.md └── exercise_5_2.c ├── exercise_5_3 ├── README.md └── exercise_5_3.c ├── exercise_6_1 ├── README.md └── exercise_6_1.c ├── exercise_6_2 ├── README.md └── exercise_6_2.c ├── exercise_6_3 ├── README.md └── exercise_6_3.c ├── exercise_7_1 ├── README.md └── exercise_7_1.c ├── exercise_7_2 ├── README.md └── exercise_7_2.c ├── exercise_7_3 ├── README.md └── exercise_7_3.c ├── exercise_8_1 ├── README.md └── exercise_8_1.c ├── exercise_8_2 ├── README.md └── exercise_8_2.c ├── exercise_9_1 ├── README.md └── exercise_9_1.c ├── exercise_9_2 ├── README.md └── exercise_9_2.c └── exercise_9_3 ├── README.md └── exercise_9_3.c /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | *.DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Exercises 2 | 3 | This repository contains the solutions to basic C programming exercises. 4 | 5 | ### Table of content 6 | 7 | 1. Introduction to C programming 8 | 1. [Exercise 1](exercises/exercise_1_1) 9 | 2. From travelling algorithms to the world of computing 10 | 1. [Exercise 1](exercises/exercise_2_1) 11 | 2. [Exercise 2](exercises/exercise_2_2) 12 | 3. Variables in the C language 13 | 1. [Exercise 1](exercises/exercise_3_1) 14 | 2. [Exercise 2](exercises/exercise_3_2) 15 | 4. Program statements, expressions and operators 16 | 1. [Exercise 1](exercises/exercise_4_1) 17 | 2. [Exercise 2](exercises/exercise_4_2) 18 | 5. If statements add power to your programs 19 | 1. [Exercise 1](exercises/exercise_5_1) 20 | 2. [Exercise 2](exercises/exercise_5_2) 21 | 3. [Exercise 3](exercises/exercise_5_3) 22 | 6. Loop structures in the C language: the lazy programmer's dream 23 | 1. [Exercise 1](exercises/exercise_6_1) 24 | 2. [Exercise 2](exercises/exercise_6_2) 25 | 3. [Exercise 3](exercises/exercise_6_3) 26 | 7. Functions - a route to more extensive programs 27 | 1. [Exercise 1](exercises/exercise_7_1) 28 | 2. [Exercise 2](exercises/exercise_7_2) 29 | 3. [Exercise 3](exercises/exercise_7_3) 30 | 8. Numerical arrays in the C language 31 | 1. [Exercise 1](exercises/exercise_8_1) 32 | 2. [Exercise 2](exercises/exercise_8_2) 33 | 9. Handling files in the C language 34 | 1. [Exercise 1](exercises/exercise_9_1) 35 | 2. [Exercise 2](exercises/exercise_9_2) 36 | 3. [Exercise 3](exercises/exercise_9_3) 37 | 10. Handling characters and strings in the C language 38 | 1. [Exercise 1](exercises/exercise_10_1) 39 | 2. [Exercise 2](exercises/exercise_10_2) 40 | 11. Simple data structures in the C language 41 | 1. [Exercise 1](exercises/exercise_11_1) 42 | 2. [Exercise 2](exercises/exercise_11_2) 43 | 3. [Exercise 3](exercises/exercise_11_3) 44 | 12. Pointers in the C language 45 | 1. [Exercise 1](exercises/exercise_12_1) 46 | 2. [Exercise 2](exercises/exercise_12_2) 47 | 13. Arrays and pointers 48 | 1. [Exercise 1](exercises/exercise_13_1) 49 | 2. [Exercise 2](exercises/exercise_13_2) 50 | -------------------------------------------------------------------------------- /exercises/exercise_10_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 10 exercise 1 2 | 3 | Write a program that asks for the user's first and last name and saves these into a file named by the user. 4 | 5 | The program must start by asking for the first name. The last name is entered next, followed by the desired file name. The first part of the file name may have a maximum of 8 characters and the second part may have 3 (for example: personal.usr). The file must reside in the same directory as the program. 6 | 7 | The last name can have a maximum of 20 characters, the first name 15. 8 | 9 | **Hint:** 10 | In the chapter dealing with file processing, files were opened using a string array. Read the chapter and you should be able to perceive how to implement the program. 11 | 12 | _Example output_ 13 | 14 | ``` 15 | The program saves your first and last name into a file. 16 | Enter your first name:Thomas 17 | Enter your last name:Jefferson 18 | File where you want to save your name:personal.usr 19 | 20 | Successfully saved the data! 21 | ``` 22 | -------------------------------------------------------------------------------- /exercises/exercise_10_1/exercise_10_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | char firstname[16], lastname[21], filename[13]; 6 | 7 | printf("The program saves your first and last name into a file."); 8 | printf("Enter your first name: "); 9 | scanf("%s", &firstname[0]); 10 | printf("Enter your last name: "); 11 | scanf("%s", &lastname[0]); 12 | printf("File where you want to save your name: "); 13 | scanf("%s", &filename[0]); 14 | 15 | FILE *file = fopen(filename, "w"); 16 | 17 | fprintf(file, "%s %s", firstname, lastname); 18 | printf("Successfully saved the data!"); 19 | fclose(file); 20 | 21 | return 0; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /exercises/exercise_10_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 10 exercise 2 2 | 3 | Write a program that prompts the user for a word (max 15 lower-case letters) and calculates the number of vowels (a, e, i, o, u, y) in the word. Hint: 4 | You have learned to compare the value of a character variable to 'a', for example. You can use an index to refer to single characters in a string array. (char[] = "word"; word\[1\] == 'a';) 5 | 6 | _Example output_ 7 | 8 | ``` 9 | The program calculates the number of vowels. 10 | Enter a word:test 11 | The word contains 1 vowels. 12 | ``` 13 | -------------------------------------------------------------------------------- /exercises/exercise_10_2/exercise_10_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | char word[16]; 6 | int vowels = 0; 7 | 8 | printf("The program calculates the number of vowels.\nEnter a word: "); 9 | scanf("%s", &word[0]); 10 | 11 | int i; 12 | for (i = 0; i < 16; i++) { 13 | if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u' || word[i] == 'y') { 14 | vowels++; 15 | } 16 | } 17 | 18 | printf("The word contains %d vowels.", vowels); 19 | 20 | return 0; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /exercises/exercise_11_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 11 exercise 1 2 | 3 | In this chapter, we create independent programs that are closely associated with each other. All the programs are components of a phone directory program. The intention is that you can continue your programming exercises by programming a phone directory. The exercises in this chapter can easily be converted into functions for a larger entity. 4 | 5 | The name and number data for the phone directory are contained in the file "phonedir.txt" which has the following format: 6 | 7 | ``` 8 | 4 9 | Firstname Lastname 050-3500980 10 | John Doe 041-3478924 11 | Brian Smith 040-3980982 12 | Brita Smith 05-4567393 13 | ``` 14 | 15 | The first line of the file indicates the number of names added to the directory as an integer. The first name, last name and telephone number are processed in text format. The data items are separated by spaces in the file, and the length of an individual item (such as a first name) may be no more than 20 characters. Each line includes the information for one person only. The directory may contain a maximum of 50 people. Your first task is to write a program for adding a new telephone number to the directory. Upon execution, the program must first ask for a first name, after which it prompts for a last name and a telephone number. When data is saved, the value on the first line of the file must increase by one. Note that in order to simplify the exercise, the data to be entered may not include a newline character, for example. Each personal data item is stored on its own line. (Remember the newline character.) 16 | 17 | _Example output_ 18 | 19 | ``` 20 | Enter first name:Thomas 21 | Enter last name:Jefferson 22 | Enter telephone number:05-6243370 23 | Successfully saved the data. 24 | ``` 25 | -------------------------------------------------------------------------------- /exercises/exercise_11_1/exercise_11_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | struct person { 6 | char firstname[20]; 7 | char lastname[20]; 8 | char telephone[11]; 9 | }; 10 | 11 | struct person new_person; 12 | 13 | printf("Enter first name: "); 14 | scanf("%s", &new_person.firstname[0]); 15 | printf("Enter last name: "); 16 | scanf("%s", &new_person.lastname[0]); 17 | printf("Enter telephone number: "); 18 | scanf("%s", &new_person.telephone[0]); 19 | 20 | FILE *file = fopen("phonedir.txt", "a"); 21 | 22 | if (file == NULL) { 23 | printf("Unable to open file."); 24 | return 0; 25 | } else { 26 | 27 | fprintf(file, "%s %s %s\n", new_person.firstname, new_person.lastname, new_person.telephone); 28 | fclose(file); 29 | printf("Successfully saved the data."); 30 | 31 | } 32 | 33 | return 0; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /exercises/exercise_11_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 11 exercise 2 2 | 3 | Write a program that prints all the numbers in the phone directory to the screen. The file for reading the numbers is the same as in the previous exercise (phonedir.txt). Upon execution, the program opens the file, reads the data and prints it. 4 | 5 | The name and number data for the phone directory are contained in the file "phonedir.txt" which has the following format: 6 | 7 | ``` 8 | 4 9 | Firstname Lastname 050-3500980 10 | John Doe 041-3478924 11 | Brian Smith 040-3980982 12 | Brita Smith 05-4567393 13 | ``` 14 | 15 | _Example output_ 16 | 17 | ``` 18 | Brita Smith 05-4567393 19 | Thomas Jefferson 05-6243370 20 | ``` 21 | -------------------------------------------------------------------------------- /exercises/exercise_11_2/exercise_11_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | FILE *file = fopen("phonedir.txt", "r"); 6 | 7 | struct person { 8 | char firstname[20]; 9 | char lastname[20]; 10 | char telephone[11]; 11 | } current; 12 | 13 | if (file == NULL) { 14 | printf("Unable to read file."); 15 | return 0; 16 | } else { 17 | 18 | int entries = 0; 19 | fscanf(file, "%d", &entries); 20 | 21 | int i; 22 | for (i = 0; i < entries; i++) { 23 | fscanf(file, "%s%s%s", ¤t.firstname[0], ¤t.lastname[0], ¤t.telephone[0]); 24 | printf("%s %s %s\n", current.firstname, current.lastname, current.telephone); 25 | } 26 | 27 | fclose(file); 28 | 29 | } 30 | 31 | return 0; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /exercises/exercise_11_3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 11 exercise 3 2 | 3 | Write a program for removing a specific person's data from the directory. The program asks for a first name followed by a last name. The program checks if the person's data can be found. If the person is in the directory, the data is removed. Otherwise print an error message. 4 | 5 | **Hint:** 6 | You should carefully design the program before writing any actual code. The use of structures may make the task easier. It is most obvious that when you save an updated list, the existing data in the file phonedir.txt is overwritten. 7 | 8 | You can compare name information using the C language function strcmp() in the library file string.h. You can find information about using the function in the C manual. 9 | 10 | The name and number data for the phone directory are contained in the file "phonedir.txt" which has the following format: 11 | 12 | ``` 13 | 4 14 | Firstname Lastname 050-3500980 15 | John Doe 041-3478924 16 | Brian Smith 040-3980982 17 | Brita Smith 05-4567393 18 | ``` 19 | 20 | _Example output_ 21 | 22 | ``` 23 | Enter first name:Tim 24 | Enter last name:Franklin 25 | Data removed from the directory. 26 | ``` 27 | -------------------------------------------------------------------------------- /exercises/exercise_11_3/exercise_11_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | FILE *file_read = fopen("phonedir.txt", "r"); 7 | 8 | if (file_read == NULL) { 9 | printf("Unable to open file."); 10 | return 0; 11 | } 12 | 13 | struct person { 14 | char firstname[20]; 15 | char lastname[20]; 16 | char telephone[12]; 17 | }; 18 | 19 | struct person searched; 20 | 21 | printf("Enter first name: "); 22 | scanf("%s", &searched.firstname[0]); 23 | printf("Enter last name: "); 24 | scanf("%s", &searched.lastname[0]); 25 | 26 | int entries = 0; 27 | fscanf(file_read, "%d", &entries); 28 | 29 | struct person persons[entries]; 30 | struct person current; 31 | 32 | int i, count = -1; 33 | for (i = 0; i < entries; i++) { 34 | fscanf(file_read, "%s%s%s", ¤t.firstname[0], ¤t.lastname[0], ¤t.telephone[0]); 35 | if (strcmp(searched.firstname, current.firstname) != 0 || strcmp(searched.lastname, current.lastname) != 0) { 36 | persons[++count] = current; 37 | } 38 | } 39 | 40 | fclose(file_read); 41 | 42 | FILE *file_write = fopen("phonedir.txt", "w"); 43 | 44 | fprintf(file_write, "%d\n", count + 1); 45 | 46 | for (i = 0; i <= count; i++) { 47 | fprintf(file_write, "%s %s %s\n", persons[i].firstname, persons[i].lastname, persons[i].telephone); 48 | } 49 | 50 | printf("Data removed from the directory."); 51 | 52 | fclose(file_write); 53 | 54 | return 0; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /exercises/exercise_12_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 12 exercise 1 2 | 3 | The following is a simple C program that prompts the user for an integer and stores it in the variable x. The program prints the number, calls the subroutine "change_number" passing the number as an argument, and finally prints the number again: 4 | 5 | ```c 6 | #include 7 | 8 | void change_number(int x); 9 | 10 | int main(void) { 11 | int x; 12 | printf("Enter the number x: "); 13 | scanf("%d", &x); 14 | printf("In the main program: x = %d\n", x); 15 | change_number(x); 16 | printf("In the main program: x = %d\n", x); 17 | } 18 | ``` 19 | 20 | Your task is to write the subroutine "change_number" that takes an integer, increases it by three and prints the changed number as shown in the example printout. Pay attention to the value of the variable "x" in different parts of the program. In the following exercise, your subroutine will be supplemented with pointers. 21 | 22 | _Example output_ 23 | 24 | ``` 25 | Enter the number x: 111 26 | In the main program: x = 111 27 | In the subroutine: x = 114 28 | In the main program: x = 111 29 | ``` 30 | -------------------------------------------------------------------------------- /exercises/exercise_12_1/exercise_12_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void change_number(int x); 4 | 5 | int main(void) { 6 | int x; 7 | printf("Enter the number x: "); 8 | scanf("%d", &x); 9 | printf("In the main program: x = %d\n", x); 10 | change_number(x); 11 | printf("In the main program: x = %d\n", x); 12 | } 13 | 14 | void change_number(int x) { 15 | printf("In the subroutine: x = %d\n", x + 3); 16 | } 17 | -------------------------------------------------------------------------------- /exercises/exercise_12_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 12 exercise 2 2 | 3 | The C program introduced in the previous exercise has been changed. This time, the program prompts the user for an integer and stores it in the variable x. The program prints the number, calls the subroutine "change_number" passing the address of the variable x as an argument, and finally prints the value of the variable x again: 4 | 5 | ```c 6 | #include 7 | 8 | void change_number(int *x); 9 | 10 | int main(void) { 11 | int x; 12 | printf("Enter the number x: "); 13 | scanf("%d", &x); 14 | printf("In the main program: x = %d\n", x); 15 | change_number(&x); 16 | printf("In the main program: x = %d\n", x); 17 | } 18 | ``` 19 | 20 | Your task is to write the subroutine "change_number" that takes the address of the variable x, increases the value of the variable by three and prints the changed number as shown in the example printout. As you can see in the example printout, the subroutine will permanently change the value of the variable x in the main program. 21 | 22 | _Example output_ 23 | 24 | ``` 25 | Enter the number x: 555 26 | In the main program: x = 555 27 | In the subroutine: x = 558 28 | In the main program: x = 558 29 | ``` 30 | -------------------------------------------------------------------------------- /exercises/exercise_12_2/exercise_12_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void change_number(int *x); 4 | 5 | int main(void) { 6 | int x; 7 | printf("Enter the number x: "); 8 | scanf("%d", &x); 9 | printf("In the main program: x = %d\n", x); 10 | change_number(&x); 11 | printf("In the main program: x = %d\n", x); 12 | } 13 | 14 | void change_number(int *x) { 15 | *x += 3; 16 | printf("In the subroutine: x = %d\n", *x); 17 | } 18 | -------------------------------------------------------------------------------- /exercises/exercise_13_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 13 exercise 1 2 | 3 | our task is to write a subroutine that takes a pointer to the first element of an integer array, as well as an integer variable. The arguments are passed to the subroutine in this order. The integer variable passed as an argument holds the number of elements in an array, while the actual array holds the number of integers specified by the variable. 4 | 5 | The subroutine must go through the table and return the address of the element holding the smallest value to the calling program. After receiving the address, the main program uses it to print the smallest number contained in the array. 6 | 7 | The prototype of the subroutine is the following: 8 | 9 | ```c 10 | int *address_of_smallest_value(int *numbers, int size); 11 | ``` 12 | 13 | In the above, the * operator indicates that the function's return value is a pointer. 14 | 15 | _Example output_ 16 | 17 | ``` 18 | The smallest number in the array is: 3 19 | ``` 20 | -------------------------------------------------------------------------------- /exercises/exercise_13_1/exercise_13_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *address_of_smallest_value(int *numbers, int size); 4 | 5 | int main(void) { 6 | 7 | int numbers[] = { 21, 55, 5, 3, 53 }; 8 | int size = 5; 9 | 10 | int *smallest = address_of_smallest_value(numbers, size); 11 | 12 | printf("The smallest number in the array is: %d", *smallest); 13 | 14 | return 0; 15 | 16 | } 17 | 18 | int *address_of_smallest_value(int *numbers, int size) { 19 | int *smallest = numbers, i; 20 | for (i = 0; i < size; i++) { 21 | if (numbers[i] < *smallest) smallest = &numbers[i]; 22 | } 23 | return smallest; 24 | } 25 | -------------------------------------------------------------------------------- /exercises/exercise_13_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 13 exercise 2 2 | 3 | The C program below receives integers as command line arguments. After receiving the required values, the program calls the subroutine "calculate_and_print" which receives an array and its size passed by the main program, prints the numbers on the screen and prints their sum on the screen. 4 | 5 | ```c 6 | #include 7 | #include 8 | 9 | void calculate_and_print(int *array, int size); 10 | 11 | int main (int argc, char *argv[]) { 12 | int x, size = 5, array[5]; 13 | if (argc == 6) { 14 | for (x = 0; x < argc - 1; x++) array[x] = atoi(argv[x + 1]); 15 | calculate_and_print(array, size); 16 | } else { 17 | printf("Incorrect number of command line arguments.\n"); 18 | } 19 | return 0; 20 | } 21 | ``` 22 | 23 | Your task is to write the subroutine "calculate_and_print". The example printout shows how to print the numbers. The main program includes things that are outside the scope of this course, but you only need to write a subroutine that serves the main program. 24 | 25 | _Example output_ 26 | 27 | ``` 28 | Elements of the array: 43 53 654 65 77 29 | Sum = 892 30 | ``` 31 | -------------------------------------------------------------------------------- /exercises/exercise_13_2/exercise_13_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void calculate_and_print(int *array, int size); 5 | 6 | int main (int argc, char *argv[]) { 7 | int x, size = 5, array[5]; 8 | if (argc == 6) { 9 | for (x = 0; x < argc - 1; x++) array[x] = atoi(argv[x + 1]); 10 | calculate_and_print(array, size); 11 | } else { 12 | printf("Incorrect number of command line arguments.\n"); 13 | } 14 | return 0; 15 | } 16 | 17 | void calculate_and_print(int *array, int size) { 18 | int sum = 0, i; 19 | printf("Elements of the array: "); 20 | for (i = 0; i < size; i++) { 21 | printf(" %d", array[i]); 22 | sum += array[i]; 23 | } 24 | printf("\nSum = %d\n", sum); 25 | } 26 | -------------------------------------------------------------------------------- /exercises/exercise_1_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 exercise 1 2 | 3 | The purpose of this exercise is to introduce you to authoring programs in the C language. The following is a program in C. Your task is to rewrite the program. In the next chapter, you will learn how the program works and what it does. 4 | 5 | ```c 6 | #include 7 | /* This is a fine program */ 8 | 9 | int main(void) { 10 | int number; 11 | printf("Enter a number:"); 12 | scanf("%d", &number); 13 | printf("The number was: %d\n", number); 14 | return 0; 15 | } 16 | ``` 17 | 18 | _Example output_ 19 | 20 | ``` 21 | Enter a number:83 22 | The number was: 83 23 | ``` 24 | -------------------------------------------------------------------------------- /exercises/exercise_1_1/exercise_1_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int number; 5 | printf("Enter a number: "); 6 | scanf("%d", &number); 7 | printf("The number was: %d\n", number); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /exercises/exercise_2_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 2 exercise 1 2 | 3 | Write a functioning program in C that, upon execution, prints the following on the screen: "Hello everybody!", followed by a newline character \n after the exclamation point. After printing, the program closes itself. 4 | 5 | **Hint:** 6 | Everything required for creating the program can be found in the example program in Chapter 2. Once you have understood the operating principle of the example, it should be easy to write your program. You do not need any boxes or variables in this program. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Hello everybody! 12 | ``` 13 | -------------------------------------------------------------------------------- /exercises/exercise_2_1/exercise_2_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | printf("Hello everybody!\n"); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /exercises/exercise_2_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 2 exercise 2 2 | 3 | Write a program that prompts the user for an integer, calculates the square and prints the result on the screen (also print out a newline character \n after the result). After printing, the program closes itself. 4 | 5 | **Hint:** 6 | The program can be written in quite much the same way as the example in the chapter. You can implement the program in many ways, but you will probably need two variables: one for the number entered by the user and one for the square. In the example, the addition operator + was used for calculating a sum. In this program, you need the multiplication operator *. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Enter an integer: 11 12 | The square of the number you entered is 121 13 | ``` 14 | -------------------------------------------------------------------------------- /exercises/exercise_2_2/exercise_2_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int number = 0; 5 | printf("Enter an integer: "); 6 | scanf("%d", &number); 7 | printf("The square of the number you entered is %d\n", number * number); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /exercises/exercise_3_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 exercise 1 2 | 3 | Write a program that first asks the user for an integer and after that, a floating-point number. Finally, the program prints both numbers on the screen. The floating-point number shall be printed with two decimal places of precision. 4 | 5 | _Example output_ 6 | 7 | ``` 8 | Enter an integer: 14 9 | Enter a decimal number:3.123 10 | 11 | You entered the integer: 14 12 | You entered the decimal number, rounded to two decimal places: 3.12 13 | ``` 14 | -------------------------------------------------------------------------------- /exercises/exercise_3_1/exercise_3_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int int_number = 0; 6 | float float_number = 0.0; 7 | 8 | printf("Enter an integer: "); 9 | scanf("%d", &int_number); 10 | 11 | printf("Enter a decimal number: "); 12 | scanf("%f", &float_number); 13 | 14 | printf("You entered the integer: %d\n", int_number); 15 | printf("You entered the decimal number, rounded to two decimal places: %.2f\n", float_number); 16 | 17 | return 0; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /exercises/exercise_3_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 exercise 2 2 | 3 | Write a program that prompts the user for an amount in Finnish markka and converts it to euro. Finally, the program prints the amount on the screen in euro with two decimal places of precision. The euro conversion factor is 5.94573. 4 | 5 | **Hint:** 6 | You need three variables: one for the conversion factor, one for markka and one for euro. The required division can be accomplished by using the "/" character. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Enter an amount in FIM:9.90 12 | FIM converted to euro: 1.67 13 | ``` 14 | -------------------------------------------------------------------------------- /exercises/exercise_3_2/exercise_3_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | float fim = 0.0, rate = 5.94573; 6 | 7 | printf("Enter an amount in FIM: "); 8 | scanf("%f", &fim); 9 | 10 | printf("FIM converted to euro: %.2f", fim / rate); 11 | 12 | return 0; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /exercises/exercise_4_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 exercise 1 2 | 3 | Write a program that prompts the user for two integers and prints the sum, difference and product of the numbers on the screen. 4 | 5 | **Hint:** 6 | This exercise is an excellent opportunity for practicing how to print several variables in one printf() statement. The variables in the program are integer variables. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Enter the first number:83 12 | Enter the second number:78 13 | 14 | 83+78=161 15 | 83-78=5 16 | 83*78=6474 17 | ``` 18 | -------------------------------------------------------------------------------- /exercises/exercise_4_1/exercise_4_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int first_num = 0, second_num = 0; 6 | 7 | printf("Enter the first number: "); 8 | scanf("%d", &first_num); 9 | printf("Enter the second number: "); 10 | scanf("%d", &second_num); 11 | 12 | printf("%d+%d=%d\n", first_num, second_num, first_num + second_num); 13 | printf("%d-%d=%d\n", first_num, second_num, first_num - second_num); 14 | printf("%d*%d=%d\n", first_num, second_num, first_num * second_num); 15 | 16 | return 0; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /exercises/exercise_4_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 exercise 2 2 | 3 | Write a program that prompts the user for an integer and checks whether it is even or odd. If the number is even, print the value 0, and if it is odd, print the value 1. 4 | 5 | **Hint:** 6 | A number is even if the remainder from division by two is zero. 7 | 8 | 9 | _Example output_ 10 | 11 | ``` 12 | Enter an integer: 83 13 | The number is 1 14 | ``` 15 | -------------------------------------------------------------------------------- /exercises/exercise_4_2/exercise_4_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int integer = 0; 6 | 7 | printf("Enter an integer: "); 8 | scanf("%d", &integer); 9 | 10 | int result = integer % 2; 11 | printf("The number is %d", result); 12 | 13 | return 0; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /exercises/exercise_5_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 exercise 1 2 | 3 | Write a program that prompts the user for an integer and checks whether it is even or odd. If the number is even, print "Number 2 is even.", if it is odd, print "Number 1 is odd.". 4 | 5 | **Hint:** 6 | The program is almost the same as in the previous chapter. The new part is: If the remainder is zero, print "even". If the remainder is one, print "odd". Otherwise print an error message. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Enter an integer: 1978 12 | Number 1978 is even. 13 | ``` 14 | -------------------------------------------------------------------------------- /exercises/exercise_5_1/exercise_5_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int integer; 6 | 7 | printf("Enter an integer: "); 8 | scanf("%d", &integer); 9 | 10 | if (integer % 2) { 11 | printf("Number %d is odd.", integer); 12 | } else { 13 | printf("Number %d is even.", integer); 14 | } 15 | 16 | return 0; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /exercises/exercise_5_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 exercise 2 2 | 3 | Write a program that first asks the user whether they drink coffee or tea and then how many cups a day. Finally, the program prints a statement in accordance with the following instructions: Coffee 0 to 2 cups: "You don't drink a lot of coffee, do you?", coffee 3-20 cups: "You drink a lot of coffee!", Tea 0-2 cups: "You do not drink a lot of tea.", tea 3-20 cups: "You drink a lot of tea!", Otherwise "An error occurred in the program!" The number of cups shall be processed as an integer variable and the drink as a character variable (c/t). You do not need to account for upper case letters. 4 | 5 | **Hint:** 6 | Spend enough time on program design before you start typing your program. You can get help for the exercise in the learning material for Chapter 4. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Do you drink coffee or tea (c/t)?s 12 | How many cups do you drink daily:55 13 | An error occurred in the program! 14 | ``` 15 | -------------------------------------------------------------------------------- /exercises/exercise_5_2/exercise_5_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | char drink; 6 | int cups = 0; 7 | 8 | printf("Do you drink coffee or tea (c/t)?"); 9 | scanf("%c", &drink); 10 | 11 | printf("How many cups do you drink daily: "); 12 | scanf("%d", &cups); 13 | 14 | if (drink == 'c' && cups >= 0 && cups <= 2) { 15 | printf("You don't drink a lot of coffee, do you?\n"); 16 | } else if (drink == 'c' && cups >= 3 && cups <= 20) { 17 | printf("You drink a lot of coffee!\n"); 18 | } else if (drink == 't' && cups >= 0 && cups <= 2) { 19 | printf("You do not drink a lot of tea.\n"); 20 | } else if (drink == 't' && cups >= 3 && cups <= 20) { 21 | printf("You drink a lot of tea!\n"); 22 | } else { 23 | printf("An error occurred in the program!"); 24 | } 25 | 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /exercises/exercise_5_3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 exercise 3 2 | 3 | Write a simple calculator program that calculates the difference, sum or product of two numbers. First, the program asks which calculation to perform. After this, the program prompts for the numbers, performs the calculation and prints the result. The selection within the program has to be done using a switch() statement. 4 | 5 | _Example output_ 6 | 7 | ``` 8 | 1: subtraction 9 | 2: addition 10 | 3: multiplication 11 | Select function:3 12 | Enter the first number:45 13 | Enter the second number:67 14 | 45*67=3015 15 | ``` 16 | -------------------------------------------------------------------------------- /exercises/exercise_5_3/exercise_5_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int first_num = 0, second_num = 0, func = 0; 6 | 7 | printf("1: subtraction\n2: addition\n3: multiplication\nSelect function: "); 8 | scanf("%d", &func); 9 | 10 | printf("Enter the first number: "); 11 | scanf("%d", &first_num); 12 | printf("Enter the second number: "); 13 | scanf("%d", &second_num); 14 | 15 | switch (func) { 16 | case 1: { 17 | printf("%d-%d=%d", first_num, second_num, first_num - second_num); 18 | break; 19 | } 20 | case 2: { 21 | printf("%d+%d=%d", first_num, second_num, first_num + second_num); 22 | break; 23 | } 24 | case 3: { 25 | printf("%d*%d=%d", first_num, second_num, first_num * second_num); 26 | break; 27 | } 28 | default: { 29 | printf("An error has occurred while running the program."); 30 | break; 31 | } 32 | } 33 | 34 | return 0; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /exercises/exercise_6_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 exercise 1 2 | 3 | Write a program that prompts the user for an integer n (n>0) and prints the numbers 1,2,3...n on the screen one below the other. Write the program using the FOR statement. 4 | 5 | _Example output_ 6 | 7 | ``` 8 | Enter an integer: 3 9 | 1 10 | 2 11 | 3 12 | ``` 13 | -------------------------------------------------------------------------------- /exercises/exercise_6_1/exercise_6_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int integer = 0; 6 | 7 | printf("Enter an integer: "); 8 | scanf("%d", &integer); 9 | 10 | int i; 11 | for (i = 1; i <= integer; i++) { 12 | printf("%d\n", i); 13 | } 14 | 15 | return 0; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /exercises/exercise_6_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 exercise 2 2 | 3 | Write a program that prompts the user for an integer n (n>0) and prints the factorial of the number on the screen. For example, the factorial of n is designated n!, which means the number calculated as follows: 1*2*3...*n 4 | 5 | _Example output_ 6 | 7 | ``` 8 | Enter an integer: 3 9 | The factorial of 3 is 6 10 | ``` 11 | -------------------------------------------------------------------------------- /exercises/exercise_6_2/exercise_6_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int integer = 0, factorial = 1; 6 | 7 | printf("Enter an integer: "); 8 | scanf("%d", &integer); 9 | 10 | int i; 11 | for (i = 1; i <= integer; i++) { 12 | factorial *= i; 13 | } 14 | 15 | printf("The factorial of %d is %d\n", integer, factorial); 16 | 17 | return 0; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /exercises/exercise_6_3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 exercise 3 2 | 3 | Write a program that asks for the students' exam scores (using integers 4 to 10) and calculates the average. The program must accept scores until entry is terminated by a negative integer. Finally, the program prints out the number of scores and the calculated average with two decimal places of precision. 4 | 5 | **Hint:** 6 | You can write your program using either the while or do...while statement. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | The program calculates the average of scores you enter. 12 | End with a negative integer. 13 | Enter score (4-10):7 14 | Enter score (4-10):8 15 | Enter score (4-10):9 16 | Enter score (4-10):10 17 | Enter score (4-10):4 18 | Enter score (4-10):4 19 | Enter score (4-10):5 20 | Enter score (4-10):-1 21 | You entered 7 scores. 22 | Average score: 6.71 23 | ``` 24 | -------------------------------------------------------------------------------- /exercises/exercise_6_3/exercise_6_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int count = 0, sum = 0, n = 0; 6 | 7 | printf("The program calculates the average of scores you enter.\nEnd with a negative integer.\n"); 8 | 9 | do { 10 | printf("Enter score (4-10):"); 11 | scanf("%d", &n); 12 | if (n >= 4 && n <= 10) { 13 | sum = sum + n; 14 | count++; 15 | } 16 | } while (n >= 0); 17 | 18 | float average = 1.0f * sum / count; 19 | 20 | printf("You entered %d scores.\nAverage score: %.2f\n", count, average); 21 | 22 | return 0; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /exercises/exercise_7_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7 exercise 1 2 | 3 | Your task is to supplement the source code below with the missing functions. The program prints the following text in accordance with the user's selections: 4 | 5 | ``` 6 | Choice 1: "The cat says meow!" 7 | Choice 2: "The dog says wuff-wuff!" 8 | Choice 3: "The cow says moo!" 9 | ``` 10 | 11 | Upon execution, the program asks the user to select one of the animals. Once selected, the program calls a function that prints out the correct utterance. After executing the function, the program returns to ask the user for a new choice. The program is terminated by a negative integer. 12 | 13 | Code to be supplemented: 14 | 15 | ```c 16 | #include 17 | 18 | void cat(void); 19 | void dog(void); 20 | void cow(void); 21 | 22 | int main(void) { 23 | int choice; 24 | do { 25 | printf("1: cat\n2: dog\n3: cow\n"); 26 | printf("End with a negative number.\n"); 27 | printf("Select animal: "); 28 | scanf("%d", &choice); 29 | if(choice < 0) { 30 | printf("Terminating the program..."); 31 | } else if(choice == 1) { 32 | cat(); 33 | } else if(choice == 2) { 34 | dog(); 35 | } else if(choice == 3) { 36 | cow(); 37 | } else { 38 | printf("You entered an invalid number.\n\n"); 39 | } 40 | } while(choice>0); 41 | 42 | return 0; 43 | } 44 | ``` 45 | 46 | **Hint:** The purpose of the exercise is to provide you with a feeling for writing functions. Do not copy the source code, only write functions that print the output. The functions are declared in the prototypes before the main program. 47 | 48 | _Example output_ 49 | 50 | ``` 51 | 1:cat 52 | 2:dog 53 | 3:cow 54 | End with a negative number. 55 | Select animal:1 56 | The cat says meow! 57 | 58 | 1:cat 59 | 2:dog 60 | 3:cow 61 | End with a negative number. 62 | Select animal:2 63 | The dog says wuff-wuff! 64 | 65 | 1:cat 66 | 2:dog 67 | 3:cow 68 | End with a negative number. 69 | Select animal:3 70 | The cow says moo! 71 | 72 | 1:cat 73 | 2:dog 74 | 3:cow 75 | End with a negative number. 76 | Select animal:-1 77 | Terminating the program... 78 | ``` 79 | -------------------------------------------------------------------------------- /exercises/exercise_7_1/exercise_7_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void cat(void); 4 | void dog(void); 5 | void cow(void); 6 | 7 | int main(void) { 8 | int choice; 9 | do { 10 | printf("1: cat\n2: dog\n3: cow\n"); 11 | printf("End with a negative number.\n"); 12 | printf("Select animal: "); 13 | scanf("%d", &choice); 14 | if(choice < 0) { 15 | printf("Terminating the program..."); 16 | } else if(choice == 1) { 17 | cat(); 18 | } else if(choice == 2) { 19 | dog(); 20 | } else if(choice == 3) { 21 | cow(); 22 | } else { 23 | printf("You entered an invalid number.\n\n"); 24 | } 25 | } while(choice>0); 26 | 27 | return 0; 28 | } 29 | 30 | void cat(void) { 31 | printf("The cat says meow!\n"); 32 | } 33 | 34 | void dog(void) { 35 | printf("The dog says wuff-wuff!\n"); 36 | } 37 | 38 | void cow(void) { 39 | printf("The cow says moo!\n"); 40 | } 41 | -------------------------------------------------------------------------------- /exercises/exercise_7_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7 exercise 2 2 | 3 | Write a calculator program that calculates the sum, difference or product of two integers. The program asks for the type of calculation first (1 = sum, 2 = difference, 3 = product). After calculation, the result is printed on the screen and the program returns to prompt for the next calculation. 4 | 5 | You should write a function for each calculation, prompting for the numbers, performing the desired calculation and printing the answer on the screen. The program is terminated by entering a negative number just like in the previous exercise. 6 | 7 | **Hint:** 8 | You can use the previous exercise as the body of your program. The functions carrying out the calculations and printing do not take any parameters or return any value. 9 | 10 | _Example output_ 11 | 12 | ``` 13 | 1: sum of two numbers 14 | 2: difference of two numbers 15 | 3: product of two numbers 16 | <0: terminate the program 17 | Select calculation:1 18 | Enter the first number:55 19 | Enter the second number:33 20 | 55 + 33 = 88 21 | 22 | 1: sum of two numbers 23 | 2: difference of two numbers 24 | 3: product of two numbers 25 | <0: terminate the program 26 | Select calculation:2 27 | Enter the first number:14 28 | Enter the second number:41 29 | 14 - 41 = -27 30 | 31 | 1: sum of two numbers 32 | 2: difference of two numbers 33 | 3: product of two numbers 34 | <0: terminate the program 35 | Select calculation:3 36 | Enter the first number:4 37 | Enter the second number:5 38 | 4 * 5 = 20 39 | 40 | 1: sum of two numbers 41 | 2: difference of two numbers 42 | 3: product of two numbers 43 | <0: terminate the program 44 | Select calculation:-1 45 | Terminating the program... 46 | ``` 47 | -------------------------------------------------------------------------------- /exercises/exercise_7_2/exercise_7_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int function = 0, first_num = 0, second_num = 0; 6 | 7 | do { 8 | 9 | printf("1: sum of two numbers\n2: difference of two numbers\n3: product of two numbers\n<0: terminate the program\nSelect calculation: "); 10 | scanf("%d", &function); 11 | 12 | if (function < 0) break; 13 | 14 | printf("Enter the first number: "); 15 | scanf("%d", &first_num); 16 | printf("Enter the second number: "); 17 | scanf("%d", &second_num); 18 | 19 | switch (function) { 20 | case 1: { 21 | printf("%d+%d=%d", first_num, second_num, first_num + second_num); 22 | break; 23 | } 24 | case 2: { 25 | printf("%d-%d=%d", first_num, second_num, first_num - second_num); 26 | break; 27 | } 28 | case 3: { 29 | printf("%d*%d=%d", first_num, second_num, first_num * second_num); 30 | break; 31 | } 32 | default: { 33 | printf("Invalid function selected."); 34 | } 35 | } 36 | 37 | } while (function >= 0); 38 | 39 | printf("Terminating the program...\n"); 40 | 41 | return 0; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /exercises/exercise_7_3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7 exercise 3 2 | 3 | Write a program that takes three integers as input and prints the smallest and largest of these numbers. The main program must prompt for integers and read them. Write the functions largest() and smallest() that receive the entered numbers as their parameters. Correspondingly, the functions shall return values corresponding to their names. 4 | 5 | **Hint:** 6 | You can use combined comparisons and the if-else structure in your program. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | Enter the 1. number:12355 12 | Enter the 2. number:32145 13 | Enter the 3. number:22112 14 | Among the numbers you entered, 15 | the largest was 32145 and the smallest was 12355. 16 | ``` 17 | -------------------------------------------------------------------------------- /exercises/exercise_7_3/exercise_7_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int largest(int first, int second, int third); 4 | int smallest(int first, int second, int third); 5 | 6 | int main(void) { 7 | 8 | int first = 0, second = 0, third = 0; 9 | 10 | printf("Enter the 1. number: "); 11 | scanf("%d", &first); 12 | printf("Enter the 2. number: "); 13 | scanf("%d", &second); 14 | printf("Enter the 3. number: "); 15 | scanf("%d", &third); 16 | 17 | int largest_num = largest(first, second, third); 18 | int smallest_num = smallest(first, second, third); 19 | 20 | printf("Among the numbers you entered,\nthe largest was %d and the smallest was %d.", largest_num, smallest_num); 21 | 22 | return 0; 23 | 24 | } 25 | 26 | int largest(int first, int second, int third) { 27 | int result = first; 28 | if (second > result) result = second; 29 | if (third > result) result = third; 30 | return result; 31 | } 32 | 33 | int smallest(int first, int second, int third) { 34 | int result = first; 35 | if (second < result) result = second; 36 | if (third < result) result = third; 37 | return result; 38 | } 39 | -------------------------------------------------------------------------------- /exercises/exercise_8_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 8 exercise 1 2 | 3 | Write a program that calculates the number of hours worked within a specific period and prints the total number of hours, the average length of a day and an itemisation of the hours entered. First, the program must ask how many days of working hours shall be entered (max 30 days). After this, the program asks for the daily working hours. The program output shall have one decimal place of precision. 4 | 5 | **Hint:** 6 | It is easiest to implement the program using an array with 30 elements. 7 | 8 | _Example output_ 9 | 10 | ``` 11 | The program calculates the total hours worked during 12 | a specific period and the average length of a day. 13 | 14 | How many days:3 15 | Enter the working hours for day 1:5.5 16 | Enter the working hours for day 2:6.6 17 | Enter the working hours for day 3:7.7 18 | 19 | Total hours worked: 19.8 20 | Average length of day: 6.6 21 | Hours entered: 5.5 6.6 7.7 22 | ``` 23 | -------------------------------------------------------------------------------- /exercises/exercise_8_1/exercise_8_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int days = 0; 6 | 7 | printf("The program calculates the total hours worked during\na specific period and the average length of a day.\n"); 8 | printf("How many days:"); 9 | scanf("%d", &days); 10 | 11 | if (days > 30) return 0; 12 | 13 | float hours[days]; 14 | 15 | int i; 16 | for (i = 0; i < days; i++) { 17 | printf("Enter the working hours for day %d:", i + 1); 18 | scanf("%f", &hours[i]); 19 | } 20 | 21 | float total = 0.0; 22 | for (i = 0; i < days; i++) total += hours[i]; 23 | printf("\nTotal hours worked: %.1f\n", total); 24 | 25 | float average = total / days; 26 | printf("Average length of day: %.1f\n", average); 27 | 28 | printf("Hours entered:"); 29 | for (i = 0; i < days; i++) printf(" %.1f", hours[i]); 30 | 31 | return 0; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /exercises/exercise_8_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 8 exercise 2 2 | 3 | The following is the initialisation of a 5 x 5 integer array: 4 | 5 | ```c 6 | int matrix[5][5] = { 7 | 4, 6, 25, 88, 5, 8 | 34, 5, 300, 4, 65, 9 | 78, 43, 11, 90, 125, 10 | 98, 585, 12, 63, 21, 11 | 45, 35, 9, 5, 1 12 | }; 13 | ``` 14 | 15 | Copy the initialisation into a program that prints the array on the screen and calculates the sum of the elements. Each line shall be followed by a newline character. 16 | 17 | **Hint:** 18 | The program can be implemented using nested for statements. You can use an if statement to check whether a newline is required. The numbers on the lines of the matrix must be separated by single spaces. 19 | 20 | _Example output_ 21 | 22 | ``` 23 | In the array: 24 | 4 6 25 88 5 25 | 34 5 300 4 65 26 | 78 43 11 90 125 27 | 98 585 12 63 21 28 | 45 35 9 5 1 29 | 30 | 31 | the sum of the elements is 1757 32 | ``` 33 | -------------------------------------------------------------------------------- /exercises/exercise_8_2/exercise_8_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | int sum = 0; 6 | int matrix[5][5] = { 7 | 4, 6, 25, 88, 5, 8 | 34, 5, 300, 4, 65, 9 | 78, 43, 11, 90, 125, 10 | 98, 585, 12, 63, 21, 11 | 45, 35, 9, 5, 1 12 | }; 13 | 14 | printf("In the array:\n"); 15 | 16 | int i, j; 17 | for (i = 0; i < 5; i++) { 18 | for (j = 0; j < 5; j++) { 19 | sum += matrix[i][j]; 20 | if (j == 4) { 21 | printf("%d\n", matrix[i][j]); 22 | } else { 23 | printf("%d\t", matrix[i][j]); 24 | } 25 | } 26 | } 27 | 28 | printf("\n\nthe sum of the elements is %d", sum); 29 | 30 | return 0; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /exercises/exercise_9_1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9 exercise 1 2 | 3 | Write a program that prints the text "Hello world!" into the file "hello.usr". The file does not exist, so it must be created. Finally, the program must print a message on the screen indicating that writing to the file was successful. The text printed to the file must exactly match the assignment. 4 | 5 | _Example output_ 6 | 7 | ``` 8 | Writing to the file was successful. 9 | Closing the program. 10 | ``` 11 | -------------------------------------------------------------------------------- /exercises/exercise_9_1/exercise_9_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | FILE *file = fopen("hello.usr", "w"); 6 | 7 | fprintf(file, "Hello world!\n"); 8 | 9 | printf("Writing to the file was successful.\nClosing the program.\n"); 10 | 11 | fclose(file); 12 | 13 | return 0; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /exercises/exercise_9_2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9 exercise 2 2 | 3 | The file "numbers.s" contains 4 integers. The numbers are on the first line of the file, separated by spaces. Write a program that reads the integers from the file and calculates their sum. Finally, the program prints the numbers and the sum on the screen. 4 | 5 | _Example output_ 6 | 7 | ``` 8 | Numbers found in the file numbers.s: 9 | 93, 432, 65 and 22 10 | 11 | Sum of the numbers: 612 12 | ``` 13 | -------------------------------------------------------------------------------- /exercises/exercise_9_2/exercise_9_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | FILE *file = fopen("numbers.s", "r"); 6 | 7 | if (file == NULL) { 8 | printf("Unable to open file for reading."); 9 | return 0; 10 | } else { 11 | 12 | int nums[4]; 13 | 14 | int i; 15 | for (i = 0; i < 4; i++) fscanf(file, "%d", &nums[i]); 16 | 17 | printf("Numbers found in the file numbers.s:\n"); 18 | printf("%d, %d, %d and %d", nums[0], nums[1], nums[2], nums[3]); 19 | 20 | int sum = nums[0] + nums[1] + nums[2] + nums[3]; 21 | printf("Sum of the numbers: %d", sum); 22 | 23 | fclose(file); 24 | 25 | } 26 | 27 | return 0; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /exercises/exercise_9_3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9 exercise 3 2 | 3 | The files "mata.txt" and "matb.txt" contain integer matrices of size 10 x 10. Write a program that calculates the sum of the matrices in a new matrix. The resulting sum matrix shall be saved to the file "sum.usr". 4 | 5 | The matrix elements are separated by spaces and newlines. For example: 6 | 7 | ``` 8 | 1 2 3 4 5 6 7 8 9 10 9 | 11 12 13 14 15 16 17 18 19 20 10 | ... ... ... 11 | ... ... ... 12 | 91 92 93 94 95 96 97 98 99 100 13 | ``` 14 | The sum matrix must be saved in the same format. At the end, the program prints a message indicating successful completion. 15 | 16 | _Example output_ 17 | 18 | ``` 19 | The sum of the matrices has been calculated into the file sum.usr. 20 | ``` 21 | -------------------------------------------------------------------------------- /exercises/exercise_9_3/exercise_9_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | 5 | FILE *mata = fopen("mata.txt", "r"); 6 | FILE *matb = fopen("matb.txt", "r"); 7 | FILE *sum = fopen("sum.usr", "w"); 8 | 9 | if (mata == NULL || matb == NULL) { 10 | printf("Unable to open files for reading."); 11 | return 0; 12 | } else { 13 | 14 | int i, j, a, b; 15 | for (i = 0; i < 10; i++) { 16 | for (j = 0; j < 10; j++) { 17 | fscanf(mata, "%d", &a); 18 | fscanf(matb, "%d", &b); 19 | if (j == 9) { 20 | fprintf(sum, "%d\n", a + b); 21 | } else { 22 | fprintf(sum, "%d\t", a + b); 23 | } 24 | } 25 | } 26 | 27 | printf("The sum of the matrices has been calculated into the file sum.usr."); 28 | 29 | fclose(mata); 30 | fclose(matb); 31 | fclose(sum); 32 | 33 | } 34 | 35 | return 0; 36 | 37 | } 38 | --------------------------------------------------------------------------------