JPtheOne
JPtheOne

Reputation: 11

Function ain't printing the correct matrix after adding

I'm trying to do a code in C with simple functions that can add two given matrixes (bidimensional arrays),, in order to make a matrix calculator. The thing is, for some reason the code keeps printing the second matrix instead of the final one. We have three matrixes (1,2,3) and user gives the value of 1 and 2, to store it and add them to print matrix3. When executing, it will only give the values of the second matrix. I hope someone could tell me why or give any suggestions pls.

#include <stdio.h>
int r1,c1,r2,c2,i,j, matrix1[100][100], matrix2[100][100], finalmatrix[100][100]; //r stands for rows, c for columns and i,j are iterarions
        
        void fillMatrix(){ //Function that creates two bidimensional arrays and asks for its parameters
          
          printf("Enter the number of rows of first matrix: \n");
          scanf("%d", &r1);
        
          printf("Enter the number of columns of first matrix: \n");
          scanf("%d", &c1);
        
          printf(" First matrix sizes are:[%d][%d] \n", r1,c1); //This is only the print of rows and columns values of matrix1
          int matrix1[r1][c1];
        
          printf("\nEnter elements of 1st matrix:\n"); //asks for the values of the matrix
            for (i = 0; i < r1; ++i)
              for (j = 0; j < c1; ++j) {
                printf("Enter element a%d%d: \n", i + 1, j + 1);
                scanf("%d", &matrix1[i][j]);
              }
        
          printf("Enter the number of rows of second matrix: \n");
          scanf("%d", &r2);
        
          printf("Enter the number of columns of second matrix: \n");
          scanf("%d", &c2);
        
          printf("Second matrix sizes are:[%d][%d] \n", r2,c2);   //This is only the print of rows and columns values of matrix1
        
          printf("\n Enter elements of 2nd matrix: \n");
              for (i=0; i< r2; i++)
                  for (j=0; j<c2; j++){
                  printf("Enter the element a%d%d: \n", i+1,j+1);
                  scanf("%d", &matrix2[i][j]);
                  }      
        }
    
    
    void addMatrix()
    {
      
      //Initialize matrix3=0
      for(i=0; i<r1; i++)
      {
          for (j=0; j<c2; ++j)
          {
            finalmatrix[i][j]=0;
          }
      }

//Add matrix1 to matrix2 and store in matrix3
  for (i=0; i<r1; i++)
  {
    for (j=0; j<c2; j++)
    {
      finalmatrix[i][j]=matrix1[i][j] + matrix2[i][j];
    }
  }

//print matrix3 
printf("The resulting matrix is: \n");
  for (i=0; i<r1; i++)
  {
    for (j=0;j<c2; j++)
    {
      printf("%d ", finalmatrix[i][j]);
      if (j==c2-1){
      printf("\n \n");}
    }
  }
}

int main(){
  fillMatrix();
  addMatrix();
}

Upvotes: 0

Views: 39

Answers (1)

Unmitigated
Unmitigated

Reputation: 89374

Remove the declaration of matrix1 in the fillMatrix function that hides the global matrix1.

int matrix1[r1][c1]; // remove this line

Upvotes: 2

Related Questions