C Array Problems: Practical Examples with code and Explination

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 <stdio.h>
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])Actionfound State
056 == 5 (False)Continue to next index0
186 == 8 (False)Continue to next index0
266 == 6 (True)Print "Number Found at index: 2"1
Break--Exit the loop immediately1

2. Removing Duplicates

Write a C program to take numbers entered by users and remove any duplicates

				
					#include <stdio.h>
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 valuej valuea[i] == a[j]?Action TakenArray Staten
0 (val: 7)1 (val: 5)7 == 5 (No)None7, 5, 0, 5, 85
0 (val: 7)2 (val: 0)7 == 0 (No)None7, 5, 0, 5, 85
0 (val: 7)3 (val: 5)7 == 5 (No)None7, 5, 0, 5, 85
0 (val: 7)4 (val: 8)7 == 8 (No)None7, 5, 0, 5, 85
1 (val: 5)2 (val: 0)5 == 0 (No)None7, 5, 0, 5, 85
1 (val: 5)3 (val: 5)5 == 5 (YES)Start Shift (k)7, 5, 0, [5], 85
Shift Loopk = 3-a[3] = a[4] (8 moves left)7, 5, 0, 84
1 (val: 5)3 (val: 8)5 == 8 (No)j was decremented, now 37, 5, 0, 84
2 (val: 0)3 (val: 8)0 == 8 (No)None7, 5, 0, 84
3 (val: 8)--Loop Ends7, 5, 0, 84

3. Voter System

Votes of n voters are stored (candidate numbers). Write a C Program to count votes for each candidate.

				
					#include <stdio.h>
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 VoteArray Index (vote - 1)CalculationUpdated count Array
154count[4]++{0, 0, 0, 0, 1}
210count[0]++{1, 0, 0, 0, 1}
310count[0]++{2, 0, 0, 0, 1}
432count[2]++{2, 0, 1, 0, 1}
543count[3]++{2, 0, 1, 1, 1}

4. Bubble Sort

Write a C Program to perform bubble sort

				
					#include <stdio.h>
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 valueComparisonResultActionArray State
0j = 04 > 2TrueSwap{2, 4, 9, 3, 1}
j = 14 > 9FalseNone{2, 4, 9, 3, 1}
j = 29 > 3TrueSwap{2, 4, 3, 9, 1}
j = 39 > 1TrueSwap{2, 4, 3, 1, 9}
1j = 02 > 4FalseNone{2, 4, 3, 1, 9}
j = 14 > 3TrueSwap{2, 3, 4, 1, 9}
j = 24 > 1TrueSwap{2, 3, 1, 4, 9}
2j = 02 > 3FalseNone{2, 3, 1, 4, 9}
j = 13 > 1TrueSwap{2, 1, 3, 4, 9}
3j = 02 > 1TrueSwap{1, 2, 3, 4, 9}

5. Matrix Addition

Write a C Program to perform matrix Addition of the user’s choice

				
					#include <stdio.h>

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]
00111 + 12
1545 + 49
2959 + 514
10464 + 610
1535 + 38
2606 + 06
20282 + 810
1595 + 914
2878 + 715

6. Matrix Multiplication

Write a C Program to perform matrix multiplication

				
					#include <stdio.h>

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)kCalculation (a[i][k] * b[k][j])Subtotal (mul[i][j])Final Cell Value
Row 0, Col 005 * 5 = 2525
14 * 9 = 3625 + 36 = 6161
Row 0, Col 105 * 8 = 4040
14 * 6 = 2440 + 24 = 6464
Row 1, Col 001 * 5 = 55
12 * 9 = 185 + 18 = 2323
Row 1, Col 101 * 8 = 88
12 * 6 = 128 + 12 = 2020