In the previous section we discussed the basics of C Arrays. Now let us take a look at some real-life application Problems.
1. Linear Search
Write a C Program to take user-entered numbers and perform linear Search Operation
#include
int main() {
int num[100], n, i, key, found = 0;
printf("Enter number count: ");
scanf("%d", &n);
printf("Enter the numbers: ");
for (i = 0; i < n; i++) {
scanf("%d", &num[i]);
}
printf("Enter the number to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++) {
if (num[i] == key) {
found = 1;
printf("Number Found at index: %d\n", i);
break;
}
}
if (!found)
printf("Number Not Found\n");
return 0;
}
// Enter number count: 5
// Enter the numbers: 5 8 6 4 12
// Enter the number to search: 6
// Number Found at index: 2
| Step (i) | Array Element (num[i]) | Comparison (6 == num[i]) | Action | found State |
|---|---|---|---|---|
| 0 | 5 | 6 == 5 (False) | Continue to next index | 0 |
| 1 | 8 | 6 == 8 (False) | Continue to next index | 0 |
| 2 | 6 | 6 == 6 (True) | Print "Number Found at index: 2" | 1 |
| Break | - | - | Exit the loop immediately | 1 |
2. Removing Duplicates
Write a C program to take numbers entered by users and remove any duplicates
#include
int main() {
int a[100], n, i, j, k;
printf("Enter number count: ");
scanf("%d", &n);
printf("Enter numbers with duplicates: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] == a[j]) {
for (k = j; k < n - 1; k++)
a[k] = a[k + 1];
n--;
j--;
}
}
}
printf("Array after removing duplicates:\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
// Enter number count: 5
// Enter numbers with duplicates: 7 5 0 5 8
// Array after removing duplicates:
// 7 5 0 8
| i value | j value | a[i] == a[j]? | Action Taken | Array State | n |
|---|---|---|---|---|---|
| 0 (val: 7) | 1 (val: 5) | 7 == 5 (No) | None | 7, 5, 0, 5, 8 | 5 |
| 0 (val: 7) | 2 (val: 0) | 7 == 0 (No) | None | 7, 5, 0, 5, 8 | 5 |
| 0 (val: 7) | 3 (val: 5) | 7 == 5 (No) | None | 7, 5, 0, 5, 8 | 5 |
| 0 (val: 7) | 4 (val: 8) | 7 == 8 (No) | None | 7, 5, 0, 5, 8 | 5 |
| 1 (val: 5) | 2 (val: 0) | 5 == 0 (No) | None | 7, 5, 0, 5, 8 | 5 |
| 1 (val: 5) | 3 (val: 5) | 5 == 5 (YES) | Start Shift (k) | 7, 5, 0, [5], 8 | 5 |
| Shift Loop | k = 3 | - | a[3] = a[4] (8 moves left) | 7, 5, 0, 8 | 4 |
| 1 (val: 5) | 3 (val: 8) | 5 == 8 (No) | j was decremented, now 3 | 7, 5, 0, 8 | 4 |
| 2 (val: 0) | 3 (val: 8) | 0 == 8 (No) | None | 7, 5, 0, 8 | 4 |
| 3 (val: 8) | - | - | Loop Ends | 7, 5, 0, 8 | 4 |
3. Voter System
Votes of n voters are stored (candidate numbers). Write a C Program to count votes for each candidate.
#include
int main() {
int votes[100], count[5] = {0};
int n, i;
printf("Enter number of voters: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter vote (1-5): ");
scanf("%d", &votes[i]);
count[votes[i] - 1]++;
}
for (i = 0; i < 5; i++) {
printf("Candidate %d: %d votes\n", i + 1, count[i]);
}
return 0;
}
// Enter number of voters: 5
// Enter vote (1-5): 5
// Enter vote (1-5): 1
// Enter vote (1-5): 1
// Enter vote (1-5): 3
// Enter vote (1-5): 4
// Candidate 1: 2 votes
// Candidate 2: 0 votes
// Candidate 3: 1 votes
// Candidate 4: 1 votes
// Candidate 5: 1 votes
| Voter (i) | Input Vote | Array Index (vote - 1) | Calculation | Updated count Array |
|---|---|---|---|---|
| 1 | 5 | 4 | count[4]++ | {0, 0, 0, 0, 1} |
| 2 | 1 | 0 | count[0]++ | {1, 0, 0, 0, 1} |
| 3 | 1 | 0 | count[0]++ | {2, 0, 0, 0, 1} |
| 4 | 3 | 2 | count[2]++ | {2, 0, 1, 0, 1} |
| 5 | 4 | 3 | count[3]++ | {2, 0, 1, 1, 1} |
4. Bubble Sort
Write a C Program to perform bubble sort
#include
int main() {
int a[50], n, i, j, temp;
printf("Enter number count: ");
scanf("%d", &n);
printf("Enter the numbers :");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
// Enter number count: 5
// Enter the numbers :4 2 9 3 1
// Sorted array:
// 1 2 3 4 9
| Pass (i) | j value | Comparison | Result | Action | Array State |
|---|---|---|---|---|---|
| 0 | j = 0 | 4 > 2 | True | Swap | {2, 4, 9, 3, 1} |
| j = 1 | 4 > 9 | False | None | {2, 4, 9, 3, 1} | |
| j = 2 | 9 > 3 | True | Swap | {2, 4, 3, 9, 1} | |
| j = 3 | 9 > 1 | True | Swap | {2, 4, 3, 1, 9} | |
| 1 | j = 0 | 2 > 4 | False | None | {2, 4, 3, 1, 9} |
| j = 1 | 4 > 3 | True | Swap | {2, 3, 4, 1, 9} | |
| j = 2 | 4 > 1 | True | Swap | {2, 3, 1, 4, 9} | |
| 2 | j = 0 | 2 > 3 | False | None | {2, 3, 1, 4, 9} |
| j = 1 | 3 > 1 | True | Swap | {2, 1, 3, 4, 9} | |
| 3 | j = 0 | 2 > 1 | True | Swap | {1, 2, 3, 4, 9} |
5. Matrix Addition
Write a C Program to perform matrix Addition of the user’s choice
#include
int main() {
int a[10][10], b[10][10], sum[10][10];
int r, c, i, j;
printf("Enter number of rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter elements of Matrix A:\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of Matrix B:\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &b[i][j]);
}
}
// Matrix Addition
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Resultant Matrix (A + B):\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
// Enter number of rows and columns: 3 3
// Enter elements of Matrix A:
// 1 5 9 4 5 6 2 5 8
// Enter elements of Matrix B:
// 1 4 5 6 3 0 8 9 7
// Resultant Matrix (A + B):
// 2 9 14
// 10 8 6
// 10 14 15
| Row (i) | Col (j) | a[i][j] | b[i][j] | Calculation (a+b) | sum[i][j] |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 + 1 | 2 |
| 1 | 5 | 4 | 5 + 4 | 9 | |
| 2 | 9 | 5 | 9 + 5 | 14 | |
| 1 | 0 | 4 | 6 | 4 + 6 | 10 |
| 1 | 5 | 3 | 5 + 3 | 8 | |
| 2 | 6 | 0 | 6 + 0 | 6 | |
| 2 | 0 | 2 | 8 | 2 + 8 | 10 |
| 1 | 5 | 9 | 5 + 9 | 14 | |
| 2 | 8 | 7 | 8 + 7 | 15 |
6. Matrix Multiplication
Write a C Program to perform matrix multiplication
#include
int main() {
int a[10][10], b[10][10], mul[10][10];
int r1, c1, r2, c2;
int i, j, k;
printf("Enter rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of Matrix B: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible\n");
return 0;
}
printf("Enter elements of Matrix A:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of Matrix B:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
mul[i][j] = 0;
}
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < c1; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Resultant Matrix (A × B):\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", mul[i][j]);
}
printf("\n");
}
return 0;
}
// Enter rows and columns of Matrix A: 2 2
// Enter rows and columns of Matrix B: 2 2
// Enter elements of Matrix A:
// 5 4 1 2
// Enter elements of Matrix B:
// 5 8 9 6
// Resultant Matrix (A × B):
// 61 64
// 23 20
| Result Cell (i, j) | k | Calculation (a[i][k] * b[k][j]) | Subtotal (mul[i][j]) | Final Cell Value |
|---|---|---|---|---|
| Row 0, Col 0 | 0 | 5 * 5 = 25 | 25 | |
| 1 | 4 * 9 = 36 | 25 + 36 = 61 | 61 | |
| Row 0, Col 1 | 0 | 5 * 8 = 40 | 40 | |
| 1 | 4 * 6 = 24 | 40 + 24 = 64 | 64 | |
| Row 1, Col 0 | 0 | 1 * 5 = 5 | 5 | |
| 1 | 2 * 9 = 18 | 5 + 18 = 23 | 23 | |
| Row 1, Col 1 | 0 | 1 * 8 = 8 | 8 | |
| 1 | 2 * 6 = 12 | 8 + 12 = 20 | 20 |