### Can we make these 2 function shorter?

Difference between these 2 function is '>' for finding max value and '>' for finding min value.
I want to make it shorter because there 2 function are very much the same.

 ``12345678910111213141516171819`` ``````bool findMax(Array array) { if(array.getSize() == 0) return false; int position = 0; int value = array.getData(position); for(int counter = 1; counter

 ``12345678910111213141516171819`` ``````bool findMin(Array array) { if(array.getSize() == 0) return false; int position = 0; int value = array.getData(position); for(int counter = 1; counter newValue) { value = newValue; position = counter + 1; } } print("Min found at : " + int2Str(position+1) + ", Value : " + int2Str(value) + NEWLINE); return true; }``````
Your design of the functions is very bad. Each function shall do some one thing. If you named a function as findMax then it shall return the maximum element.
I have to find min, max and as well as their position.
So if I refactor the code to :

int findMax(Array <int>array);
int findMin (Array <int>array);
int findPosition(Array <int>array, int value);

Is this a good idea?

but this is still not the answer for my topic findMin() and findMax() are so very similar. Can I make them shorter?
I think that it is redundant to have two functions one of which returns the max and other returns the position of the max. Write only one function which will return the position of the max element. Knowing the position a user can get the maximum element.
Last edited on
Oh, You are right.

I can make only make findMaxPosition and findMinPosition and still can get max and min from these functions, right?

now I only have 2 function findMaxPosition and findMinPosition :

 ``1234567891011121314`` ``````int findMaxPosition(Array array) { int position = 0; int value = array.getData(position); for(int counter = 1; counter' to finMinPosition value = newValue; position = counter + 1; } } return position; }``````

But there 2 functions are very similar to each other, Is there a way to make it shorter?
Can it be that getQuantity() will return 0?
I've made my Array Class that store an array.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````// array.h #ifndef ARRAY_H #define ARRAY_H #include template class Array { public: Array(int size) { setQuality(0); setSize(size); data = new type[size]; } int getSize() { return size; } int getQuantity() { return quantity; } std::string getType() { std::string str = typeid(type).name(); return str; } bool isNotDuplicate(type aType) { for(int counter=0; counter=position; counter--) data[counter] = data[counter - 1]; data[position] = aType; quantity++; return true; } else { return false; } } bool deleteAt(int position) { if(position < quantity) { for(int counter=position; countersize = size; } void setQuality(int quality) { this->quantity = quality; } }; #endif ``````

getQuantity() return number of items that store in this Array class.
Last edited on
Anybody?
Topic archived. No new replies allowed.