Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » array : minimum value
array : minimum value [message #35624] Wed, 07 March 2012 11:17 Go to next message
idkfa46 is currently offline  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 Go to previous messageGo to next message
Wolfgang is currently offline  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 Go to previous messageGo to next message
Sender Ghost is currently offline  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

Re: array : minimum value [message #35633 is a reply to message #35624] Thu, 08 March 2012 13:17 Go to previous messageGo to next message
idkfa46 is currently offline  idkfa46
Messages: 155
Registered: December 2011
Experienced Member
Lovely ...
Thanks both Very Happy

[Updated on: Thu, 08 March 2012 13:18]

Report message to a moderator

Re: array : minimum value [message #35634 is a reply to message #35626] Thu, 08 March 2012 18:06 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 263
Registered: July 2011
Location: Bangalore
Experienced Member


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


Code above will return minvalue as zero which is not array element

Small logical change

Define minNo = max-expected-value

This way you will get min value of array.


Warm Regards

Deepak
Re: array : minimum value [message #35635 is a reply to message #35634] Thu, 08 March 2012 21:37 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
sorry deep but i think you're wrong.
int minNo=0; don't defines the minimal number, it defines the index of the first value to compare with. (because of this you can do a for (int i=1...) instead of int i=0.

vet[0] is element of array
Re: array : minimum value [message #35655 is a reply to message #35635] Sat, 10 March 2012 15:50 Go to previous message
deep is currently offline  deep
Messages: 263
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Wolfgang,

I missed it



Warm Regards

Deepak
Previous Topic: Key pressing with MINGW and Windows XP does not work
Next Topic: Problem using more classes...
Goto Forum:
  


Current Time: Thu Mar 28 13:21:24 CET 2024

Total time taken to generate the page: 0.01236 seconds