Respuesta :
The c++ program for the given problem is shown below along with the explanation of each step.
#include <iostream>
using namespace std;
int main() {
int arr[20];
int count = 0, num, swap;
for(int i=0; i<20; i++)
{
arr[i] = 0;
}
cout<<"Enter the integers to be sorted in ascending order. Enter 0 to stop entering the integers. " <<endl;
for(int i=0; i<20; i++)
{
cin>>num;
if(num != 0)
{
arr[i] = num;
count++;
}
else
{
cout<<"The numbers entered have been recorded. "<<endl;
break;
}
}
for(int i=0; i<20; i++)
{
for(int j=i+1; j<20; j++)
{
if(arr[i] != 0)
{
if(arr[i] > arr[j])
{
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
else
continue;
}
else
break;
}
}
cout<<"Total number of integers entered is "<<count<<endl;
cout<<"The numbers in ascending order are "<<endl;
for(int i=0; i<20; i++)
{
if(arr[i] != 0)
cout<<arr[i]<<endl;
}
}
1. We use an integer array to hold the integers entered by the user. Hence, we declare integer variables and array of size 20. The variables and array is initialized to 0.
int arr[20];
int count = 0, num, swap;
Array initialization is done to ease the sorting and displaying of the elements. User enters 0 only to quit, hence, elements having value 0 indicates null values.
for(int i=0; i<20; i++)
{
arr[i] = 0;
}
2. The integers entered by the user are put in the array and counted simultaneously.
for(int i=0; i<20; i++)
{
cin>>num;
if(num != 0)
{
arr[i] = num;
count++;
}
else
{
cout<<"The numbers entered have been recorded. "<<endl;
break;
}
}
3. The integers in the array are sorted in ascending order using a third variable swap, for swapping the integers.
for(int i=0; i<20; i++)
{
for(int j=i+1; j<20; j++)
{
if(arr[i] != 0)
{
if(arr[i] > arr[j])
{
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
else
continue;
}
else
break;
}
}
4. Lastly, the number of integers entered and the integers in ascending order the displayed.
for(int i=0; i<20; i++)
{
if(arr[i] != 0)
cout<<arr[i]<<endl;
}
Answer:
integers=[]
while True:
number=int(input())
if number<0:
break
integers.append(number)
print("",min(integers))
print("",max(integers))
Explanation: