There is an important semantic difference between the [2]
using in two different places. Syntax is something that can be repeated with different meaning. It’s like a "take it to me" and "this is light to me".
In the first line of effective code you are declaring two variables, that is, you are telling the compiler to reserve space for two variables, one that will be called in the code from now on numero
and the other of ent
. Are you saying that the placeholder should be considered the size of the type that supports a int
and that needs exactly 2 memory positions that fit this type in each of the variables, so it is a array, a vector.
Below we use the variable name syntax with the same [2]
, but now also with [0]
, [1]
. Now, if I finished the index 0, and the 1, and the 2, I am accessing 3 different indexes, 3 elements of this array. But I booked 2! Problem there.
If the first element is 0, as mathematics teaches us, the second is 1, to have an element 2 we need to have 3 elements. So the variable statement is wrong, there needs to be [3]
. It may sound weird, but it makes perfect sense.
If I tell you to count to 9 starting with the first known positive integer in math, you count to 10 numbers.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Count there, have 10.
I took advantage of organizing the code better:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
int numero[3], ent[3];
cin >> ent[0];
cin >> ent[1];
cin >> ent[2];
sort(numero, numero + 3);
cout << numero[0] << ", " << numero[1] << ", " << numero[2] << endl ;
cout << ent[0] << ", " << ent[1] << ", " << ent[2];
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
One last note is that you don’t usually use array from C to C++. For this it has better structures: Difference between Std::list, Std::vector and Std:array.
Instead of
numero[2],ent[2];
would not benumero[3], ent[3];
?– NoobSaibot
int array[numero];
thenumero
indicates the number of boxes not which is last. Soint numero[2];
creates an array with 2 houses and not 3.– Isac
It worked only the number of arrays. Thanks to you:)
– Fernando Junior