In: Computer Science
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only one .cpp file
#include <iostream>
using namespace std;
//prototypes
void input (double x[], int length);
void copy (double source[], double dest[], int length);
void sort (double x [], int length);
void display (double x[], int length);
int main()
{
double data[20],sdata[20];
int length;
cout<<"\nEnter data item count
<1-20>";
cin>>length;
if(length <0 || length > 20)//validate length
cout<<"Item count is NOT within required range. Required
range is 1 to 20.";
input(data,length);
copy(data,sdata,length);
sort(sdata,length);
cout<<"\nOriginal Data: ";
display(data,length);
cout<<"\nSorted Data: ";
display(sdata,length);
return 0;
}
void input (double x[], int length)
{
int i;
for(i=0;i<length;i++)
{
cout<<"\nEnter score";
cin>>x[i]; //input score
}
}
void copy (double source[], double dest[], int length)
{
int i;
for(i=0;i<length;i++)
{
dest[i] = source[i]; //copy array elements
}
}
void sort (double x[], int length) //bubble sort
{
double temp;
int i,j;
for(i=0;i<length;i++)
{
for(j=0;j<length-i-1;j++)
{
if(x[j]> x[j+1]) //swap if not in ascending order
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
}
void display (double x[] ,int length)
{
int i;
for(i=0;i<length;i++)
{
cout<<x[i]<<" ";
}
}
output:
Enter data item count <1-20> 9
Enter score 5.5
Enter score 3.3
Enter score 4.4
Enter score 2.2
Enter score 9.9
Enter score 6.6
Enter score 8.8
Enter score 7.7
Enter score 1.1
Original Data: 5.5 3.3 4.4 2.2 9.9 6.6 8.8 7.7 1.1
Sorted Data: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
Do ask if any doubt.