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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool findMax(Array <int>array) {
if(array.getSize() == 0)
returnfalse;
int position = 0;
int value = array.getData(position);
for(int counter = 1; counter<array.getQuantity() - 1; counter++) {
int newValue = array.getData(counter + 1);
if(value < newValue) {
value = newValue;
position = counter + 1;
}
}
print("Min found at : " + int2Str(position+1) + ", Value : " + int2Str(value) + NEWLINE);
returntrue;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool findMin(Array <int>array) {
if(array.getSize() == 0)
returnfalse;
int position = 0;
int value = array.getData(position);
for(int counter = 1; counter<array.getQuantity() - 1; counter++) {
int newValue = array.getData(counter + 1);
if(value > newValue) {
value = newValue;
position = counter + 1;
}
}
print("Min found at : " + int2Str(position+1) + ", Value : " + int2Str(value) + NEWLINE);
returntrue;
}
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 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.