# calculating address offset for multi-dimensional array

Page 1 of 1

## 3 Replies - 12008 Views - Last Post: 12 December 2006 - 11:11 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=21928&amp;s=c16559c2902f5034e11fe1148d998ec8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mmo

Reputation: 0
• Posts: 4
• Joined: 28-November 06

# calculating address offset for multi-dimensional array

Posted 11 December 2006 - 02:22 AM

Hi everyone,

I am working on an assignment that I need to calculate address offset for any multi-dimensional array, and give the array size.

However, my output does not add up to the intended result.

Anybody with an idea of how to resolve the inaccuracy.

Code:
```#include<vector>
#include<iostream>

using namespace std;

int dimension();
//int n;

vector< vector<int> >m(dimension(), vector<int>(5));
char order();

int memory_offset, ArraySize, dimen;
const int element_size=2;

void init_m()
{
char ordering = order();
ArraySize=0;

for (int i = 0; i<m.size(); i++)
for(int j = 0; j<m[i].size(); j++) {
m[i][j] = 10*i+j;
ArraySize=ArraySize+m[i][j];
if(ordering == 'R'){
memory_offset = (m.size()*i + 1* j)*element_size;
}
if(ordering == 'C'){
memory_offset = (1*i +m.size()*j)*element_size;
}

}

}

void print_m()
{
for (int i = 0; i<m.size(); i++) {
for(int j=0; j<m[i].size(); j++)
cout<<m[i][j]<<'\t';
cout<<'\n';

}
cout<<"Offset= "<< memory_offset<<endl;
cout<<"Array size = " <<ArraySize<<endl;
}

char order()
{
char ordering = 'R, C';

cout<<"Enter R(ow) or C(olumn) major order (R or C) :"<<endl;
cin>>ordering;

return ordering;
}

int dimension()
{
int ub, lb, d, cols;
cout<<"Enter dimension :"<<endl;
cin>>d;
int columns = 0;
for(int i=1; i<=d; i++)
{
cout<<"Enter upper and lower bound subscript " <<i<<": "<<endl;
cin>>ub>>lb;
cols = ub-lb +1;
columns =columns + cols;
}

return d;

}

int main()
{
init_m();
print_m();
}
```

This post has been edited by Dark_Nexus: 11 December 2006 - 04:42 AM

Is This A Good Question/Topic? 0

## Replies To: calculating address offset for multi-dimensional array

### #2 eXceed69

• "Super Sentai Knight Of DawN"

Reputation: 8
• Posts: 688
• Joined: 12-November 06

## Re: calculating address offset for multi-dimensional array

Posted 12 December 2006 - 06:51 PM

Could you post what expected result you want?

This post has been edited by eXceed69: 12 December 2006 - 10:02 PM

### #3 mmo

Reputation: 0
• Posts: 4
• Joined: 28-November 06

## Re: calculating address offset for multi-dimensional array

Posted 12 December 2006 - 09:58 PM

mmo, on 11 Dec, 2006 - 02:22 AM, said:

Hi everyone,

I am working on an assignment that I need to calculate address offset for any multi-dimensional array, and give the array size.

However, my output does not add up to the intended result.

Anybody with an idea of how to resolve the inaccuracy.

Code:
```#include<vector>
#include<iostream>

using namespace std;

int dimension();
//int n;

vector< vector<int> >m(dimension(), vector<int>(5));
char order();

int memory_offset, ArraySize, dimen;
const int element_size=2;

void init_m()
{
char ordering = order();
ArraySize=0;

for (int i = 0; i<m.size(); i++)
for(int j = 0; j<m[i].size(); j++) {
m[i][j] = 10*i+j;
ArraySize=ArraySize+m[i][j];
if(ordering == 'R'){
memory_offset = (m.size()*i + 1* j)*element_size;
}
if(ordering == 'C'){
memory_offset = (1*i +m.size()*j)*element_size;
}

}

}

void print_m()
{
for (int i = 0; i<m.size(); i++) {
for(int j=0; j<m[i].size(); j++)
cout<<m[i][j]<<'\t';
cout<<'\n';

}
cout<<"Offset= "<< memory_offset<<endl;
cout<<"Array size = " <<ArraySize<<endl;
}

char order()
{
char ordering = 'R, C';

cout<<"Enter R(ow) or C(olumn) major order (R or C) :"<<endl;
cin>>ordering;

return ordering;
}

int dimension()
{
int ub, lb, d, cols;
cout<<"Enter dimension :"<<endl;
cin>>d;
int columns = 0;
for(int i=1; i<=d; i++)
{
cout<<"Enter upper and lower bound subscript " <<i<<": "<<endl;
cin>>ub>>lb;
cols = ub-lb +1;
columns =columns + cols;
}

return d;

}

int main()
{
init_m();
print_m();
}
```

### #4 mmo

Reputation: 0
• Posts: 4
• Joined: 28-November 06

## Re: calculating address offset for multi-dimensional array

Posted 12 December 2006 - 11:11 PM

memory_offset and arraySize are delacred gobally.Using the following input;

rowmajor and 3 dimensions,

lower and upper bounds for subscripts
1: 1 4
2: 1 3
3: 2 6

set of subscripts: 2 3 4

output
offset to 2, 3, 4 is : 54
Array size is : 120