Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,271 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,437 people online right now. Registration is fast and FREE... Join Now!




calculating address offset for multi-dimensional array

 
Reply to this topicStart new topic

calculating address offset for multi-dimensional array

mmo
11 Dec, 2006 - 01:22 AM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2006
Posts: 4


My Contributions
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:
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 Dec, 2006 - 03:42 AM
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Calculating Address Offset For Multi-dimensional Array
12 Dec, 2006 - 05:51 PM
Post #2

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions

Could you post what expected result you want?biggrin.gif

This post has been edited by eXceed69: 12 Dec, 2006 - 09:02 PM
User is offlineProfile CardPM
+Quote Post

mmo
RE: Calculating Address Offset For Multi-dimensional Array
12 Dec, 2006 - 08:58 PM
Post #3

New D.I.C Head
*

Joined: 28 Nov, 2006
Posts: 4


My Contributions
QUOTE(mmo @ 11 Dec, 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:
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();
}



User is offlineProfile CardPM
+Quote Post

mmo
RE: Calculating Address Offset For Multi-dimensional Array
12 Dec, 2006 - 10:11 PM
Post #4

New D.I.C Head
*

Joined: 28 Nov, 2006
Posts: 4


My Contributions
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


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month