Autor: 09.11.2023
C Language - Practical Exercises with Solutions
In this article, we will present three simple tasks in the C language. These are examples of tasks you may encounter on exams or quizzes. For each task, we have provided a problem description, sample function tests, and a brief explanation of the solution.
C - Practice Challenges
Expand your coding skills in the C language. Complete a variety of diverse tasks. Years go by, and the C language is still used in many fields. It is used for writing drivers, operating systems, programming microcontrollers, and in many other specialized areas. C is also used in educational institutions and is often the first encounter with programming for many students. If you already know the basics of this language and want to learn how to solve practical tasks, take advantage of this course. Learn more
Using Enumerated Types
Write a program in the C language that uses an enumerated type called enum Directions to represent geographical directions NORTH, SOUTH, EAST, WEST. Then, define a function get_direction() that takes a direction (a value of type enum Directions) as an argument and returns the corresponding direction character ('N' - north, 'S' - south, 'E' - east, 'W' - west).
We expect the get_direction() function to pass the following tests:
- get_direction(EAST) -> E
- get_direction(NORTH) -> N
- get_direction(WEST) -> W
- get_direction(SOUTH) -> S
How can you approach this problem? Here's our proposed solution:
#include <stdio.h>
enum Directions {NORTH, SOUTH, EAST, WEST};
char get_direction(enum Directions dir) {
char sign_direction[] = {'N', 'S', 'E', 'W'};
return sign_direction[dir];
}
int main(void) {
printf("%c \n", get_direction(NORTH));
printf("%c \n", get_direction(SOUTH));
printf("%c \n", get_direction(EAST));
printf("%c \n", get_direction(WEST));
return 0;
}
After compiling the code, everything works correctly. Our task is an example of the practical use of an enumerated type in the C language.
Task - Function Operating on Characters
Write a function in the C language that can convert a character representing a digit to an integer.
We expect the convert_to_digit() function to pass the following tests:
- convert_to_digit('1') -> 1
- convert_to_digit('5') -> 5
- convert_to_digit('7') -> 7
- convert_to_digit('0') -> 0
Here's our proposed solution:
#include <stdio.h>
#include <stdlib.h>
int convert_to_digit(char c) {
char digit[2] = {c, '\0'};
return atoi(digit);
}
int main(void) {
printf("%d \n", convert_to_digit('1'));
return 0;
}
The solution is straightforward. We use the atoi() function from the stdlib library to convert a character string to an integer.
Using Pointers and Structures
Implement a calculate_total() function in the C language that takes a pointer to a Product structure, representing a product in a store. The Product structure contains a price field (the product's price) and a quantity field (the quantity of the product). The calculate_total() function should calculate the total cost of purchasing the given product (price * quantity) and return it as the result.
We expect the calculate_total() function to pass the following tests:
- calculate_total(&products[0]) -> 2400
- calculate_total(&products[1]) -> 2100
- calculate_total(&products[2]) -> 0
As usual, we'll provide a solution to the task:
#include <stdio.h>
struct Product {
double price;
int quantity;
};
struct Product products[3] = {{1200.0, 2}, {2100.0, 1}, {0.0, 10}};
double calculate_total(struct Product *product) {
return product->price * product->quantity;
}
int main(void) {
double total1 = calculate_total(&products[0]);
printf("Total for the first product: %.2f\n", total1);
double total2 = calculate_total(&products[1]);
printf("Total for the second product: %.2f\n", total2);
double total3 = calculate_total(&products[2]);
printf("Total for the third product: %.2f\n", total3);
return 0;
}
In the code, we have a calculate_total() function that takes a pointer to a Product structure and returns the result of multiplying the product's price by its quantity.
C - Practice Challenges
Expand your coding skills in the C language. Complete a variety of diverse tasks. Years go by, and the C language is still used in many fields. It is used for writing drivers, operating systems, programming microcontrollers, and in many other specialized areas. C is also used in educational institutions and is often the first encounter with programming for many students. If you already know the basics of this language and want to learn how to solve practical tasks, take advantage of this course. Learn more
Summary
The three tasks in this article are just selected examples of tasks you may encounter on an exam. So, consider them as a slice of knowledge that you need to have.