Consider the following C++ program that accepts 5 numbers from user and displays
ID: 3581707 • Letter: C
Question
Consider the following C++ program that accepts 5 numbers from user and displays them in ascending order. The sample output following the program should be produced if both parts (a) and (b) are implemented correctly. # include using namespace std; void insert { int num[], int n, int count) { //: Your codes for c2(a) should be inserted here } int main () { int num [5] = {}; //array for storing the numbers int count = 0; // how many numbers in the array int n; // for accepting user input cout n; insert { num, n, count}; count ++; } while (countExplanation / Answer
#include <iostream>
using namespace std;
void insert(int num[],int n,int count){
num[count]=n;
}
int main()
{
int num[5]={};
int count=0;
int n;
cout << "Enter 5 distinct numbers: ";
do{
cin>>n;
insert(num,n,count);
count++;
}while(count<5);
cout << "The 5 numbers in ascending order: ";
/* selection sort for sorting the array in ascending order */
int min,loc,temp;
for(int i=0;i<count-1;i++)
{
min=num[i];
loc=i;
for(int j=i+1;j<count;j++)
{
if(min>num[j])
{
min=num[j];
loc=j;
}
}
temp=num[i];
num[i]=num[loc];
num[loc]=temp;
}
for(int i=0;i<count;i++)
cout<<num[i]<<" ";
return 0;
}
/*********OUTPUT********
Enter 5 distinct numbers:
15
6
21
-47
3
The 5 numbers in ascending order:
-47 3 6 15 21
******OUTPUT***********/
/* Note: Please do ask in case of any doubt,Thanks */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.