Help with Array Location

I need assistance with this..
All my code is good, except the functions: findMacLoc and findMinLoc .
They don't give me the correct array location of the max/min number in the array.
I know is something simple I need to tweak in those two functions.

Thanks.


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <time.h>
using namespace std;

void printArray(int a [], int & size)
{
	int i;
	for (i = 0; i < size; i++)
		cout << a[i] << " ";

	cout << endl;
}

void initArray(int a [], int size)
{
	a[0] = 5;
	a[1] = 1;
	a[2] = 9;
	a[3] = 2;
	a[4] = 11;
	a[5] = 1;
	a[6] = 0;
	a[7] = 4;
	a[8] = 10;
	a[9] = 8;
}

int findMin(int a [], int size)
{
	int m = a[0];
	for (int i = 0; i < size; i++)
	{
		if (m < a[i])
			m = m;
		else
			m = a[i];

	}

	return m;

}

int findMax(int a [], int size)
{
	int m = a[0];
	for (int i = 0; i < size; i++)
	{
		if (m > a[i])
			m = m;
		else
			m = a[i];

	}

	return m;

}

int addArray(int a [], int size)
{
	int sum = 0;
	for (int i = 0; i < size; i++)
		sum = sum + a[i];

	return (sum);
}

int findMaxLoc(int a [], int size)
{
	int m = a[0];
	int retainer;
	for (int i = 0; i < size; i++)
	{
		if (m < a[i])
		{
			m = m;
		}
		else
		{
			m = a[i];
			retainer = i;
		}
	}

	return retainer;

}

int findMinLoc(int a [], int size)
{
	int m = a[0];
	int retainer;
	for (int i = 0; i < size; i++)
	{
		if (m > a[i])
		{
			m = m;
		}
		else
		{
			m = a[i];
			retainer = i;
		}
	}

	return retainer;

}

int main()
{
	int a[10];
	int size = 10;

	initArray(a, size);
	cout << "Array: ";
	printArray(a, size);
	cout << "Max = " << findMax(a, size) << endl;
	cout << "Min = " << findMin(a, size) << endl;
	cout << "Max Location = " << findMaxLoc(a, size) << endl;
	cout << "Min Location = " << findMinLoc(a, size) << endl;
	cout << endl;
	system("Pause");
}
You had your <s and your >s reversed on lines 75 and 96. Do you see why? Compare those functions with your findMax/findMin functions if not.

Also, you may want to consider initializing retainer to zero in each of those functions.

Also, why all the unnecessary m = m statements? O.o

-Albatross
Wow well that was simple -_-

I'm not sure why they're there. Maybe because I'm weird. Lol

This is the new code and it works :).

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
int findMaxLoc(int a [], int & size)
{
	int m = a[0];
	int retainer;
	for (int i = 0; i < size; i++)
	{
		if (m < a[i])
		{
			m = a[i];
			retainer = i;
		}
	}

	return retainer;

}

int findMinLoc(int a [], int & size)
{
	int m = a[0];
	int retainer = 0;
	for (int i = 0; i < size; i++)
	{
		if (m > a[i])
		{
			m = a[i];
			retainer = i;
		}
	}

	return retainer;

}


thanks!
Topic archived. No new replies allowed.