Identify multiple numbers in a variable

Asked

Viewed 113 times

-1

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

My problem is identifying these cities by their number...

This order number can be identified by a single variable?

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

That’s all I got

//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");

}

1 answer

1


Array

It is not possible to store multiple values in a scalar variable as you wish. It is possible to store in a multi valued variable. A vector is the most obvious type to do this. An array can be stored in a single variable, but can only store 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 at all to keep it all in one, except with some trick possible if you have some restrictions.

If you choose a vector just add the order number whenever it meets the condition and then navigate the vector in a later loop.

Boolean

In fact you can make an array with an element for each city, there only keeps if the city meets the filter or not. In other words, it is a boolean operation.

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

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

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

You’d have to use the operator of bit to generate bits in the right places.

String

You still have the option to make a string with all values separated by some character or with fixed size. Technically it is still only a value, but you will give a semantics for this value that will behave as if there were several. I think I’ve gone too far and especially in this case I don’t see the need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.