asdfasdf

asdfasdfasdf
mail@pastecode.io avatar
unknown
c_cpp
3 years ago
773 B
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
void makematrix(int **matrix,int r,int col){
  
  //read value for each element in matrix
  for(int i =0;i<r;++i){
    for(int j=0;j<col;++j){
      printf("Enter [%d][%d] element",i,j);
      scanf("%d",matrix[i]+j);
    }
  }
}
void printmatrix(int **matrix,int r, int col){
  for (int i =0;i<r;++i){
    putchar('\n');
    for (int k=0;k<col;++k){
      printf("%4d",*(*(matrix+i)+k));
    }
  }
}
int main(){
  int rows,columns;
  printf("Enter number of rows:");
  scanf("%d",&rows);
  printf("Enter number of columns:");
  scanf("%d",&columns);
  int **mat = malloc(sizeof(int*)*rows);
  for (int i =0 ;i<rows;i++){
    mat[i] = malloc(sizeof(int)*columns);
  }
  makematrix(mat,rows,columns);
  printmatrix(mat,rows,columns);
}