C Programs

C Programming – Arrays


Programs at a Glance

This section lists the programs covered under Arrays and Matrices. Click on a program title to jump directly to its explanation.

  1. Largest and Smallest Element in an Array
  2. Mean, Variance and Standard Deviation
  3. Addition of Two Matrices
  4. Multiplication of Two Matrices

Largest and Smallest Element in an Array


Problem Statement

Write a C program to find the largest and smallest number among a list of integers.

Algorithm
  1. Start the program.
  2. Read the number of elements n.
  3. Read n elements into an array.
  4. Initialize both maximum and minimum with the first element.
  5. Compare remaining elements to update maximum and minimum.
  6. Display the largest and smallest values.
  7. Stop the program.
C Program

#include <stdio.h>

int main() {
    int n, i;
    int a[100], max, min;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    max = min = a[0];

    for (i = 1; i < n; i++) {
        if (a[i] > max)
            max = a[i];
        if (a[i] < min)
            min = a[i];
    }

    printf("Largest = %d\n", max);
    printf("Smallest = %d\n", min);
    return 0;
}
    
Sample Output

Enter number of elements:
5
Enter array elements:
12 45 3 22 9
Largest = 45
Smallest = 3
    
Explanation

The first array element is used to initialize both max and min, and then each subsequent element is compared with these values. Whenever a larger or smaller value is found, max or min is updated, so a single scan of the array is sufficient.

Mean, Variance and Standard Deviation


Problem Statement

Write a C program to compute mean, variance and standard deviation of n elements.

Algorithm
  1. Start the program.
  2. Read number of elements n.
  3. Read elements and calculate their sum.
  4. Compute mean as sum divided by n.
  5. Calculate variance using squared deviations from mean.
  6. Find standard deviation as square root of variance.
  7. Display results.
  8. Stop the program.
C Program

#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    float a[100], mean = 0, variance = 0, sd;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++) {
        scanf("%f", &a[i]);
        mean += a[i];
    }

    mean = mean / n;

    for (i = 0; i < n; i++) {
        variance += pow(a[i] - mean, 2);
    }

    variance = variance / n;
    sd = sqrt(variance);

    printf("Mean = %.2f\n", mean);
    printf("Variance = %.2f\n", variance);
    printf("Standard Deviation = %.2f\n", sd);
    return 0;
}
    
Sample Output

Enter number of elements:
4
Enter array elements:
2 4 6 8
Mean = 5.00
Variance = 5.00
Standard Deviation = 2.24
    
Explanation

The mean is calculated as the average of all values, variance is the average of squared deviations from this mean, and standard deviation is the square root of the variance. The functions pow() and sqrt() from math.h are used for squaring and square root operations respectively.

Addition of Two Matrices


Problem Statement

Write a C program to add two matrices.

Algorithm
  1. Start the program.
  2. Read rows and columns.
  3. Read elements of both matrices.
  4. Add corresponding elements.
  5. Display resultant matrix.
  6. Stop the program.
C Program

#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:\n");
    scanf("%d %d", &r, &c);

    printf("Enter elements of first matrix:\n");
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\n");
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            sum[i][j] = a[i][j] + b[i][j];

    printf("Sum of matrices:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
            printf("%d ", sum[i][j]);
        printf("\n");
    }

    return 0;
}
    
Sample Output

Enter number of rows and columns:
2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8
Sum of matrices:
6 8
10 12
    
Explanation

Matrix addition adds elements that are in the same row and column position in both matrices. Nested loops are used to visit each position (i, j) and store the sum a[i][j] + b[i][j] in the result matrix.

Multiplication of Two Matrices


Problem Statement

Write a C program to multiply two matrices.

Algorithm
  1. Start the program.
  2. Read matrix dimensions.
  3. Check multiplication condition.
  4. Multiply matrices using nested loops.
  5. Display product matrix.
  6. Stop the program.
C Program

#include <stdio.h>

int main() {
    int a[10][10], b[10][10], mul[10][10];
    int r1, c1, r2, c2, i, j, k;

    printf("Enter rows and columns of first matrix:\n");
    scanf("%d %d", &r1, &c1);

    printf("Enter rows and columns of second matrix:\n");
    scanf("%d %d", &r2, &c2);

    if (c1 != r2) {
        printf("Matrix multiplication not possible\n");
        return 0;
    }

    printf("Enter elements of first matrix:\n");
    for (i = 0; i < r1; i++)
        for (j = 0; j < c1; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\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 (k = 0; k < c1; k++) {
                mul[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    printf("Product of matrices:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++)
            printf("%d ", mul[i][j]);
        printf("\n");
    }

    return 0;
}
    
Sample Output

Enter rows and columns of first matrix:
2 2
Enter rows and columns of second matrix:
2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8
Product of matrices:
19 22
43 50
    
Explanation

Each element of the product matrix is obtained by multiplying the elements of a row from the first matrix with the corresponding elements of a column from the second matrix and adding these products. Three nested loops are used to traverse rows, columns and the inner index needed for the dot-product calculation.