The program in C to Input a 3 * 3 matrix using keyboard, and to convert it in to 4*4 matrix by adding corresponding row and columns is given below:
#include <stdio.h>
#define ROWS 3
#define COLS 3
#define NEW_ROWS 4
#define NEW_COLS 4
int main() {
int arr[ROWS][COLS], new_arr[NEW_ROWS][NEW_COLS], i, j;
printf("Enter the elements of the 3x3 matrix:\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
scanf("%d", &arr[i][j]);
}
}
// Copy the elements of the 3x3 matrix to the 4x4 matrix
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
new_arr[i][j] = arr[i][j];
}
}
// Add the corresponding rows and columns
for (i = 0; i < ROWS; i++) {
new_arr[i][COLS] = 0;
new_arr[ROWS][i] = 0;
for (j = 0; j < COLS; j++) {
new_arr[i][COLS] += arr[i][j];
new_arr[ROWS][i] += arr[i][j];
new_arr[ROWS][COLS] += arr[i][j];
}
}
printf("The 4x4 matrix is:\n");
for (i = 0; i < NEW_ROWS; i++) {
for (j = 0; j < NEW_COLS; j++) {
printf("%d ", new_arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the elements of the 3×3 matrix:
45
67
33
12
89
99
54
33
21
The 4×4 matrix is:
45 67 33 145
12 89 99 200
54 33 21 108
145 200 108 22299
Pro-Tips💡
This program defines two integer constants ROWS and COLS to set the size of the 3×3 array and another two constants NEW_ROWS, NEW_COLS to set the size of the 4×4 array.
Then, it reads in the elements of the 3×3 array using nested for loops.
Next, it uses another set of nested for loops to copy the elements of the 3×3 array to the 4×4 array.
Then it uses another set of nested for loops to add the corresponding rows and columns of the 3×3 array and update the 4×4 array.
Finally, it prints out the 4×4 array.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.