I don't know what the problem is, I've been trying for a while now and I don't know how to solve it, here's the code, it's for a college exercise but honestly I don't understand anything, and I'm adding this only because the page asks me hahahahaha, I don't know what more detail to give lpm ahr
void Ordenar(int n, int vec[])
{
for(int k=0;k<n;k++)
{
for(int f=0;f<n-k;f++)
{
if (vec[f]>vec[f+1])
{
int aux;
aux=vec[f];
vec[f]=vec[f+1];
vec[f+1]=aux;
}
}
}
for (int i=0; i<n; i++)
{
cout << "\nEl valor de la posicion " << i << " es " << vec[i] << endl;
}
}
int main()
{
int n;
int vec[n];
cout << "----------------------------------------" << endl;
cout << "¿Cuantas posiciones tendra el vector? ";
cin >> n;
cout << "----------------------------------------\n" << endl;
for (int i=0; i<=n; i++)
{
cout << "Ingrese un valor para la posicion " << i << ":" << endl;
cin >> vec[n];
}
cout << Ordenar(n, vec[n]) << endl;
return 0;
}
I see several things that may be subject to failure.
vec
, you give it a size ofn
. But at that point in the program, the variablen
has no value, is not initialized or assigned, so the size of the array is unpredictable. You should create the arrayvec[n]
once the value of is knownn
.vec
... Notice that you are completing an array with the variablen
, when in fact itn
is fixed and the variable that runs through the array isi
, so you must enter the values invec[i]
.cout << Ordenar(n, vec[n]) << endl;
, inside the functionOrdenar
you are already printing the values inside the function call, so you would not need to output the results of the sort function to the terminal.With these things, the code would look like this:
All the best.