Saturday, May 9, 2009

C++ array function programming.?

I'm currently trying to develop an array class that addresses some of the shortcomings of the standard C++ array implementation.





The file has functions for an array. I'm having trouble coding correct functions. Please help








double GetData(int); //returns value of the int-th element. If past the array boundaries, then return first or last element





public Resive(int); //resizes the array to the new int size





public Output(); //prints the entire array onto stdout





public Input(); //inputs individual elements of the array





~array(); //destructor





I'm currently working on this, but these are giving me the most problems. Any advice or help is GREATLY appreciated. Any snippets of codes or comment lines are greatly appreciated as well.

C++ array function programming.?
If your array class has a member variable size, then double GetDouble(int element) should be straightforward:





{


if (element %26lt;0) return Array[0];


else if (element %26gt;= size) return Array[size-1];


else return Array[element];


}





Remember an array of size elements is number 0 through size-1, not 1 through size.





As a C programmer, primarily, I would use Realloc for the Resize(int) program. Information here:





http://www.cplusplus.com/reference/clibr...





for output what is so hard about:





for (int counter=0;counter %26lt; size; counter+=1)


cout %26lt;%26lt; array[counter];





The others I'll leave you with. As I said I'm primarily a C programmer and will occasionally do constructors but VERY rarely destructors. I hate C++ so much I won't even do it if I can't get away with an occasional memory leak.





It's not the classes I hate, it's the namespaces.


No comments:

Post a Comment