– Program should have a user-defined sort() function that • Takes an array and i
ID: 3638246 • Letter: #
Question
– Program should have a user-defined sort() functionthat
• Takes an array and its size as input parameters
• Sorts the array from smallest to largest
– Program can optionally use a swap function if needed
Please show the source code about this program
below is an explanation for an assignment which instructor gave us .
please follow the explanation to make program as much as possible.
#include "stdafx.h"
#include <iostream>
using namespace std;
/*
Description: swaps values of two cells in the array
Parameters:
dataArray: array of double values
i: array index for the first value
j: array index for the second value
Return Value: None
*/
void swap(double dataArray[], int i, int j)
{
// copy value at cell i to a new variable
// copy value at cell j to cell i
// copy value from new variable to cell j
}
/*
Description: sorts the array values from lowest to highest
Parameters:
dataArray: array of double values
arraySize: number of values in the array
Return Value: None
*/
void sort(double dataArray[], int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
for (int j = i + 1; j < arraySize; j++)
{
// if array value at j is less then array value at i then
// swap the values at two locations using the swap function
// declared above
}
}
}
/*
Description: Main entry-point. Asks users to enter 10 values.
Sorts the values and displays results
Parameters: None
Return Value: 0
*/
int main()
{
char key;
do
{
const int ARRAY_SIZE = 10;
double dataArray[ARRAY_SIZE];
cout << "Please Enter 10 numbers to sort: " << endl;
for (int i = 0; i < ARRAY_SIZE; i++)
{
cin >> dataArray[i];
}
sort(dataArray, ARRAY_SIZE);
cout << "-- Sorted Array --" << endl;
for (int i = 0; i < ARRAY_SIZE; i++)
{
cout << dataArray[i] << endl;
}
cout << "Press any key to continue (or Q to quit)" << endl;
cin >> key;
} while (key != 'Q');
return 0;
}
Explanation / Answer
#include #include #include #include using namespace std; void swap(int* a,int* b) { float c; //coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.