Sort in C++ does not show the correct input value

Asked

Viewed 142 times

4

I made a code to read 3 values, and with the sort() it sorts the values in ascending order and just below shows the input values. For example:-14 21 7 it sorts right but the output shows -14 7 21 // 21 21 7 where it was supposed to be -14 7 21.

#include <iostream>
#include<algorithm>

using namespace std;

int numero[2],ent[2];

int main(){

    cin>>ent[0];
    cin>>ent[1];
    cin>>ent[2];
    numero[0]=ent[0];
    numero[1]=ent[1];
    numero[2]=ent[2];

    sort(numero,numero+3);


    cout<<numero[0]<<endl<<numero[1]<<endl<<numero[2]<<endl;
    cout<<endl<<ent[0]<<endl<<ent[1]<<endl<<ent[2]<<endl;

    return 0;
}
  • 2

    Instead of numero[2],ent[2]; would not be numero[3], ent[3]; ?

  • 2

    int array[numero]; the numero indicates the number of boxes not which is last. So int numero[2]; creates an array with 2 houses and not 3.

  • It worked only the number of arrays. Thanks to you:)

1 answer

5


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.

Browser other questions tagged

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