In the previous section we discussed the basics of C Arrays. Now let us take a look at some real-life application Problems.
1. Student Marks Analysis (Array + Function)
Write a program using functions to find the highest, lowest and average marks of students.
#include
void analyzeMarks(int m[], int n);
int main() {
int marks[5] = {78, 85, 66, 90, 72};
analyzeMarks(marks, 5);
return 0;
}
void analyzeMarks(int m[], int n) {
int max = m[0], min = m[0], sum = 0;
for(int i = 0; i < n; i++) {
if(m[i] > max) max = m[i];
if(m[i] < min) min = m[i];
sum += m[i];
}
printf("Highest = %d\n", max);
printf("Lowest = %d\n", min);
printf("Average = %.2f\n", sum / (float)n);
}
// Highest = 90
// Lowest = 66
// Average = 78.20
2. Word Count in a Sentence
Write a C function to count the number of words in a sentence.
#include
int countWords(char str[]);
int main() {
char sentence[100];
printf("Enter sentence: ");
fgets(sentence, 100, stdin);
printf("Word Count = %d\n", countWords(sentence));
return 0;
}
int countWords(char str[]) {
int count = 0;
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] == ' ' && str[i+1] != ' ')
count++;
}
return count + 1;
}
// Enter sentence: Welcome to Qubrica
// Word Count = 3
3. Temperature Monitoring System
Write a C Program to use a global variable to store temperature and local variables inside functions to display status.
#include
// GLOBAL VARIABLE: Accessible by every function
float threshold = 30.0;
void checkTemperature(float temp);
int main() {
// LOCAL VARIABLE: Only exists inside main()
float inputTemp;
printf("Enter temperature: ");
scanf("%f", &inputTemp);
checkTemperature(inputTemp);
return 0;
}
void checkTemperature(float temp) {
if(temp > threshold)
printf("High Temperature Alert!\n");
else
printf("Temperature Normal\n");
}
// Enter temperature: 35
// High Temperature Alert!
4. Power of a Number (xⁿ)
Find x raised to the power n using recursion.
#include
int power(int x, int n);
int main() {
int x, n;
printf("Enter base and power: ");
scanf("%d %d", &x, &n);
printf("Result = %d\n", power(x, n));
return 0;
}
int power(int x, int n) {
if(n == 0)
return 1;
return x * power(x, n - 1);
}
// Enter base and power: 3 3
// Result = 27
| Step | Call Level | Function Call | Action / Condition | Return Value (Calculation) |
|---|---|---|---|---|
| 1 | Level 1 | power(3, 3) | 3 * power( 3, 2) | 3 * 9 = 27 |
| 2 | Level 2 | power(3, 2) | 3 * power(3, 1) | 3 * 3 = 9 |
| 3 | Level 3 | power(3, 1) | 3 * power(3, 0) | 3 * 1 = 3 |
| 4 | Level 4 | power(3, 0) | Base Case reached (n = 0) | 1 |
5. Factorial of a Number (Basic Recursion)
Write a recursive function to find the factorial of a given number.
#include
int factorial(int n);
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d\n", factorial(n));
return 0;
}
int factorial(int n) {
if(n == 0)
return 1;
return n * factorial(n - 1);
}
// Enter a number: 4
// Factorial = 24
6. Fibonacci Series (Classic Recursion)
Generate the Fibonacci series using recursion.
console.log( 'Code is Poetry' );#include
int fib(int n);
int main() {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
for(int i = 0; i < n; i++)
printf("%d ", fib(i));
return 0;
}
int fib(int n) {
if(n == 0)
return 0;
if(n == 1)
return 1;
return fib(n - 1) + fib(n - 2);
}
// Enter number of terms: 6
// 0 1 1 2 3 5
| Iteration (i) | Function Call | Recursive Calculation | Result | Sequence |
|---|---|---|---|---|
| 0 | fib(0) | Base case: returns 0 | 0 | 0 |
| 1 | fib(1) | Base case: returns 1 | 1 | 0, 1 |
| 2 | fib(2) | fib(1) + fib(0) -> 1 + 0 | 1 | 0, 1, 1 |
| 3 | fib(3) | fib(2) + fib(1) -> 1 + 1 | 2 | 0, 1, 1, 2 |
| 4 | fib(4) | fib(3) + fib(2) -> 2 + 1 | 3 | 0, 1, 1, 2, 3 |
| 5 | fib(5) | fib(4) + fib(3) -> 3 + 2 | 5 | 0, 1, 1, 2, 3, 5 |
7 Call by Value
#include
void swap(int a, int b);
int main() {
int x = 10, y = 20;
swap(x, y);
printf("x=%d y=%d\n", x, y);
return 0;
}
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// x=10 y=20
8. Parking Fee Collection
Write a C program to calculate the parking fee for a vehicle using a user-defined function.
The parking charges are calculated as follows:
For the first 2 hours, the parking fee is ₹20 (flat rate)
For every additional hour beyond 2 hours, the charge is ₹10 per hour
#include
int parkingFee(int hours);
int main() {
int h;
printf("Enter number of hours parked: ");
scanf("%d", &h);
printf("Fee = Rs.%d\n", parkingFee(h));
return 0;
}
int parkingFee(int hours) {
if(hours <= 2)
return 20;
return 20 + (hours - 2) * 10;
}
// Enter number of hours parked: 6
// Fee = Rs.60