vendredi 5 février 2021

control flow for boolean expression

I'm trying to return a boolean expression within an if else statement, from an output given by an array using this:

#include <iostream>

int main()
{
    const int x = 5;
    int arr[x] = { 1, 3, 5, 7, 9 };
    if( x == 5 ) {
         std::cout << "X less than 5:" << x;
    }
    else {
        std::cout << (x > 5) ? 1 : 0;
    }
}

Essentially I want to select the values within the array that prints those values into the console given the conditions above. The code above only gives X less than 5:5, I want it so that: X less than 5:1, 3, 5, 7, 9

Furthermore, is there a way to make a condition to that statement, where given that the above is true, then also print out those values less than 5 as 1 and those greater than 5 as 0? How would I do that?

Aucun commentaire:

Enregistrer un commentaire