creating a dynamic 2d array?

This is a discussion on creating a dynamic 2d array? within the C Programming forums, part of the General Programming Boards category; Hi I new here, I already had a look into the search but, I didn't find that what I am ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    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. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    Thanks,

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

    regards,

    Marco

  4. #4
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    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.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    Thumbs up

    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.*/

    /* Header file inclusions*/
    #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);
    }

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by anand
    hope this helps u.
    Code:
    /* File name: Matmul.c */
    
    /* Aim: To do matrix multiplication using Dynamic Memory Allocation.*/
    
    /* Header file inclusions*/
    #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?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I get this when I click your link Salem:

    Sorry - no matches. Please try some different terms.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    "dynamic 2d array"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote 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.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 03:25 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 01:01 AM
  5. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21