Frage

I have two questions:

  1. If we want to declare one million variable names all having the same length then what should be the smallest length possible to use?

  2. If we want to declare one million variable names of any possible valid length then what is the maximum size of the variable name required?

War es hilfreich?

Lösung

The first character in an identifier must be a-z, A-Z, or underscore, for 53 possibilities. Subsequent characters can be digits 0-9 too, for 63 possibilities. So n characters give 53 * (63**(n-1)) legal identifiers:

Length Names
1      53
2      3339
3      210357    // much less than 1000000
4      13252491  // much more than 1000000

Some of these are reserved words (int, return etc.), which we are not allowed to use. Others begin with an underscore, which the C++ standard frowns on. But these small considerations don't change the answer, which is 4 (to both your questions).

Andere Tipps

You have at least 53 chars which could be used as first char of variable name ('a'-'z', 'A'-'Z', and '_'). Then you can also use digits, so you have 63 possible chars. 53*63*63*63 > 1 000 000, so you don't need names longer than 4 chars.

How long variable name could be depends only on compiler.

This is not programming question, but math question. To name variable we have to use letters a-z, numbers 0-9 and few special characters (-, _). So we can use appr. 39 different characters. This is our alphabet. So to store 1000000 names we have to use names with minimum length:

log(base39)(1000000) = ~3.8

So to name million variables (to get 1000000 unique names), you have to use at least 4 characters in name. (I know about not allowed "023abc" variable names, but if we exclude them, result will not change a lot).

Answer: 4 chars. This is minimum character count. Maximum name length is limited by your compilator.

If I have understood you correctly then C++ has special template class std::numeric_limits that allows to get maximum and minimum values for any arithmetic type. To use it you need to include header <limits>

For example that to get the maximum and minimum values for type int you could write

#include <iostream>
#include <limits>

int main()
{
   std::cout << "The maximum value of int is " << std::numeric_limits<int>::max() << std::endl;
   std::cout << "The minimum value of int is " << std::numeric_limits<int>::min() << std::endl;
}

If you are asking about the minimum length for all one million identifiers having the same length then you should calculate the number of permutations such that n! will be not less than one million

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top