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

New Topic/Question
Reply




MultiQuote




|