This section lists the programs covered under Arrays and Matrices. Click on a program title to jump directly to its explanation.
Write a C program to find the largest and smallest number among a list of integers.
n.n elements into an array.
#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;
}
Enter number of elements:
5
Enter array elements:
12 45 3 22 9
Largest = 45
Smallest = 3
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.
Write a C program to compute mean, variance and standard deviation of n elements.
n.n.
#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;
}
Enter number of elements:
4
Enter array elements:
2 4 6 8
Mean = 5.00
Variance = 5.00
Standard Deviation = 2.24
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.
Write a C program to add two matrices.
#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;
}
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
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.
Write a C program to multiply two matrices.
#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;
}
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
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.