Results 1 to 12 of 12

Thread: arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Question

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    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;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Unhappy

    Hay tanks! I was able to get the upper bound of both 1 dim array's and 2 dim arrays


    Code:
    #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

    Code:
    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?
    Last edited by KingDavid; Feb 22nd, 2001 at 11:08 PM.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I don't know much about templates, but I'm pretty sure you can use a template function for that:

    Code:
    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.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, they're still there. You have to cast them to a proper pointer type before you can use them.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Question

    I don't know anything about Templates. How do you use them?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Like so:
    Code:
    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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    My mistake, and my apologies too. Knew there was something about em.
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, yours was fine -- function templates are useful too
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Question

    How that Different from a regular class?

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Unhappy

    I still dont understand that template stuff, but I gave it a Try.

    Code:
    #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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width