Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** USING C++ *** Description Write a program that will sort in ascending order

ID: 3838457 • Letter: #

Question

*** USING C++ ***

Description

Write a program that will sort in ascending order a set of decimal data items provided by the user. The user may enter up to a maximum of 20 decimal data items. First, the program will ask the user to enter data item count. Then it will ask the user to enter a data item. It will ask the user to enter a data item repeatedly till all the data items are entered. Then it will display all the data items in the order entered by the user. After that, it will display all the data items sorted in ascending order.

Above, when the user is asked to enter data item count and the user enters a count that is outside the required range, the program will display a message to that effect and exit.

Implementation

Create two arrays data and sdata. Input user data into array data. Copy the contents of array data into sdata. Then sort the array sdata. Then display the contents of array data and sdata.

Use Bubble sort for sorting the array.

Use separate methods for inputting, copying, sorting and displaying. Below are the proto-types for these methods.

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);

Call the input function after inputting and validating the data item count. The function input will input data elements equal to the value of length provided.

x – data array to work on

maxLength – maximum length of array.

length – length of data array to work on

source – source array (array to copy from)

dest – destination array (array to copy to)

Test Data

Run 1

Enter data item count <1-20>

9

Enter data element

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

Run 2

Enter data item count <1-20>

21

Item count is NOT within required range. Required range is 1 to 20.

Bye.

Run 3

Enter data item count <1-20>

-1

Item count is NOT within required range. Required range is 1 to 20.

Bye.

Explanation / Answer

#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<<" Enter 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<<" Original Data: ";
display(data,length);
  
cout<<" Sorted Data: ";
display(sdata,length);
  
   return 0;
}

void input (double x[], int length)
{
int i;
for(i=0;i<length;i++)
{
cout<<" Enter 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