-2
    string result;

    double zpls, zmin, ypls, ymin, xpls, zmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

How to find "first minimum positive" among these values and set

    result = "+Z";// or wichever value is lowest -z, +Y etc  

(in C++)

i tried arrays , if else and min(zpls,min(zmin,min(....))) // with #include <algorithm>

but could not get it correctly

thanks

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
Raj
  • 107
  • 2
  • 10

4 Answers4

2

As long as you have the data in independent variables, you'll need to do an if-else

result = ""; // no result yet
double minValue = std::numeric_limits<double>::max( );
if( zplus > 0 && zpls < minValue ) {
  minValue = zpls;
  result = "+Z"
}
if( zmin > 0 && zmin < minValue ) {
  minValue = zmin;
  result = "-Z"
}

and so forth, in that pattern. At the end result will either have the string you want, or it will be empty (in which case, there were no positive values.)

Here is a complete sample:

#include <iostream>
#include <limits>
#include <string>

int main () {
    std::string result;

    double zpls, zmin, ypls, ymin, xpls, xmin ;

    zpls = 4.2;
    zmin = 0;
    ypls = -2;
    ymin = 4.2;
    xpls = 6;
    xmin = 8;

    result = ""; // no result yet
    double minValue = std::numeric_limits<double>::max( );
    if( zpls > 0 && zpls < minValue ) {
      minValue = zpls;
      result = "+Z";
    }
    if( zmin > 0 && zmin < minValue ) {
      minValue = zmin;
      result = "-Z";
    }
    if( ypls > 0 && ypls < minValue ) {
      minValue = ypls;
      result = "+Y";
    }
    if( ymin > 0 && ymin < minValue ) {
      minValue = ymin;
      result = "-Y";
    }

    if( xpls > 0 && xpls < minValue ) {
      minValue = xpls;
      result = "+X";
    }
    if( xmin > 0 && xmin < minValue ) {
      minValue = xmin;
      result = "-X";
    }


    std::cout << "The first mininum positive element is: " << result << "\n";
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • i have already tried this and also six temp variables to ::max() and comparing them .. all i get is minimum value ..which is not sufficient to set FLAG "+Z" – Raj Dec 22 '11 at 17:15
  • Do you see the line in my code that reads `result = "+Z"`? That is where you set the flag. – Robᵩ Dec 22 '11 at 17:24
  • @Raj, I notice that you have asked five questions, but haven't accepted any of the answers. Please go through your questions. Please upvote (click the up-arrow) the answers that help you, and then accept (click the check-mark) the answers that help you the most. – Robᵩ Dec 22 '11 at 17:47
0

if you run std::sort on any collection of numbers, the "first minimum positive" will simply be the "first positive".

If you run std::find_if on that sorted collection, you can find that first positive.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

Don't adjust algorithms. The algorithm is clear ("find minimum"). Adjust the search criteria instead, and stay in O(n) realm.

Code.

#include <algorithm>
#include <vector>
#include <iostream>
int main () {
    // std::min_element()
    std::vector<float> vec;

    vec.push_back(0);
    vec.push_back(-1);
    vec.push_back(-2);
    vec.push_back(2);
    vec.push_back(4);

    auto cmp = [](float lhs, float rhs) {
        const bool lz = lhs < 0,
                   rz = rhs < 0;
        if (lz && rz) return lhs < rhs;
        if (lz) return false;
        if (rz) return true;
        return lhs < rhs;
    };

    const float mp = *std::min_element (vec.begin(), vec.end(), cmp);        
    std::cout << mp << '\n';

    // demonstration of our comparison
    sort (vec.begin(), vec.end(), cmp);
    for (auto it=vec.begin(), end=vec.end(); it!=end; ++it)
        std::cout << *it << " ";
    std::cout << std::endl;
}

Output.

0
0 2 4 -1 -2

Explanation.

Our sorting function is encoded in cmp. It checks the signs of its operands. If both are negative, the bigger one wins. If only the LHS is negative, then RHS is automatically preferred in the sorting. On the opposite, if the RHS is negative, LHS is preferred. Of both are positive, we fall back to the normal order.

Nice thing is that this runs exactly one time over the range and in O(n).

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

are you really need sepearte variables. Why not to design like this:

enum Tags
{
 ZPlus,
 ZMinus,
 Etc
};

const std::string [] names = {"z+", "z-", "etc" }

int values[3];

and insted of zmin = -3; you get values[ZMinus] = -3; then you can find minimum index in values array and return names[minimumIndex];

Lol4t0
  • 12,444
  • 4
  • 29
  • 65