Solved

Error: Dynamic Multidimensional Array

Posted on 2003-12-10
8
1,210 Views
Last Modified: 2008-02-01

Hi,

I'm new to C++ and on the web I found a rather long-winded way to define multidimensional arrays.  I tweaked it into my code but I'm getting an error.  What's wrong?

-------------- SECTION OF CODE ---------------------

/*NOTE n = integer passed into function as variable
           sigma = multidimarray passed into function*/
   
double w;
double y;

//sigma (rows, columns)
w = sigma[0][0];
y = sigma[n][0] - w + 1;
       
//Create dataarray[y][1]
double ** dataarray;
dataarray = new double *[y];
dataarray[0]= new double[y*1];
for (i=1;i<y;i++) {
dataarray[i]=dataarray[i-1]+1;
}

//populate rates in column 2 of dataArray
for(i=0; i<=n; i++)
{
x = sigma[i][0];
dataarray[(x-w+1)][1] = sigma[i][1];
}

-------------- ERROR MESSAGE --------------------

D:\test.cpp(482) : error C2108: subscript is not of integral type
D:\test.cpp(482) : error C2440: '=' : cannot convert from 'double' to 'double *'
        There is no context in which this conversion is possible

-------------------------------------------------------

NOTE: Line 182 is the second last line beginning "dataarray[("

Thanks,
Malcolm


0
Comment
Question by:malramsay
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9918251
Hi,

-------------- ERROR MESSAGE --------------------

>>>>>>D:\test.cpp(482) : error C2108: subscript is not of integral type
>>>>>>D:\test.cpp(482) : error C2440: '=' : cannot convert from 'double' to 'double *'
        There is no context in which this conversion is possible



An array index should be integral, U are tryin with a double

HTH

Amit

0
 

Author Comment

by:malramsay
ID: 9918275

Hi again Amit,

In this code several times I need to create dynamic multidimensional arrays where all the values in those arrays are doubles.  Is there a way to do this?

I read up about a "Little Template Library (LTL)" that had MArray in it, but I'm not sure if that's the best method.  Is there some other way?

Cheers,
Malcolm
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9918285
Hi

I meant the array index should be integral

U can definitely have double arrays i.e. Array in which all values are double

HTH

Amit


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
Sys_Prog earned 100 total points
ID: 9918292
This would let create multi dimenison double array

      double **arr ;
      int rows ;
      int cols ;
      cout << "Enter no of rows " ;
      cin >> rows ;
      cout << "Enter no of cols " ;
      cin >> cols ;
      
      arr = new ( double * ) [rows] ;
      
      for ( int i = 0 ; i < rows ; i ++ )
          arr [i] = new double [cols] ;


HTH

Amit
0
 

Author Comment

by:malramsay
ID: 9918340
Thanks for that Amit,

I got this to work (slight variation with n*n array), but I haven't checked the answers,

double ** a;
a=new double *[n];
for (i=0;i<n;i++) {
a[i] = new double [n] ;
}

The main difference is changing (from your code),

arr = new ( double * ) [rows] ;

to

arr = new double * [rows] ;

should that make a difference?
0
 
LVL 30

Expert Comment

by:Axter
ID: 9918344
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9918373
No, that won't make a difference
0
 

Author Comment

by:malramsay
ID: 9918378

Thanks again Amit!

Thanks also Axter.  I'll check out your site - it seems to have lots of interesting stuff on there for c++ newbies.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question