Solved

Error: Dynamic Multidimensional Array

Posted on 2003-12-10
8
1,206 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now