[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: c++: 2D array



You could try something like

int **i;
int ctr;
i=new int*[5];
for(ctr=0;ctr<4;ctr++)
     {
          i[ctr]=new int[4];
     }

Then you can refer to members as i[3][2].

Complete program which I made is


#include <iostream.h>

void main()
 {
    int **i;
    int ctr,count1;
    i=new int*[5];
    for(ctr=0;ctr<4;ctr++)
     {
 i[ctr]=new int[4];
     }
    for(ctr=0;ctr<4;ctr++)
     {
 for(count1=0;count1<3;count1++)
    cin>>i[ctr][count1];
     }
    for(ctr=0;ctr<4;ctr++)
     {
 for(count1=0;count1<3;count1++)
    cout<<i[ctr][count1]<<"\t";
     }
 }


It works.



----- Original Message ----- 
From: "Dwivedi Ajay kumar" <ajayd@xxxxxxxxxx>
To: <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, September 20, 2000 10:37 PM
Subject: [LIP] c++: 2D array


> hi all,
> how can we declare a 2 dimensional array in c++ on heap. I am
> trying to make a Matrix class and need to allocate the array using new in
> the constructor. 
> Just wanted to ask if I could do something like:
> 
> int ** a;
> a = new [4][5];
> 
> Or some other way to accomplish the above so that I can call a[i][j]
> later.
> 
> -- 
> 
> #!!! If anything can go wrong, _FIX_ it. (To hell with MURPHY)
> 
> Ajay kumar Dwivedi
> ajayd@xxxxxxxxxx
> 
> 
> ---------------------------------------------
> Find out more about this and other Linux India
> mailing lists at http://lists.linux-india.org/


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com