Problem Statement
A matrix is a 2D array of numbers arranged in rows and columns. We give you a Matrix of N
rows and M
columns.
Now your task is to do this operation on this matrix:
- If the value matches with the current row and column number then add
3
with the value. - If the value matches with only the current row number then add
2
with the value. - If the value matches with only the current column number then add
1
with the value.
Input Format
- The first line contains
N
is the number of rows in this matrix andM
is the number of columns in this matrix - The second line contains a 2D array
Arr[i][j]
.
Constraints
1 <= N, M <= 10
0 <= Arr[i][j] <= 100
Output Format
Print the matrix after the operation is done.
Sample Input 0
3 3
1 1 1
1 1 1
1 1 1
Sample Output 0
4 3 3
2 1 1
2 1 1
#include <stdio.h>
int main(){
//here taking the row and column from the users
int row;
int column, temp;
printf("enter row and column\n");
scanf("%d%d", &row, &column);
int arr[row][column];
//here taking the matrix input through the users
for(int i = 0; i < row; i ){
for(int j = 0; j < column; j ){
scanf("%d", &arr[i][j]);
}
for (int i=0; i<row; i ){
for(int j=0; j<column; j ){
if (arr[i][j] == arr[i 1] && arr[i][j]== arr[j 1]){
temp= arr[i][j] 3;
printf("%d", temp);
}
else if (arr[i][j] == arr[i 1]){
temp= arr[i][j] 2;
printf("%d", temp);
}
else if (arr[i][j]== arr[j 1]){
temp= arr[i][j] 1;
printf("%d", temp);
}
else{
temp= arr[i][j];
printf("%d", temp);
}
}
}
}
return 0;
}
After I run the code, this doesn't work.
CodePudding user response:
At least this issue:
if (arr[i][j] == arr[i 1] && arr[i][j]== arr[j 1]){
is undefined behavior (UB) as code attempts to access outside int arr[row][column];
with arr[i 1]
and arr[j 1]
.
arr[i][j] == arr[i 1]
is not sensible as it attempts to compare an int
with a pointer.
CodePudding user response:
In c, there are two types of memories. One of them is run-time memory, where your program dynamically allocates memory based on actions done in the program execution time.
In your answer, you are setting your matrix size based on the numbers inputed by the user, which is dynamic memory.
A static array(what you are using) is not dynamic and it is the reason that you can't add more elements and change the size of it.