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.

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)
		return false;

	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);
	
	return true;
}


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)
		return false;

	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);

	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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int findMaxPosition(Array <int>array) {
	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) {  // change '<' to '>' 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// array.h
#ifndef ARRAY_H
#define ARRAY_H

#include <string>

template<class type> 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<quantity; counter++)
				if(aType == data[counter])
					return false;

			return true;
		}

		bool insert(type aType, int position = 0) {
			if(quantity + 1 <= size && position <= quantity && isNotDuplicate(aType)) {
				for(int counter=quantity; 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; counter<quantity; counter++)
					data[counter] = data[counter + 1];

				quantity--;
				return true;
			} else {
				return false;
			}
		}

		type getData(int position) {
			return data[position];
		}
	private:
		int size;
		int quantity;
		type *data;	

		void setSize(int size) {
			this->size = 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.