Find the lowest positive value from an array

Pages: 12
Mar 27, 2012 at 12:52pm
Hello guys.
Can anyone help me with this problem.
I need to find the lowest positive value from an array
for example lowest (minimal) positive value from this A[n]={-2,8,-9,8,2,55,5,7}; array is 2. so positivemin=2.
an array may contain only negative numbers so we should consider that to.
the main problem that i have is that what value should positivemin get first when it gets compared to A[i].
Mar 27, 2012 at 1:29pm
Have you got your code so far?
Mar 28, 2012 at 10:20am
allright, here is what I've done.
it works fine but Im not sure if this is the best solution
if anyone has any idea please tell.
The problem was: "Find the lowest (minimal) positive value from an array"
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
#include<iostream>
using namespace std;
int main()
{
	const int n=5;
	int A[n]={10,20,-61,8,3};
	int i,x=0,p=0;//x is the lowest positive value, p is the position of that value
	for(i=0;i<n;i++)
	{
		if(A[i]>0)
		{
			x=A[i];
			goto a;
		}
	}
a:	if(x==0)
	{
		cout<<"This array does not any positive element\n";
		return 0;
	}
	for(i=0;i<n;i++)
	{
		if((A[i]>0)&&(A[i]<x))
		{
			x=A[i];
			p=i;
		}

	}
	cout<<"The minimal positive value is: "<<x;
	cout<<"\nThe position of this value is: "<<p+1;
	cout<<endl;
	return 0;
}
Last edited on Mar 28, 2012 at 10:21am
Mar 28, 2012 at 10:45am
Your code doesn't really make sense, at least the first loop doesn't. You could remove that part completely and it would work the exact same. Also don't use goto whenever you can avoid it, it's looked down upon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	int SmallestNumber = -1;

	const int Array[ 5 ] = 
	{ 
		10, 20, -61, 8, 3 
	};

	const int ArraySize = sizeof( Array ) / sizeof( int );

	for ( int i = 0; i < ArraySize; i ++ )
	{
		if ( SmallestNumber < 0 )
			SmallestNumber = Array[ i ];

		if ( Array[ i ] >= 0 && Array[ i ] < SmallestNumber )
			SmallestNumber = Array[ i ];
	}

	std::cout << "Smallest positive number in the array is " << SmallestNumber << ".\n";
}
Mar 28, 2012 at 10:52am
@codingshark
thank you but, for example when Array[5]={-10,-20,-61,-8,-3)
your code says
"Smallest positive number in the array is -3."


which I think does not make any sense

positive ... -3. ???
Mar 28, 2012 at 11:02am
Mar 28, 2012 at 11:06am
read my post again.
Here you go bro.
http://i39.tinypic.com/scucmq.png

this is when the array does not have any positive element.
can you see, every number has the minus sign.
Last edited on Mar 28, 2012 at 11:07am
Mar 28, 2012 at 11:11am
Agonche,

the idea is simple. If an array has no positive elements you return index of the minimum element equal to the size of your array. So if the position of the minimum element is equal to the siize of the array it means that the array has no positive elements and as a result has no the minimum positive element.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int min_element( int a[], int size )
{
   int i;
   
   for ( i = 0; i < size && a[i] <= 0; i++ );

   int min = i;

   for ( ; i < size; i++ )
   {
      if ( 0 < a[i] && a[i] < a[min] ) min = i;
   }

   return ( min );
}

So you return the position of the minimum positive element.

In the main you can write

1
2
3
int min = min_element( A, n );
if ( min != n ) std::cout << "minimum positive element is " 
                                   << A[min] << std::endl;
Mar 28, 2012 at 11:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Include this
#include<limits.h>


// In main
int SmallestNumber = INT_MAX;

for (int i=0; i < ArraySize; i++)
{
   if (Array[i] => 0 && Array[i] < SmallestNumber)
   {
      SmallestNumber = Array[i];
   }
}
Mar 28, 2012 at 11:14am
codingshark .

your code is incorrect in case an array contains all negative elements.
Last edited on Mar 28, 2012 at 11:14am
Mar 28, 2012 at 11:26am
iHutch105,

It is interesting how will you distinguish two situations when either all elements are non-positive or all elements are eqaul to INT_MAX?!:)
Mar 28, 2012 at 11:32am
Is it that hard to need more than 10 posts?
1
2
3
4
5
6
7
8
int Result = -1;
for( int i = 0; i < Size; i++)
{
     if(Array[i] >= 0 && (Result == -1 || Array[i] < Result))
     {
          Result = Array[i];
     }
}

If there is no positive value, Result is -1.
If you don't want to include the 0 as a positive value, change from
if(Array[i] >= 0 to if(Array[i] > 0
Mar 28, 2012 at 11:54am
EssGeEich

All such designs of searching a minimum or a maximum elements are bad. Because ecah time you shall set a new initial value for a target element.
So in general it is a bad algorithm.
Last edited on Mar 28, 2012 at 11:55am
Mar 28, 2012 at 12:01pm
I think i don't understand what you mean.
You mean I have to find a way not to set Result to -1?
For what reason? Doesn't it do what the OP asked to?
I cannot think of a faster search algorithm, and sure it will not fail to do its job.
Mar 28, 2012 at 12:16pm
EssGeEich,

I have said that your design of the algorithm is bad because if there is a need to find for example the minimum negative element you shall rewrite your code. So evvery time when the condition will be changed you will rewrite your code. It is a bad idea.
Tru youself for example to find negative minimum element.
Mar 28, 2012 at 12:17pm
Alright, so what's the big deal if my code doesn't work properly for all negative numbers. I made it as a simple proof of concept for what you were trying to do, the rest is up to you. What's so hard about adding a simple if check for if the final number is less than 0?

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
#include <iostream>

int main()
{
	int SmallestNumber = -1;

	const int Array[ 5 ] = 
	{ 
		-10, -20, -61, -8, -3 
	};

	const int ArraySize = sizeof( Array ) / sizeof( int );

	for ( int i = 0; i < ArraySize; i ++ )
	{
		if ( SmallestNumber < 0 )
			SmallestNumber = Array[ i ];

		if ( Array[ i ] >= 0 && Array[ i ] < SmallestNumber )
			SmallestNumber = Array[ i ];
	}

	if ( SmallestNumber < 0 )
		std::cout << "There are no positive numbers.\n";
	else
		std::cout << "Smallest positive number in the array is " << SmallestNumber << ".\n";
}
Mar 28, 2012 at 12:34pm
Try youself for example to find negative minimum element.


1
2
3
4
5
6
7
8
int Result = 1;
for( int i = 0; i < Size; i++)
{
     if(Array[i] <= 0 && (Result == 1 || Array[i] < Result))
     {
          Result = Array[i];
     }
}

Remember, you said the MINIMUM element (The nearest to INT_MIN).
If you meant the MAXIMUM element, change from Array[i] < Result to Array[i] > Result
Mar 28, 2012 at 12:46pm
Do you see that you have changed the initial value for result? You will do so every time! So your design is not suitable to write a general algorithm. So your design is very bad.
Mar 28, 2012 at 12:49pm
Well, how can you make a single algorithm for two different things?
You can make a single function with different algorithms. But they are two different calculations.
Last edited on Mar 28, 2012 at 12:49pm
Mar 28, 2012 at 1:16pm
I have already showed the example of such algorithm. The only thing that should be added is a predicate.

Here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int min_element( int a[], int size, bool compare( int ) )
{
   int i;
   
   for ( i = 0; i < size && !compare( a[i] ); i++ );

   int min = i;

   for ( ; i < size; i++ )
   {
      if ( compare( a[i] ) && a[i] < a[min] ) min = i;
   }

   return ( min );
}


For example, you can use the following function as an argument of the third parameter

1
2
3
4
inline bool positive( int x )
{
   return ( 0 < x );
}

Or you can use another function

1
2
3
4
inline bool negative( int x )
{
   return ( x < 0 );
}

So to find the minimum positive element you can write

int pos = min_element( array, size, positive );

To find minimum negative element you can write

int pos = min_element( array, size, negative );

As you see nothing was changed in the algorithm.

P.S. You even can find the minimum element among even numbers! Only use as the third parameter the function

inline bool even( int x ) { return ( x % 2 == 0 ); }
Last edited on Mar 28, 2012 at 1:26pm
Pages: 12