# MultiDimensional Array Problem

Page 1 of 1

## 4 Replies - 1202 Views - Last Post: 20 January 2011 - 12:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=211090&amp;s=9b49e7c8cadd74cb642db239078ded9e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 KingWill

Reputation: 0
• Posts: 29
• Joined: 08-January 11

# MultiDimensional Array Problem

Posted 20 January 2011 - 11:46 AM

Hi there, I'm having problems properly declaring a multidimensional array in the header without a size, then defining the multidimensional array, or *pointer to pointer* in my .cpp class.

Also, i'm having problems on how to set up a method that returns the multidimensional array...

I'm not allowed to use vectors or anything in the STL library.

Any help would be apreciated, the multidimensional array as seen below, has been named, positions

``` #ifndef SHIP_H
#define SHIP_H

using namespace std;

class Ship {
public:
int** getPositions(); // returns the ship's points
private:
int** positions;
};

#endif

```

.cpp Class
```#include <iostream>
#include <string>
#include <Ship.h>
using namespace std;

Ship::Ship () {
shipMarker = '0';
shipSize = 0;
}

Ship::Ship(int vertical, int horizontal, int yPos, int xPos, int orientation, int size, char Marker) {
shipMarker = Marker;
shipSize = size;
verticalSpace = vertical;
horizontalSpace = horizontal;
yStart = yPos + 8;
xStart = xPos + 4;

positions = new int[2][8];

for (int i = yPos; i < verticalSpace; i += 2) {
for (int k = xPos; k < horizontalSpace; k += 2) {
positions[i][k] = shipMarker;
}
}

}

Ship::~Ship() {

if (positions != NULL) {
delete*[] positions;
}
}

int Shipp::getYStart() {
return yStart;
}
int Shipp:: getXStart()
return xStart;
}

int** Ship::getPositions() {
return positions;
}

char Ship::getMarker() {
return shipMarker;
}

int Ship::getVertical() {
return verticalSpace;
}
int Ship::getHorizontal() {
return horizontalSpace;
}

```

Function in different class taking the 2d Array as a parameter:

```int Board::addShip(Ship shipName) {

for(int i = shipName.getYStart(); i < shipName.getVertical(); i += 2) {
for(int k= shipName.getXStart(); k < shipName.getHorizontal(); k += 2) {
board[i][k] = shipName.getPositions()[i][k];
}

}

shipsList[shipsCount] = shipName;
shipsCount++;

return 1;

}
```

This post has been edited by KingWill: 20 January 2011 - 11:47 AM

Is This A Good Question/Topic? 0

## Replies To: MultiDimensional Array Problem

### #2 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: MultiDimensional Array Problem

Posted 20 January 2011 - 11:58 AM

```positions = new int[2][8];
```

As far as I'm aware, you can only use one subscript operator ([]) with the new operator, but I may be wrong.

Instead, you will probably need to throw in an extra dereference operator.

```positions = new int*[size of array];
```

This post has been edited by Aphex19: 20 January 2011 - 11:59 AM

### #3 jimblumberg

Reputation: 4733
• Posts: 14,756
• Joined: 25-December 09

## Re: MultiDimensional Array Problem

Posted 20 January 2011 - 12:00 PM

This line positions = new int[2][8]; is not the correct syntax for new. However since you are trying to use constants why not just define your variable as int positions[2][8];?

Jim

### #4 ishkabible

• spelling expret

Reputation: 1676
• Posts: 5,836
• Joined: 03-August 09

## Re: MultiDimensional Array Problem

Posted 20 January 2011 - 12:04 PM

first off you need to tell use exactly what is happening as apposed to what you expect to happen, with that being said im gonna take a guess at what your issue is. int** will define a pointer to a pointer of an int, how dose this help use? well a pointer can point to an array, say an array of pointers that each point to an array of int's. these arrays need to be allocated in some way, there are only 2 ways of allocating resources in C++ statically and dynamically(with almost always refer to the stack and heap respectively). now becuase you cannot allocate statically (declaring an array with a constant size) you need to allocate dynamically. how do you do this? using the 'new' keyword.

```int *x = new int[40]
```

this allocates an array of 40 integers. but you want a 2D array, well there are 2 ways to do this. 1 actually allocate the 2D array or allocate a 1D array and figure out the respective 2D position.

option 1:
```int **x = new int*[10];
for(int i=0;i<10;++i) x[i] = new int[10];
x[2][2] = 10;

```

this allocates a 2D array dynamically that can be accessed just like a 2D array;

option 2:
```int* x = new int[100]
x[2*10+2] = 10;

```

this allocates a 1D array dynamically that you can access in a 2D way.

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## Re: MultiDimensional Array Problem

Posted 20 January 2011 - 12:20 PM

Here, have a tutorial to read.