### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````#include #include 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 :).

 ``123456789101112131415161718192021222324252627282930313233`` ``````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.