3 Replies - 9666 Views - Last Post: 12 December 2006 - 11:11 PM Rate Topic: -----

#1 mmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

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

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

Was This Post Helpful? 0
  • +
  • -

#3 mmo  Icon User is offline

  • New D.I.C Head

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

Re: calculating address offset for multi-dimensional array

Posted 12 December 2006 - 09:58 PM

View Postmmo, 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();
}

Was This Post Helpful? 0
  • +
  • -

#4 mmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1