Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Error: Dynamic Multidimensional Array

Posted on 2003-12-10
8
1,208 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
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.

808 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