2d array

This is a discussion on 2d array within the C++ Programming forums, part of the General Programming Boards category; Hi How can i define a 2d array without giving the size of it i want the user to enter ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    2d array

    Hi

    How can i define a 2d array without giving the size of it
    i want the user to enter the size
    please can somone tell me how?

    Code:
    #include<stdio.h>
    
    int main(){
    
    int arow,acol,brow,bcol,crow,ccol;
    int i,j,m,k;,
    int a[arow][acol],b[brow][bcol],c[crow][ccol];  // error occur here
    
    printf("Enter the first matrix row and column size\n");
    scanf("%d%d",&arow,&acol);
    
    printf("Enter the second matrix row and column size\n");
    printf("(column size must have the same size of row size of the first matrix)\n");
    scanf("%d%d",&brow,&bcol);
    
    if(arow!=bcol)
    printf("Cannot perform the multiplication\n");
    
    else{
    crow=arow;
    ccol=bcol;
    
    printf("Enter the first matrix elements\n");
    
    for(i=0;i<arow;i++)
      for(j=0;j<acol;j++)
        scanf("%d",&a[i][j]);
    
    for(i=0;i<brow;i++)
      for(j=0;j<bcol;j++)
        scanf("%d",&b[i][j]);
    
    
    for(i=0;i<crow;i++)
      for(j=0;j<ccol;j++)
    	  for(k=0; k<acol;k++){
    		m=a[i][k]*b[k][j];
            c[i][j]+=m;}
    }
    return 0; }
    Last edited by kuwait; 10-23-2003 at 09:25 AM.
    please set free our POWs

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    dynamic memory using new:
    Code:
    int size1, size2, i;
    
    cout << "enter size1;
    cin >> size1;
    cout << "enter size2;
    cin >> size2;
    
    int ** twoDarrayOfInts;
    twoDarrayOfInts = new int[size1];
    for(i = 0; i < size1; ++i)
      twoDarrayOfInts[i] = new int[size2];
    Then you need to know how to access each subarray and each index in the array as well as how to release the memory you have appropriated in the declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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