The problem, as you have been told, is in the last ordenarArreglo
loop:
for(int z = 0;z <= 10;z++)
{
cout << " " << arr[z];
}
In that loop you are iterating in the range (0.10), when the valid range is (0.9).
The simplest solution is to change <=
by <
:
for(int z = 0;z < 10;z++)
{
cout << " " << arr[z];
}
However, the value 10
repeats everywhere. A good habit is to prevent these types of values from being replicated by the code like rabbits. Imagine that to do tests you prefer to use a low value (for example 10) but then in the real environment you have to use a larger value (it means 500). Are you going to locate all the apparitions to replace them one by one? I hope your answer is not affirmative.
The solution to this problem is to use constant variables. This way of working provides two great advantages:
- It is very easy to change the value. You edit it in one place and all the code adapts to the new value.
- Names are usually more descriptive than numbers. When finding errors it is not the same to find a literal and try to deduce if this value is correct than to find a constant and, based on its name, know if you are using the correct constant.
Example:
const int TamVector= 10;
int ordenarArreglo(int *arr)
{
int i, j, temp = 0;
for(i = 0; i < TamVector - 1; i++) // <<--- AQUI!!!
{
for(j = 0; j<(TamVector - i - 1); j++) // <<--- AQUI!!!
{
// ...
}
}
// ...
for(int z = 0;z < TamVector;z++) // <<--- AQUI!!!
{
cout << " " << arr[z];
}
}
int main()
{
//Arreglo de diez numeros a modificar.
int arr[TamVector] = {15,1,3,4,8,9,7,6,7,4}; // <<--- AQUI!!!
cout << "El arreglo original es: ";
for(int i=0;i < TamVector; i++)
{
cout << " " << arr[i];
}
// ...
}
And would not it be better to use #define
?
The truth is that not since #define
does not have a strong typing, which can cause if you do not take into account that small but crucial detail.
Another silly detail of your code is the initialization of temp
, i
, and j
in the middle of ordenarArreglo
. That code is directly redundant since those variables have no use. I take this paragraph to also tell you that these variables should be declared within the corresponding loop (as you already do in other sections of the code):
for(int i = 0; i < TamVector - 1; i++)
{
for(int j = 0; j<(TamVector - i - 1); j++)
{
if(arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1 ]= temp;
}
}
}
It is highly recommended to reduce the scope of the variables to the minimum to avoid foolish mistakes when programming. Creating a variable int
in each iteration of the loop has no cost and, in addition, the compilers can do wonders when optimizing the code ... while unnecessarily lengthening the life of a variable can lead to rather complicated problems to find out what it happens if you reuse a variable without realizing it and assume that it is initialized to a value that is not what it really has?
Another detail we find here:
temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1 ]= temp;
That can be replaced quietly by this:
std::swap(arr[j],arr[j+1]);
Another problem that your code has is found here:
int ordenarArreglo(int *arr)
{
// ...
// ¿Y el return?
}
If you indicate that the function returns an integer you should necessarily put a return
somewhere (preferably at the end of the function), however this instruction is absent.
This generates an added problem and is that it allows the following statement to be valid:
cout << ordenarArreglo(arr);
Do you really want to print an additional integer after displaying the resulting vector on the screen? I guess not.
In short, the function should have the following signature:
void ordenarArreglo(int *arr)
And in the penultimate instruction of your program there is a cout
:
ordenarArreglo(arr);
return 0; // También puedes poner return EXIT_SUCCESS; que queda más elegante