Identify multiple numbers in a variable

-1
  

Create a program that, given a set of temperatures of 8 Portuguese cities, identified by an order number, indicate which ones have a temperature higher than the country average.

My problem is to identify these cities by their number ...

Is it possible to identify this order number through a single variable?

For example, the person writes the order number (which corresponds to a city) and the display shows the order numbers if the city temperature is higher than the country average.

I only have this

//var
float temperatura;
int nordem,i;


//codigo
for(i=1;i<=8;i++)
{
    cout<<"Introduza o nº de ordem: ";
    cin>>nordem;

    cout<<"Introduza a temperatura dessa cidade: ";
    cin>>temperatura;

        if(temperatura>16){
        nordem
    }

}cout<<"As seguintes cidades estão acima da média: "<<nordem<<"\n";

system("pause");

}

    
asked by anonymous 03.12.2017 / 14:48

1 answer

1

You can not save multiple values in a scalar variable as you want. You can save to a multi valued variable. A vector is the most obvious type to do this. A vector can be stored in a single variable, but can only save multiple values in it because it is composed of several variables.

And yes, each element of the vector is an individual variable. So there's no chance of saving everything in one unless with some possible trick if you have some restrictions.

If you choose a vector, simply add the order number each time you meet the condition and then browse the vector in a subsequent loop.

You can actually make a vector with one element for each city, then only if the city meets the filter or not. In other words, it is a Boolean operation.

There you can use a trick and even use a variable. If you guarantee that you have a maximum of 8 cities, you can create a variable of type char (with unsigned int up to 32 cities possible in most architectures). Then you save if the city meets the criterion with only one bit.

Then the position of the bit is the position of the index of what would be its vector and the bit there indicates whether that city has temperature above the average or not.

Of course this only works if the cities have the sequence number, preferably 0 to 7, but it can be done even starting from a larger number. It would also be possible if you can normalize to a sequence through math.

You would have to use the bit operator to generate the bits in the correct places .

You still have the option to make a string with all values separated by some character or fixed length. Technically it is still only one value, but you will give a semantics to this value that will behave like several. I think gambiarra too and especially in this case I see no need.

    
03.12.2017 / 15:48