PDA

Click to See Complete Forum and Search --> : arrays


KingDavid
Feb 21st, 2001, 11:21 AM
is there a ubound function in C++?

I came up with this, but it would only work with arrays with only one diminsional arrays. how would I find the upper bound of another dimnsion.

#define UBOUND(array) (sizeof(array)/sizeof(array[0]))

are there any other array functions in C++ that are good

parksie
Feb 21st, 2001, 02:53 PM
int x[5][3];
int y[10][9][8][7];
int mysize;

mysize = sizeof(x[0]) / sizeof(x[0][0]);
cout << mysize << endl;

mysize = sizeof(y[0][0][0]) / sizeof(y[0][0][0][0]);
cout << mysize << endl;

KingDavid
Feb 22nd, 2001, 10:03 PM
Hay tanks! I was able to get the upper bound of both 1 dim array's and 2 dim arrays



#include <iostream.h>


#define UBOUND(array) (sizeof(array)/sizeof(array[0]))
#define UBOUND2_1(array) (sizeof(array)/sizeof(array[0][0])/(sizeof(array[0]) / sizeof(array[0][0])))
#define UBOUND2_2(array) (sizeof(array[0]) / sizeof(array[0][0]))

unsigned int UBound(unsigned int SizeOfArray,unsigned int SizeOfDim1,unsigned int SizeOfType,
unsigned int NumOfDims,unsigned int DimReturned);

void main()
{
int x[30][2];
int y[22];

int dim1,dim2 = 0;

dim2 = sizeof(x[0]) / sizeof(x[0][0]);
dim1 = sizeof(x)/sizeof(x[0][0])/dim2;

cout << dim1 << "," << dim2 << endl;

cout << UBOUND2_1(x) << "," << UBOUND2_2(x) << endl;

int a,b;

a = UBound(sizeof(x),sizeof(x[0]),sizeof(x[0][0]),2,1);
b = UBound(sizeof(x),sizeof(x[0]),sizeof(x[0][0]),2,2);
cout << a << "," << b << endl;

a = UBound(sizeof(y),NULL,sizeof(y[0]),1,NULL);
cout << a << endl;
}
//===================================================================================
unsigned int UBound(unsigned int SizeOfArray,unsigned int SizeOfDim1,unsigned int SizeOfType,
unsigned int NumOfDims,unsigned int DimReturned)
{
if(NumOfDims == 1)
{

return (SizeOfArray/SizeOfType);
}
else if(NumOfDims == 2)
{
int d1,d2;
d2 = SizeOfDim1 / SizeOfType;
d1 = (SizeOfArray / SizeOfType)/d2;

if(DimReturned == 1)
return (d1);
else if(DimReturned == 2)
return (d2);
else
return (0);
}
else
{
return (0);
}
}



I wanted to create a function with only 3 paramiters but it wouldn't let me pass the array threw a void *, heres prototype


unsigned int UBound(void * array,unsigned int DimNum,unsigned int TotalNumberOfDim);


in the function every time I would try to get the size I would get errors

sizeof(array) or
sizeof(array[0])

errors...
illegal sizeof operand
subscript requires array or pointer type
'void *' : unknown size

I know very little about void pointers.
There's got to be an easier way to do this?

HarryW
Feb 22nd, 2001, 10:14 PM
I don't know much about templates, but I'm pretty sure you can use a template function for that:

template <class arraytype>
unsigned int UBound(arraytype * array,unsigned int DimNum,unsigned int TotalNumberOfDim)
{
//blah blah blah
}

Of course, I could be wrong.

In case you're wondering why it didn't work in the first place, I think passing void pointers (essentially variant pointers) was removed in C++ in order to enforce strong-typing, which helps trap errors at compile time. It's a good thing really.

parksie
Feb 23rd, 2001, 06:33 AM
No, they're still there. You have to cast them to a proper pointer type before you can use them.

KingDavid
Feb 23rd, 2001, 12:39 PM
I don't know anything about Templates. How do you use them?

parksie
Feb 23rd, 2001, 12:42 PM
Like so:

template<class T> class MyClass {
T Add(T x, T y) {
return x + y;
}
};

void main() {
MyClass<int> xxx;

cout << xxx.Add(4, 6) << endl;
}

HarryW
Feb 23rd, 2001, 12:44 PM
My mistake, and my apologies too. Knew there was something about em.

parksie
Feb 23rd, 2001, 12:50 PM
No, yours was fine -- function templates are useful too :D

KingDavid
Feb 23rd, 2001, 04:48 PM
How that Different from a regular class?

parksie
Feb 23rd, 2001, 04:51 PM
Notice the template<class T> section.

What it does is the compiler knows to use T as a parameter, and creates code for all versions of that class based on the type supplied.

KingDavid
Feb 24th, 2001, 12:18 AM
I still dont understand that template stuff, but I gave it a Try.


#include <iostream.h>

template <class ARRAY>
//|------------------------------------------------------------------------------{ UBound() }---
unsigned int UBound(ARRAY * array,unsigned int DimReturn, unsigned int TotalDims)
{
unsigned int Dim1,Dim2,Dim3,Dim4,Dim5;
if(DimReturn > TotalDims)
return (0);

if(TotalDims == 1)
return (sizeof(array)/sizeof(array[0]));
else if(TotalDims == 2)
{
Dim2 = (sizeof(array[0]) / sizeof(array[0][0]));
Dim1 = (sizeof(array)/sizeof(array[0][0]))/Dim2;

if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else
return (0);
}
else if(TotalDims == 3)
{
// no math yet
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;

if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else
return (0);
}
else if(TotalDims == 4)
{
// no math yet
Dim4 = 1;
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;

if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else if(DimReturn == 4)
return (Dim4);
else
return (0);
}
else if(TotalDims == 5)
{
// no math yet
Dim5 = 1;
Dim4 = 1;
Dim3 = 1;
Dim2 = 1;
Dim1 = 1;

if(DimReturn == 1)
return (Dim1);
else if(DimReturn == 2)
return (Dim2);
else if(DimReturn == 3)
return (Dim3);
else if(DimReturn == 4)
return (Dim4);
else if(DimReturn == 5)
return (Dim5);
else
return(0);
}
else
return (0);

}
//|------------------------------------------------------------------------{ ReturnSize() }----
template <class X>
unsigned int ReturnSize(X * ray)
{
return sizeof(ray);
}
//|------------------------------------------------------------------------------{ main() }----
void main()
{
int x[30][65];
int y[44];
int xResult;
int yResult;
int SizeOfArray;

xResult = UBound(x,2,2);// returns Corect size
cout << xResult << endl;

xResult = UBound(x,1,2);// returns 0
cout << xResult << endl;

/*
yResult = UBound(y,1,1);//error C2109
cout << yResult << endl;*/

SizeOfArray = ReturnSize(y);// returns 4 Wrong size
cout << SizeOfArray << endl;

SizeOfArray = sizeof(y);// returns Corect size: 44 * 4 = 176
cout << SizeOfArray << endl;
}
//|==================================================================={> End of Program <}=====



It don't like it when I try to pass an array with less dimensions than are in the functions, also it wont get the size of the entire array.