Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Friends ..
Just to grasp the concept of storage ordering i'm trying to write a code for MultiDimentional array.

Here is the code ..
C++
#include <iostream>
#include <iomanip>
using namespace::std;

// Allocating Memory For Multi-Dimentional Array

void getData(int **arr, int row, int col){
    int offset;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++){
			offset = i * col + j;
			cin >> *(*(arr + offset));
		}
	}
}
void printData(int **arr, int row, int col){
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++){
			int offset = i * col + j;
			cout << setw(5) << *(*(arr + offset));
			cout << "\n";
		}
	}
}

int main(){
	int **board;
	int rows = 2, cols = 2;
	// creating the rows of board
	board = new int* [rows];
	// creating the cols of the board
	for (int i = 0; i < rows; i++)
	{
		board[i] = new int[cols];
	}
	getData(board, rows, cols);
	cout << "\n";
	printData(board, rows, cols);

	return 0;
}


I know the General Formula for offset is = Starting No Of Row * Total Columns + Starting No Of Column. Here i want your suggestions.. I want to go in 3 or 4 dimensions using offset ..
Moreover in different books, calculation for offset is differ .. So puzzled ..

in my code when i enter 3rd element the program Crashes ..
I hope you can understand the problem..
Keenly waiting your reply ..
Thanks -

UPDATED .. !!
C++
#include <iostream>
using namespace::std;

void getData(int *arr, int row, int col){ // by row
	int offset = -1;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			offset = i*col + j; // for row major: starting row * total_col + starting col
			cin >> *(arr + offset);
		}
	}
	cout << endl;
}
void printData(int *ar, int row, int col){ // by col 
	for (int i = 0; i < col; i++)
	{
		for (int j = 0; j < row; j++)
		{
			int offset = j *col + i; // col major: start_row * total_cols + start_col
			cout << *(ar + offset) << endl;
		}
		cout << "\n\n";
	}
	cout << endl;
}

int main(){
	int rows = 2;
	int cols = 2;
	int *weed = new int[rows*cols];
	getData(weed, rows, cols);
	printData(weed, rows, cols);
	
	return 0;
}


If i use single pointer it works like a charm .. :/
But not in the case of 2 pointers ..
Please Help ..
Posted
Updated 17-Mar-14 8:18am
v2

The name offset in your code is a little misleading as it normally refers to a byte offset. In your case you are simply calculating the element index in a one-dimensional array. What you are doing (correctly) is to map the elements of a two-dimensional array to a one-dimensional array. And you are doing it by storing the elements of each row in a contiguous range of cells. For example, for an array with two rows and four columns you would store the elements in the following order:
C++
R0C0 R0C1 R0C2 R0C3 R1C0 R1C1 R1C2 R1C3

RxCy denoting the element of row x and column y. And that is how you come to the formula of

(Number of columns) * rowIndex + colIndex

You can easily expand that scheme to more dimensions. For example, in a three-dimensional array we might call our dimensions: column, row, plane. And the formula would be:

(Number of rows * number of columns) * planeIndex + (number of columns) * rowIndex + columnIndex

Depending on which dimension is stored contiguous, the formula is a little different. For example, your could decide to store the plane dimension contiguously, then the columns, and outermost the rows. For this storage order the formula would be:

(Number of planes * number of columns) * rowIndex + (number of planes) * columnIndex + planeIndex

Both storage schemes are different. But as long as you use the same scheme for storing and retrieval, everything will work fine. The order in which dimensions are stored has however an influence on how the processor cache will be used and may have an impact on processing time for large numerical computations.
 
Share this answer
 
Comments
Usman Hunjra 20-Mar-14 15:21pm    
Superb .. ++Respect; :)
Thank You So SO Much Sir .. :)
nv3 21-Mar-14 5:41am    
My pleasure.
Hi,

this definitely is not correct. If the multidimensional array is static, then you can compute and use offset of anz element from the array base. But, if the array is dznamically allocated, you cannot compute any offset at all! That is because, the whole array is not stored in one continual piece of memeory. (Just type "c++ dynamic array" to google pictures) to see how it is placed in the memory.

Hope this helps,
JK
 
Share this answer
 
Comments
Usman Hunjra 17-Mar-14 14:22pm    
I've updated the code with single pointer .. Here as the array is dynamically allocated,
still i can calculate offset .. AND it works ..
But not for Double Pointer .. :(
Kucera Jan 19-Mar-14 4:09am    
Sure, sure. You can compute and use an offset only for an array (or sub-array) that is in a continuous part of memory - that is obvious.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900