Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting min and max from array??

Status
Not open for further replies.

maximusAF

IS-IT--Management
Feb 11, 2003
6
0
0
US
I need some advice for getting the min and max from an array. I have a loop that reads the number and then calls a void function to do the calculations. My call by reference parameters/variables are not working right. When I output the results to the screen, it shows the max and min as the last number in the array. What could be the problem here? For some reason the number that is read from the array in the loop is not being passed or processed correctly. Thanks.
 
Post the code of the void function that determines the max and min.

Matt
 
Have you tried using the STL functions [tt]min_element[/tt] and [tt]max_element[/tt] for this? You pass in an array and it returns the pointer of the max/min element. You can then use this pointer to also discover the index of the min/max element:[tt]

#include <algorithm>
#include <iostream>

int main(void)
{
int array[] = {54,23,12,78,99,102,71};

const int* e = std::min_element(array,array+7);
int pos = e-array;

std::cout << &quot;*e=&quot; << *e << std::endl;
std::cout << &quot;position=&quot; << pos << std::endl;

return 0;
}
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Is there any way of doing this for a 2-D array?

Thanks in advance
 
The quick way to do it with the 2d array is to apply the 1d array method.

char array[20][20];

array[0] is an array of 20 characters 1d
array[1] is an array of 20 characters 1d

However, you can get tricky with the 2d array and treat is as a 1d array of size 400. We know that the allocation is in one entire memory block and not split up with the above declaration.

Matt

 
Is this what you mean (using the code provided earlier)

#include <algorithm>
#include <iostream>

int main(void)
{
int array[2][7] = {{54,23,12,78,99,102,71},{10,28,4,9,5,9,3}};

const int* e = std::min_element(array[1][1],array[1][7]);
int pos = e-array;

std::cout << &quot;*e=&quot; << *e << std::endl;
std::cout << &quot;position=&quot; << pos << std::endl;

return 0;
}

This throws up error messages. Sorry if I seem a bit slow but I am a complete novice to C++ and it is part of my college course.

Thanks in advance

Jonathan
 
I have never used min_element but I would think it COULD work like what I suggest below ***but I am not sure***. Does min_element dereference the pointer and compare the value or does it just compare pointer values? If it is just pointer values, you will always get array[0][0] pointer location back. Try this and let me know if it works.

Matt

Code:
#include <algorithm>
#include <iostream>

int main(void)
{
    int        array[2][7] = {{54,23,12,78,99,102,71},{10,28,4,9,5,9,3}};

   const      int* e = std::min_element(&array[0][0],&array[1][7]);
   int        pos = e-array;

   std::cout << &quot;*e=&quot; << *e << std::endl;
   std::cout << &quot;position=&quot; << pos << std::endl;

   return 0;
}


 
small typo... replace array[1][7] with [1][6]

also

pos will be a value that will look wierd if you are not in the first row i.e. array[0];

int idx1 = pos/7;
int idx2 = pos%7;

cout<<&quot;min element at array[&quot;<<idx1<<&quot;][&quot;<<idx2<<&quot;]\n&quot;;

Matt
 
Thanks Matt,

Finding the value works great. There is an error on the position but I do not need that.

Thanks again

Jonathan
 
Remember that there is 0 indexing with arrays, i.e. the second element is element [0][1].





int array[2][7] = {{54,23,12,78,99,102,71},{10,28,4,9,5,9,3}};

const int* e = std::min_element(array[0],array[0] + 14);
int pos = e - array[0],
idx1 = pos/7,
idx2 = pos%7;

cout<<&quot;min element at array[&quot;<<idx1<<&quot;][&quot;<<idx2<<&quot;]\n&quot;
<< &quot;*e=&quot; << *e << endl
<< &quot;position=&quot; << pos << endl;
 
At they have logically 2-D arrays that you can iterate through as if they were any other sequence. One of those + min/max_element would be the fastest way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top