Viva
Viva

Reputation: 2075

concatenate 2 matrices

I have a matrix dynamically allocated and I want to create another one which is the first matrix but with another copy beside. for example,I have the matrix:

11
22

My new matrix will be:

1 1 1 1
2 2 2 2

How can I concatenate them? This is my code in C:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int **create_matrix(int row, int col)
{
    int **matrix = malloc(sizeof (int*)*row);
    int i;
    for (i = 0; i < row; i++)
    {
        matrix[i] = malloc(sizeof (int)*col);
    }
    return matrix;
}

void matrix_input(int **matrix, int row, int col)
{
    int i, j;
    printf("enter the elements of the matrix:\n");
    for (i = 0; i < row; i++)
    {

        for (j = 0; j < col; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }
}

int **extend_matrix(int **matrix, int row, int col)
{
    int k, j;
    int i;
    int **extend_matrix = malloc(sizeof (int*)*row);
    for (k = 0; k < row + row; k++)
    {
        extend_matrix[k] = malloc(sizeof (int)*col);
    }
    extend_matrix = matrix;
    extend_matrix = (int**) realloc(extend_matrix, (row + row) * sizeof (int*));
    extend_matrix[j] = matrix[j];
    for (i = 0; i < row; i++)
    {
        extend_matrix[k] = matrix[i];
    }
}

void print_matrix(int **matrix, int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf(" %d ", matrix[i][j]);
        }
        printf("\n");
    }

}

void print_extend_matrix(int **extend_matrix, int row, int col)
{
    int k, j;
    for (k = 0; k < row + row; k++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d", extend_matrix[k][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    int **matrix;
    int **extend_matrix;
    int row, col;
    printf("enter the number of rows of cols:");
    scanf("%i%i", &row, &col);
    matrix = create_matrix(row, col);
    matrix_input(matrix, row, col);
    print_matrix(matrix, row, col);
    print_extend_matrix(extend_matrix, row, col);
    getch();
    return 0;
} 

Upvotes: 1

Views: 295

Answers (3)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58291

Although @NPE suggested to you a better way. If you want to allocate memory in extend_matrix()

error in your code (read comments)

int **extend_matrix = malloc(sizeof (int*)*row);
                                           ^ on row
for (k = 0; k < row + row; k++)            
                    ^ where as loop is for row + row
{
    extend_matrix[k] = malloc(sizeof (int)*col);  // So this cause an error,
                                                  // segment-fault
}

second, your concept is wrong to copy memory:

 extend_matrix = matrix;  

at this line you are assigning matrix to extend_matrix its wrong. you need loop here to copy each elements from matrix[][] to extend_matrix[][]. (but rectify your memory allocation code first)

Upvotes: 2

Ajeet Ganga
Ajeet Ganga

Reputation: 8653

If this is what you are looking for :

int concat(void * oldM, int row, int col,void& *newM) {
     newM = malloc(sizeof(int)*row*col*2);

     for(int i = 0;i<2;++i)
      for(int j=0;j<2;++j)
         newM[i][j+col] = newM[i][j] = oldM[i][j]; 

    for(int i = 0;i<2;++i)  {
      for(int j=0;j<4;++j) {
        cout<<"\t"<<newM[i][j];
      }
      cout<<"\n";
   }
}

Upvotes: 1

NPE
NPE

Reputation: 500793

I think extend_matrix() should just call create_matrix() to create a new matrix of double the width, and then use two simple nested loops to populate it.

Upvotes: 2

Related Questions