13. Examples on arrays and pointers
13. Examples on arrays and pointers
return 0;
}
Output
10 8 6
In this program, the user is asked to enter the number of rows r and columns c. Then, the user is
asked to enter the elements of the two matrices (of order r*c).
We then added corresponding elements of two matrices and saved it in another matrix (two-
dimensional array). Finally, the result is printed on the screen.
To multiply two matrices, the number of columns of the first matrix should be equal to the
number of rows of the second matrix.
The program below asks for the number of rows and columns of two matrices until the above
condition is satisfied.
Then, the multiplication of two matrices is performed, and the result is displayed on the screen.
int main() {
int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until columns of the first matrix is equal to the rows of
the second matrix
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
return 0;
}
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int
c2) {
printf("\nEnter elements of matrix 1:\n");
printf("\nOutput Matrix:\n");
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
printf("%d ", mult[i][j]);
if (j == c2 - 1)
printf("\n");
}
}
}
Output
Output Matrix:
24 29
6 25
The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns.
In this program, the user is asked to enter the number of rows r and columns c. Their values
should be less than 10 in this program.
Then, the user is asked to enter the elements of the matrix (of order r*c).
The program below then computes the transpose of the matrix and prints it on the screen.
Output
Entered matrix:
1 4 0
-5 2 7
return 0;
}
Output
In the program, the user is asked to enter the number of elements, which is stored in variable
num. We will allocate memory for num number of float values.
Then, the user is asked to enter num numbers. These numbers are stored in the dynamically
allocated memory.
Finally, the largest number among these numbers is determined and printed on the screen.