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].
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"
#include<iostream>
usingnamespace std;
int main()
{
constint 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;
}
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.
#include <iostream>
int main()
{
int SmallestNumber = -1;
constint Array[ 5 ] =
{
10, 20, -61, 8, 3
};
constint 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";
}
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;
// 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];
}
}
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.
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.
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.
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?
#include <iostream>
int main()
{
int SmallestNumber = -1;
constint Array[ 5 ] =
{
-10, -20, -61, -8, -3
};
constint 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";
}
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.
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.
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
inlinebool positive( int x )
{
return ( 0 < x );
}
Or you can use another function
1 2 3 4
inlinebool 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
inlinebool even( int x ) { return ( x % 2 == 0 ); }