Thread: creating a dynamic 2d array?

1. creating a dynamic 2d array?

Hi I new here,

I already had a look into the search but, I didn't find that
what I am looking for!

How do I create a 2d array, where size can be changed during
runtime. I don't know the dimensions of the array.

And after creating this, how do I find out the size of this array.
How can I find out the dimension of this array!

Thanks

Marco

2. Thanks,

and if I don't know the dimension of the array?

regards,

Marco

3. If you're not sure what the dimensions are, perhaps it would be better to create a dynamic data structure that can grow and shrink. Perhaps a linked list.

4. hi,
i'm anand & joining for the first time to any such forum sites.
the program below
1. takes user input about the dimensions af the matrices
2. Checks whenther they are multiplicable
3. Dynamicall allocates memory to the matrices
4. Multiplies them and displays the resultant matrix with the original matices.

hope this helps u.

/* File name: Matmul.c */

/* Aim: To do matrix multiplication using Dynamic Memory Allocation.*/

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

/* Main Function */
int main()
{

/* Declaring pointer fo matrix multiplication.*/
int **ptr1, **ptr2, **ptr3;

/* Declaring integer variables for row and columns of two matrices.*/
int row1, col1, row2, col2;

/* Declaring indexes. */
int i, j, k;

/* Request the user to input number of columns of the matrices.*/
printf("\nEnter number of rows for first matrix : ");
scanf("%d", &row1);
printf("\nEnter number of columns for first matrix : ");
scanf("%d", &col1);

printf("\nEnter number of rows for second matrix : ");
scanf("%d", &row2);
printf("\nEnter number of columns for second matrix : ");
scanf("%d", &col2);

if(col1 != row2)
{
printf("\nCannot multiply two matrices.");
return(0);
}

/* Allocating memory for three matrix rows. */
ptr1 = (int **) malloc(sizeof(int *) * row1);
ptr2 = (int **) malloc(sizeof(int *) * row2);
ptr3 = (int **) malloc(sizeof(int *) * row1);

/* Allocating memeory for the cloumns of three matrices. */
for(i=0; i<row1; i++)
{
ptr1[i] = (int *)malloc(sizeof(int) * col1);
}
for(i=0; i<row2; i++)
{
ptr2[i] = (int *)malloc(sizeof(int) * col2);
}

for(i=0; i<row1; i++)
{
ptr3[i] = (int *)malloc(sizeof(int) * col2);
}

/* Request the user to input members of first matrix. */
printf("\nEnter elements of first matrix :\n");
for(i=0; i< row1; i++)
{
for(j=0; j< col1; j++)
{
printf("\tA[%d][%d] = ",i, j);
scanf("%d", &ptr1[i][j]);
}
}

/* request to user to input mebmbers of first matrix. */
printf("\nEnter elements of second matrix :\n");
for(i=0; i< row2; i++)
{
for(j=0; j< col2; j++)
{
printf("\tB[%d][%d] = ",i, j);
scanf("%d", &ptr2[i][j]);
}
}

/* Calculation begins for the resultant matrix. */
for(k=0; k < row1; k++)
{
for(i=0; i < col1; i++)
{
ptr3[k][i] = 0;
for(j=0; j<col2; j++)
{
ptr3[k][i] = ptr3[k][i] + ptr1[k][j] * ptr2[j][i];
}
}
}

/* Printing the contents of first matrix. */
printf("\n\nFirst matrix :");
for(i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(j=0; j< col1; j++)
{
printf("%4d", ptr1[i][j]);
}
}
/* Printing the contents of second matrix. */
printf("\n\nSecond matrix :");
for(i=0; i< row2; i++)
{
printf("\n\t\t\t");
for(j=0; j < col2; j++)
{
printf("%4d", ptr2[i][j]);
}
}

/* Printing the contents of third matrix. */

printf("\n\nResultant matrix :");
for(i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(j=0; j < col2; j++)
{
printf("%4d", ptr3[i][j]);
}
}
return(0);
}

5. Originally Posted by anand
hope this helps u.
Code:
```/* File name: Matmul.c */

/* Aim: To do matrix multiplication using Dynamic Memory Allocation.*/

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

/* Main Function 	*/
int main()
{

/* Declaring pointer fo matrix multiplication.*/
int **ptr1, **ptr2, **ptr3;

/* Declaring integer variables for row and columns of two matrices.*/
int row1, col1, row2, col2;

/* Declaring indexes. */
int i, j, k;

/* Request the user to input number of columns of the matrices.*/
printf("\nEnter number of rows for first matrix :  ");
scanf("%d", &row1);
printf("\nEnter number of columns for first matrix :  ");
scanf("%d", &col1);

printf("\nEnter number of rows for second matrix :  ");
scanf("%d", &row2);
printf("\nEnter number of columns for second matrix :  ");
scanf("%d", &col2);

if(col1 != row2)
{
printf("\nCannot multiply two matrices.");
return(0);
}

/* Allocating memory for three matrix rows. */
ptr1 = (int **) malloc(sizeof(int *) * row1);
ptr2 = (int **) malloc(sizeof(int *) * row2);
ptr3 = (int **) malloc(sizeof(int *) * row1);

/* Allocating memeory for the cloumns of three matrices. */
for(i=0; i<row1; i++)
{
ptr1[i] = (int *)malloc(sizeof(int) * col1);
}
for(i=0; i<row2; i++)
{
ptr2[i] = (int *)malloc(sizeof(int) * col2);
}

for(i=0; i<row1; i++)
{
ptr3[i] = (int *)malloc(sizeof(int) * col2);
}

/* Request the user to input members of first matrix. */
printf("\nEnter elements of first matrix :\n");
for(i=0; i< row1; i++)
{
for(j=0; j< col1; j++)
{
printf("\tA[%d][%d] = ",i, j);
scanf("%d", &ptr1[i][j]);
}
}

/* request to user to input mebmbers of first matrix. */
printf("\nEnter elements of second matrix :\n");
for(i=0; i< row2; i++)
{
for(j=0; j< col2; j++)
{
printf("\tB[%d][%d] = ",i, j);
scanf("%d", &ptr2[i][j]);
}
}

/* Calculation begins for the resultant matrix. */
for(k=0; k < row1; k++)
{
for(i=0; i < col1; i++)
{
ptr3[k][i] = 0;
for(j=0; j<col2; j++)
{
ptr3[k][i] = ptr3[k][i] + ptr1[k][j] * ptr2[j][i];
}
}
}

/* Printing the contents of first matrix. */
printf("\n\nFirst matrix :");
for(i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(j=0; j< col1; j++)
{
printf("%4d", ptr1[i][j]);
}
}
/* Printing the contents of second matrix. */
printf("\n\nSecond matrix :");
for(i=0; i< row2; i++)
{
printf("\n\t\t\t");
for(j=0; j < col2; j++)
{
printf("%4d", ptr2[i][j]);
}
}

/* Printing the contents of third matrix. */

printf("\n\nResultant matrix :");
for(i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(j=0; j < col2; j++)
{
printf("%4d", ptr3[i][j]);
}
}
return(0);
}```
Use code tags like I've just did. Second, what a kind of operations do you want to perform in this 2-D array?

Sorry - no matches. Please try some different terms.

7. "dynamic 2d array"

8. Well couldn't you just allocate a single array and then use pointer aritmatic since you know the offsets.

create pointer to Matrix1 and initilaze it with the address to the array
create a second pointer to Matrix2 and initialize it by adding the total size of the first array to the first pointer then just incrememt both pointers

9. Originally Posted by Nessarose

Just remember there is no standard <malloc.h> header. This seems to be a throwback from some old unix systems. your dynamic memory functions are in <stdlib.h> most of them anyway.

As to the dead link: just do a search for malloc on the board or on Google.