Saltar al contenido

cómo ordenar un array en orden ascendente en el ejemplo de código c ++

Luego de de esta larga compilación de información solucionamos esta impedimento que pueden tener algunos usuarios. Te dejamos la solución y deseamos resultarte de mucha ayuda.

Ejemplo 1: c ++ cómo ordenar números en orden ascendente

// With the first loop we take a number of the array(x). // With the second we take all the numbers in the array except x.// So we can compare each number with all the other numbers(9)int arr[]=9,8,3,5,1,2,0,4,10,6;// System Input.for(int i =0; i <10; i++)// For loop running array lenght times(10).for(int j = i +1; j <10; j++)// For loop running 9 times(other nums).	if(arr[i]> arr[j])// Checking if current num(x) is larger than the next num(j).int temp = arr[i];// Storing current num(x)
				arr[i]= arr[j];// Making i number equal to the next num(j).
				arr[j]= temp;// Making the next number qeual to temp(i).// SWITCHING PLACES WITH THE CURRENT AND THE NEXT NUM.for(int i =0; i <10; i++)// For loop running array lenght times(10).
		cout << arr[i]<< endl;// printing all the numbers!

Ejemplo 2: cómo organizar array en orden ascendente en c ++

#includeusingnamespace std;#defineMAX100intmain()//array declarationint arr[MAX];int n,i,j;int temp;//read total number of elements to read
	cout<<"Enter total number of elements to read: ";
	cin>>n;//check boundif(n<0

Si te ha resultado de provecho nuestro artículo, sería de mucha ayuda si lo compartes con otros juniors de este modo contrubuyes a extender nuestra información.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *