4 Replies - 738 Views - Last Post: 20 January 2011 - 12:20 PM Rate Topic: -----

#1 KingWill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Header:

 #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  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • 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];?

Please see this link for new.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: MultiDimensional Array Problem

Posted 20 January 2011 - 12:20 PM

Here, have a tutorial to read.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1