# how to concatenate a matrix?

 Alex Nuta · August 26, 2014 Hello, I'm new here and I am also relatively new to c. I have to concatenate a matrix and I don't know how to do that, have searched a little on google but it raises more questions than answes.For example if i have a matrix  [3][3]  and I want do add 0 around the matrix, how do I do that?   the final form should be like:0 0 0 0 00 1 1 1 00 1 1 1 00 1 1 1 00 0 0 0 0 #include int main(){int mat [3][3];int i,j;  for (i=0;i< 3; i++){    for(j=0;j< 3; j++){      scanf("%d", &mat[j]);  }    }................... and now i want to add around mat 0's      ty in advance.return 0;}

## Replies

 Bucky Roberts · August 26, 2014 This doesn't use a matrix, but it does show you how you can break it up to accomplish what you want to to. It will work for any size matrix.``#include int main(){    int a, i, j;    //the column and row length of the original matrix    int col = 5;    int row = 9;    // print the first row of o's    for(a=-1; a<=col; a++){        printf("o ");    }    printf("\n");    //create the matrix with o's on left and right    for(i=0; i#define SIZE 3int main(void){    int m1[SIZE][SIZE]; //declare matrix and initialise all elements to 1's    for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) {    m1[j] = 1; }    }    int m2[SIZE + 2][SIZE + 2] = {0}; //init new larger matrix to all 0's    for (int i = 0; i < SIZE; i++) { //set the middle of the new matrix to the old matrix for (int j = 0; j < SIZE; j++) {    m2[i+1][j+1] = m1[j]; }    }    for (int i = 0; i < SIZE + 2; i++) { //Print new matrix for (int j = 0; j < SIZE + 2; j++) {    printf("%d ", m2[j]); } putchar('\n'); //put a new line    }      return 0;}``You can change the size to create a larger matrix or a smaller matrix. It should be able to grow dynamically. Alex Nuta · August 26, 2014 EDIT: Thanks to Mathias Frits Rørvik I was able to figure it out, just make another matrix 2x2 bigger and add the 0's I will not delete what I posted below(it was before I read the above post but I was unable to edit it due to the fact that a mod had to approve it before it was posted), maby someone wants to solve it too or something I now see I have done a big mistake not telling you all the details, basicly i'm working on this problem and I have gotten to that point that I have to add an outer layer to the matrix, but I do need to work on a matrix, anyways ill just post the problem so I wont fail with the description againSo I read the matrix and in order to solve it they way I am thinking I need to add 0-s around the matrix and do something like://I got the mat and r-rows c-columnsint counter=0;int maxtime=0;int r=5;int c=6;for (i=1; imaxtime){maxtime=counter;}}}}printf("%d",maxtime);
• 1

## C

118,312 followers