- 2068 Chaitra · 2+6 marks
Significance of arrays; multiply 3×3 matrices
Short answer
Arrays store many values of one type, accessible by index, stored consecutively, efficient for loops and matrices. The multiply program reads two 3x3 matrices, passes them to multiply() which computes c[i][j] = sum of a[i][k]*b[k][j] for k=0..2, then displays product in main().
Write down the significance of array in C. Write a program to multiply two 3×3 matrix. Two matrix are input from main ( ) function and pass to a user defined function with argument with array. The result is also displayed from main ( ) function.
Significance of arrays
- An array stores many values of the same type under one name, so a hundred variables are not needed.
- Elements are reached quickly by index (
a[i]), which works well with loops. - Data are stored in consecutive memory, so traversal and processing are simple and fast.
- Needed for lists, tables and matrices, and as the base for strings, sorting, searching and many data structures.
- Arrays are passed to functions easily, so one function can work on a whole set of data.
Program: multiply two 3×3 matrices
c[i][j] = Σ a[i][k] × b[k][j] for k = 0 to 2. The matrices are read in main(), passed to multiply() and the product is displayed in main().
#include <stdio.h>
void multiply(int a[3][3], int b[3][3], int c[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
c[i][j] = 0;
for (int k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
int main(void)
{
int a[3][3], b[3][3], c[3][3];
printf("Enter first matrix: ");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix: ");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
multiply(a, b, c);
printf("Product:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
printf("%5d", c[i][j]);
printf("\n");
}
return 0;
}
Sample run (second matrix is diag(1,1,2))
Input: 1 2 3 4 5 6 7 8 9 1 0 0 0 1 0 0 0 2
Enter first matrix: Enter second matrix: Product:
1 2 6
4 5 12
7 8 18
The third column is doubled, which matches the multiplier matrix.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Pass array; transpose 3×3 matrix · Computer Programming, chapter 7
- Significance of user defined functions · Computer Programming, chapter 6
- Array overflow/underflow; add two arrays · Computer Programming, chapter 7
- Switch versus user defined function · Computer Programming, chapter 5
- Significance of main() function · Computer Programming, chapter 6
- Pass student array; display passed students · Computer Programming, chapter 7
- Cascaded two-ports: ABCD matrices multiply · Electrical Circuit Theory, chapter 7
- Protected specifier; student/test/result program · Object Oriented Programming, chapter 6
- Default argument and overloading program · Object Oriented Programming, chapter 3
- Exception with argument and specification program · Object Oriented Programming, chapter 10