C++ Two Dimensional Array

Making an Array of labels

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 10486 Views - Last Post: 10 September 2010 - 11:28 AM Rate Topic: -----

#1 SuckMyBookah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-March 10

C++ Two Dimensional Array

Posted 23 August 2010 - 07:55 PM

For a project I am creating a Sudoku solver, however Im new to C++. The OS that I am using is Visual C++.

I want to create a two dimentional array of lables to create a Sudoku board(9*9) which i Can refer to in code easily, however am unsure where to begin.... any pointers/help would be create.




Cheers, Brad
Is This A Good Question/Topic? 0
  • +

Replies To: C++ Two Dimensional Array

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Two Dimensional Array

Posted 23 August 2010 - 08:03 PM

There's no such thing as a two dimensional array. You can, however, have an array of arrays.

It's also worth thinking about what the difference between a 4x4 and 16x1 matrix is.
Was This Post Helpful? 0
  • +
  • -

#3 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: C++ Two Dimensional Array

Posted 23 August 2010 - 08:32 PM

Quote

The OS that I am using is Visual C++.

OS means Operating System, ex. Windows 7.
You mean IDE

Quote

There's no such thing as a two dimensional array

Arrays of arrays can be considered multi-dimensional arrays. See http://www.cplusplus...utorial/arrays/ for more info.

Quote

array of lables

I'm not sure what you mean by lables? If you mean Goto Lables, you cannot create arrays of lables. Use an array of integers. Each array element would refer to a individual space in the puzzle, and would range from 1 - 9 (if i remember my Sodoko correctly =P )

Please try making your question more specific and I will try to help further

This post has been edited by Splatocaster: 23 August 2010 - 08:33 PM

Was This Post Helpful? 2
  • +
  • -

#4 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: C++ Two Dimensional Array

Posted 23 August 2010 - 10:00 PM

If you have two for loops the first loop could be made to handle rows and the second to input the label for each column so the first row of labels would be:
a1,a2,a3,a4,a5,a6,a7,a8,a9.
b1,b2..........etc.
hth`s
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: C++ Two Dimensional Array

Posted 24 August 2010 - 04:22 AM

View PostOler1s, on 23 August 2010 - 09:03 PM, said:

There's no such thing as a two dimensional array. You can, however, have an array of arrays.


???

What exactly would you call this?
const int BoardRows = 9;
const int BoardCols = 9;
typedef int Board[BoardRows][BoardCols];

Board board;



For the OP, I'd go with the above to start. You might find down the line that you want to store more than just a single value. Using an array of struct is a good choice:
struct Cell {
   int currentValue;
   int answerValue;
   bool isValid;
};
typedef Cell Board[BoardRows][BoardCols];



Dynamic 2D arrays, or an array of arrays, actually an array of pointers, are a true pain in the ass. If you need to allocate the thing dynamically, I'd just allocate a 1D array and figure out it's 2D position.

i.e.
int *board = new int[BoardRows * BoardCols];
int getIndex(int row, int col) { return row*BoardCols+col; }
int getValue(int *board, int row, int col) { return board[getIndex(row, col)]; }
void setValue(int *board, int row, int col, int value) { board[getIndex(row, col)] = value; }



Of course, board should really be a class...

That looks like more than enough to get you started. Good luck.
Was This Post Helpful? 2
  • +
  • -

#6 SuckMyBookah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-March 10

Re: C++ Two Dimensional Array

Posted 09 September 2010 - 04:54 PM

Hey guys,

I have a grid of labels (graphic numbers on my GUI placed in a 9x9 layout) which will display my array of numbers which will be the sudoku board. How ever I need some kind of array with I can easily place this numbers in using an array that has the same postions as a 9x9 grid.

So, I was thinking

each label is named depending on its location in an array so the first 3x3 grid would be


11 12 13
21 22 23
31 32 33

And so on through out the grid.

I know im having trouble explaining this has im very new to C++.

At the moment names are not important as im still trying to understand how this process would work. Iv attached my GUI so give you a better understanding what im trying to achive.

Attached File(s)


Was This Post Helpful? 1
  • +
  • -

#7 Switch()  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-September 10

Re: C++ Two Dimensional Array

Posted 09 September 2010 - 07:10 PM

A matrix wouldn't hurt in a program like this. I usually use them whenever dealing with any sort of grid in a program. They can be somewhat of a pain at times but they have proven to be quite efficient as well.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: C++ Two Dimensional Array

Posted 09 September 2010 - 08:06 PM

Quote

There's no such thing as a two dimensional array

So im guess you might call a "4D array" an array of arrays of arrays of arrays?
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Two Dimensional Array

Posted 09 September 2010 - 08:11 PM

You realize that my point was that 2D array, 3D, etc. are idiomatic terms rather than reflective of syntactical constructions? The OP wanted to know how to create a 2D array. I pointed out that a 2D array was simply an array of arrays.

This is as opposed to languages that do have syntactical notions of matrix or multidimensional array types.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 04:16 AM

View PostOler1s, on 09 September 2010 - 09:11 PM, said:

I pointed out that a 2D array was simply an array of arrays.


Not always true. While it is generally true for dynamically declared arrays, but not statically declared ones.

A statically declared 2D array is NOT an array of arrays, it is a 1D array with extra dimensions the compiler uses to calculate location.

You may test this out for yourself, if you like:
#include <iostream>

using namespace std;

const int BoardRows = 3, BoardCols = 3, BoardSize = 9;
typedef int Board1D[BoardSize];
typedef int Board2D[BoardRows][BoardCols];

void showLocation(Board1D B)/> {
	int *last = NULL;
	cout << "Board 1D storage" << endl;
	for(int i=0; i<BoardSize; i++) {
		int *mem = &b[i];
		cout << '(' << i << ") " << mem;
		if (last!=NULL) { cout << ' ' << (mem - last); }
		cout << endl;
		last = mem;
	}
	cout << endl;
}

void showLocation(Board2D B)/> {
	int *last = NULL;
	cout << "Board 2D storage" << endl;
	for(int row=0; row<BoardRows; row++) {
		for(int col=0; col<BoardCols; col++) {
			int *mem = &b[row][col];
			cout << '(' << row << ',' << col << ") " << mem;
			if (last!=NULL) { cout << ' ' << (mem - last); }
			cout << endl;
			last = mem;
		}
	}
	cout << endl;
}

int main() {
	Board1D b1;
	Board2D b2;
	showLocation(b1);
	showLocation(b2);
	return 0;
}



Results:
Board 1D storage
(0) 0x7fffb5770c40
(1) 0x7fffb5770c44 1
(2) 0x7fffb5770c48 1
(3) 0x7fffb5770c4c 1
(4) 0x7fffb5770c50 1
(5) 0x7fffb5770c54 1
(6) 0x7fffb5770c58 1
(7) 0x7fffb5770c5c 1
(8) 0x7fffb5770c60 1

Board 2D storage
(0,0) 0x7fffb5770c10
(0,1) 0x7fffb5770c14 1
(0,2) 0x7fffb5770c18 1
(1,0) 0x7fffb5770c1c 1
(1,1) 0x7fffb5770c20 1
(1,2) 0x7fffb5770c24 1
(2,0) 0x7fffb5770c28 1
(2,1) 0x7fffb5770c2c 1
(2,2) 0x7fffb5770c30 1


Was This Post Helpful? 1
  • +
  • -

#11 SuckMyBookah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-March 10

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 04:56 AM

Alright guys, I believe that I have well and truly enough to get me on my feet thank you very much!

If I get stuck you will prob hear from me in the future... but not before I have had a really good crack at it.

Cheers, Brad
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 05:01 AM

View Postbaavgai, on 10 September 2010 - 08:16 PM, said:

A statically declared 2D array is NOT an array of arrays, it is a 1D array with extra dimensions the compiler uses to calculate location.


I did not know that.

That's a very useful insight - thanks enormously for sharing it baavgai.
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 07:13 AM

Baavgai, I fail to see how pointing out the contiguity of arrays (which is part of the definition of an array) indicates anything about the type. Array of arrays is a type notion, while you've pointed out contiguity, which is a result of definition.

#include <iostream>
#include <typeinfo>

int intarray[5];
double dblarray[5];
int arrarray[5][5];

int main()
{
    std::cout << typeid(intarray[0]).name() << '\n';
    std::cout << typeid(dblarray[0]).name() << '\n';
    std::cout << typeid(arrarray[0]).name() << '\n';
}

//output
// int
// double
// int [5]



Each of the above is an array. The third array has element of type int [5], which is an array. All arrays have static storage, but if we had a 1D array, the element type should resolve to int, not int [5].

Along these lines, try passing arrarray as a function argument. If it were a 1D array, by array to pointer decay, we would pass in a pointer to int. However, that's not the case. The parameter type would have to be int (*)[5].

Saying a 2D array is not an array of arrays requires some inconsistency on your part.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 08:13 AM

View PostOler1s, on 10 September 2010 - 08:13 AM, said:

The parameter type would have to be int (*)[5].

Saying a 2D array is not an array of arrays requires some inconsistency on your part.


Actually, saying a 2D array is an array of arrays is not technically correct. A dynamically allocated structure can mimic a 2D array by using an array of pointers. The static allocation has no pointers.

What do you think int (*)[5] means, exactly? It means a pointer to a block of memory that has been given an extra dimension of 5. It is not an "array of arrays", it is a block of memory with a size (int) and a special parameter for width.


If you like, you may take the code above and add this:
void showLocationPointers(int *b , int rows, int cols) {
	int *last = NULL;
	cout << "showLocationPointers" << endl;
	for(int row=0; row<rows; row++) {
		for(int col=0; col<cols; col++) {
			int *mem = &b[row * cols + col];
			cout << '(' << row << ',' << col << ") " << mem;
			if (last!=NULL) { cout << ' ' << (mem - last); }
			cout << endl;
			last = mem;
		}
	}
	cout << endl;
}

LocationPointers(&b2[0][0], BoardRows, BoardCols);



Simply, you can't do that with an array of pointers. There are no "pointers" in that single, contiguous, block of data: period.

Maybe it's just semantics. Maybe I'm just not being clear. I'll defer to the creators and leave it at that.

Quote

5.9 Pointers vs. Multi-dimensional Arrays

Newcomers to C are sometimes confused about the difference between a two-dimensional array and an array of pointers, such as name in the example above. Given the definitions

int a[10][20];
int *b[10];

then a[3][4] and b[3][4] are both syntactically legal references to a single int. But a is a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col]. For b, however, the definition only allocates 10 pointers and does not initialize them; initialization must be done explicitly, either statically or with code. Assuming that each element of b does point to a twenty-element array, then there will be 200 ints set aside, plus ten cells for the pointers.

-- The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.

This post has been edited by baavgai: 10 September 2010 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Two Dimensional Array

Posted 10 September 2010 - 10:11 AM

Quote

Actually, saying a 2D array is an array of arrays is not technically correct. A dynamically allocated structure can mimic a 2D array by using an array of pointers. The static allocation has no pointers.
The allocation of an object is orthogonal to the type of the object. Furthermore, you seem to suggest that it is impossible to allocate a 2D array. But that's false. It's not typically done, as one replicates a 2D array using pointers, but the following is valid.

#include <iostream>
#include <typeinfo>


int main()
{
    using namespace std;
    int (*p)[5] = new (int [5][5]);
    int *last = NULL;
    for(int row=0; row<5; row++) {
        for(int col=0; col<5; col++) {
            int *mem = &p[row][col];
            cout << '(' << row << ',' << col << ") " << mem;
            if (last!=NULL) { cout << ' ' << (mem - last); }
            cout << endl;
            last = mem;
        }
    }
}



The above code allocates a 2D array, which preserves contiguity between elements, by definition of array. The relevant line is int (*p)[5] = new (int [5][5]); . Notice how the only pointer involved is that which points to the 2D array. The array itself is not constructed from pointers.

Quote

What do you think int (*)[5] means, exactly? It means a pointer to a block of memory that has been given an extra dimension of 5.
I made that point above, when I talked about array to pointer decay.

Quote

Simply, you can't do that with an array of pointers.
Correct, but irrelevant. An array of pointers is not an array of arrays, which is what we refer to when we talk about 2D arrays.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2