Implementing Core Computational Functions in C Programming
Task 1: Converting a Numerical Score to a Letter Grade
The function grade_converter maps an integer score to a corresponding letter grade ('A' through 'E'). The functon takes an int parameter and returns a char value. A corrected implementation of a similar switch-case structure is shown below:
char grade_converter(int score_val) {
const char* grade;
switch(score_val / 10) {
case 10:
case 9:
grade = "A";
break;
case 8:
grade = "B";
break;
case 7:
grade = "C";
break;
case 6:
grade = "D";
break;
default:
grade = "E";
}
return grade[0];
}
Key issues in a flawed implementation include type mismatch (assigning a const char* to a char variable) and missing break statements, which cause fall-through errors in the switch block.
Task 2: Summing the Digits of an Integer
The objective is to calculate the sum of all digits in a given integer. This can be achieved through both iterative and recursive methods.
// Iterative approach
int sum_digits_iterative(int num) {
int total = 0;
while(num > 0) {
total += num % 10;
num /= 10;
}
return total;
}
// Recursive approach
int sum_digits_recursive(int num) {
if(num == 0) return 0;
return (num % 10) + sum_digits_recursive(num / 10);
}
Both methods produce identical results. The iterattive version uses a loop, while the recursive version relies on self-calls to break down the problem.
Task 3: Computing Exponential Values
The function compute_power calculates x raised to the power of n. It is implemented recursively.
double compute_power(double base, int exp) {
if(exp == 0) return 1.0;
return base * compute_power(base, exp - 1);
}
Task 4: Identifying Twin Prime Numbers
This program finds and counts all twin prime pairs (primes differing by 2) within 100.
#include <stdio.h>
#include <math.h>
int check_prime(int value);
int main() {
int pair_count = 0;
printf("Twin primes under 100:\n");
for(int i = 3; i + 2 <= 100; i++) {
if(check_prime(i) && check_prime(i + 2)) {
printf("%d %d\n", i, i + 2);
pair_count++;
}
}
printf("Total twin prime pairs: %d\n", pair_count);
return 0;
}
int check_prime(int value) {
if(value <= 1) return 0;
for(int divisor = 2; divisor <= sqrt(value); divisor++) {
if(value % divisor == 0) return 0;
}
return 1;
}
Task 5: Solving the Towers of Hanoi Puzzle
This program simulates the Towers of Hanoi puzzle, calculating and displaying the sequence and total number of disk moves.
#include <stdio.h>
int solve_hanoi(unsigned int disks, char src, char aux, char dest);
void log_move(unsigned int disk_id, char from, char to);
int main() {
unsigned int disk_count;
while(scanf("%u", &disk_count) != EOF) {
int moves = solve_hanoi(disk_count, 'A', 'B', 'C');
printf("Total moves: %d\n", moves);
}
return 0;
}
int solve_hanoi(unsigned int disks, char src, char aux, char dest) {
int move_count = 0;
if(disks == 1) {
log_move(disks, src, dest);
move_count++;
} else {
move_count += solve_hanoi(disks - 1, src, dest, aux);
log_move(disks, src, dest);
move_count++;
move_count += solve_hanoi(disks - 1, aux, src, dest);
}
return move_count;
}
void log_move(unsigned int disk_id, char from, char to) {
printf("%u: %c -> %c\n", disk_id, from, to);
}
Task 6: Calculating Binomial Coefficients
This task involves computing the binomial coefficient C(n, m) using both recursive and iterative methods.
// Recursive calculation
int binomial_recursive(int n_val, int m_val) {
if(m_val == 0 || m_val == n_val) return 1;
return binomial_recursive(n_val - 1, m_val) + binomial_recursive(n_val - 1, m_val - 1);
}
// Iterative calculation
int binomial_iterative(int n_val, int m_val) {
if(m_val > n_val) return 0;
if(m_val == 0 || m_val == n_val) return 1;
int coeff = 1;
for(int i = 1; i <= m_val; i++) {
coeff = coeff * (n_val - i + 1) / i;
}
return coeff;
}
Task 7: Computing the Greatest Common Divisor of Three Numbers
This program finds the GCD of three integers by successively applying the GCD function.
#include <stdio.h>
int calculate_gcd(int val_a, int val_b);
int main() {
int a, b, c, result;
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
result = calculate_gcd(calculate_gcd(a, b), c);
printf("GCD: %d\n", result);
}
return 0;
}
int calculate_gcd(int val_a, int val_b) {
while(val_b != 0) {
int remainder = val_a % val_b;
val_a = val_b;
val_b = remainder;
}
return val_a;
}