Home » Community » Newbie corner » array : minimum value
array : minimum value [message #35624] |
Wed, 07 March 2012 11:17  |
idkfa46
Messages: 155 Registered: December 2011
|
Experienced Member |
|
|
Hello guys,
I have to find the minimum value into an array of 6 elements... here my solutions:
1)
...
double vet[] = { _a, _b, _c, _d, _e, _f };
double r = min(_a, _b);
r= min(r,_c);
r= min(r,_d);
r= min(r,_e);
r= min(r,_f);
return r;
2)
double vet[] = { _a, _b, _c, _d, _e, _f };
double min = vet[0];
for (int i=0; i<(sizeof vet/sizeof *vet ); i++)
{
if(vet[i] < min){ min = vet[i];}
}
return min;
There is an easyer way to do it ?!
thanks Matteo
|
|
|
Re: array : minimum value [message #35626 is a reply to message #35624] |
Wed, 07 March 2012 21:27   |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
Hi,
I think your second solution is fine?!
Maybe just a little change:
double vet[] = { 10,23423,232,2,3,4,5,6,7 };
int minNo=0;
for (int i=1;i <(sizeof vet/sizeof *vet );i++)
if(vet[i] < vet[minNo])
minNo = i;
return vet[minNo];
greetings
Wolfgang
|
|
|
Re: array : minimum value [message #35631 is a reply to message #35624] |
Thu, 08 March 2012 12:31   |
Sender Ghost
Messages: 301 Registered: November 2008
|
Senior Member |
|
|
Hello.
idkfa46 wrote on Wed, 07 March 2012 11:17 | There is an easier way to do it ?!
|
If you meant already defined functions to find minimum (maximum) value of the array, then there are two function templates from std and Upp namespaces (corresponding to STL and NTL (U++)), at least:
std::min_element (std::max_element),
Upp::MinElement (Upp::MaxElement).
#include <Core/Core.h>
CONSOLE_APP_MAIN {
int vec[] = { 5, 4, 3, 2, 1, 6, 7, 8, 9, 10 },
*Upp_value = Upp::MinElement(vec, vec + __countof(vec)),
*std_value = std::min_element(vec, vec + __countof(vec));
ASSERT(Upp_value == std_value);
Upp::Cout() << "Minimum value"
<< "\nUpp: " << *Upp_value
<< "\nstd: " << *std_value
<< "\n\nIndex of minimum value: " << Upp_value - vec << '\n';
}
[Updated on: Thu, 08 March 2012 12:40] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Tue Apr 29 09:32:37 CEST 2025
Total time taken to generate the page: 0.00813 seconds
|