Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error: Dynamic Multidimensional Array

Posted on 2003-12-10
8
Medium Priority
?
1,215 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
[X]
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
  • 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
Industry Leaders: 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 400 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 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

Technology Partners: 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!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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