Solved

Error: Dynamic Multidimensional Array

Posted on 2003-12-10
8
1,205 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now