how to concatenate a matrix?

+1 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 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0 

#include <stdio.h>

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;
}

Post a Reply

Replies

Oldest  Newest  Rating
+5 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 <stdio.h>

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<row; i++){
        for(j=-1; j<=col; j++){
            if(j == -1 || j == col){
                printf("o ");
            }else{
                printf("x ");
            }
        }
        printf("\n");
    }

    // print the last  row of o's
    for(a=-1; a<=col; a++){
        printf("o ");
    }
    printf("\n");

}

Here is the output:

/images/forum/upload/2014-08-26/13325f577e713ed37e9762a998de1a2f.png
+4 Mathias Frits Rørvik · August 26, 2014
#include <stdio.h>

#define SIZE 3

int 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.
+1 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 again/images/forum/upload/2014-08-26/d7a62e47bdaccf4336d675ee9e75f3e1.png

So 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-columns

int counter=0;
int maxtime=0;
int r=5;
int c=6;
for (i=1; i<r-1; i++){
for(j=1;j<c-1;j++){
if(mat[j]!=o){
counter=counter+9;

//a lot of if's to test for other candles around the candle (and in order to test the outer matrix i need first to concatenate around it 0's

if (mat[i-1][j-1]!=0){
counter=counter-1;}



if (mat [.i][j-1]!=0){ 
counter=counter-1;}

if (mat[i+1][j-1]!=0){
counter=counter-1;}

if (mat[i-1][j]!=0){
counter=counter-1;}

if (mat[i+1][j]!=0){
counter=counter-1;}

if (mat[i-1][j+1]!=0){
counter=counter-1;}

if (mat[.i][j+1]!=0){
counter=counter-1;}

if (mat[i+1][j+1]!=0){
counter=counter-1;}

If(counter>maxtime){
maxtime=counter;}

}
}
}
printf("%d",maxtime);
  • 1

C

107,249 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator