Write a program to read integers into an array and then sort the array using sel
ID: 3607364 • Letter: W
Question
Write a program to read integers into an array and then sort the array using selection sort, where you find the small est element in the array and exchange it with the first element, and then find the second smallest element and exchange it with the second element, etc., until the entire array is sorted. Have your program include four functions, besides main, which perform the following functions: 1. Read in an undetermined number of array elements 2. Print the array elements (call this function both before and after the elements are sorted) 3. Sort the array elements 4. Swap two elements of an array (this function will be called by your sort function) Which of these four functions should have the array passed in as const? 2 Where should the local prototypes of each these functions be places? Use #define to declare the maximum array size as a constant MAXSIZE with a value of 10. Run your program using the following sets ofinput· int a[moxs1 ; tn 1. 5 12-730 2. 12 34576 3. Enter more values than the declared array size. Display an appropriate error message and sort the first 10 4. Enter an invalid (nonnumeric) character. Display an appropriate error message and flush the bad data and p..nt?tab le C9,n) values anyway continue reading until end-of-file or the array is fullExplanation / Answer
Other than main,
The four methods are:
void swap(int *xp, int *yp)
void normalSort(int arr[], int n , bool asc)
void printArray(int arr[], int size)
void inputArray(int arr[])
********************************************************************************************************************************
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include<string.h>
#define MAXSIZE 10
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void normalSort(int arr[], int n , bool asc){
int i, j, min_i;
for (i = 0; i < n-1; i++)
{
min_i = i;
for (j = i+1; j < n; j++)
{
if(asc==true){
if (arr[j] < arr[min_i])
min_i = j;
}
else{
if (arr[j] > arr[min_i])
min_i = j;
}
}
swap(&arr[min_i], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf(" ");
}
void inputArray(int arr[])
{
int i;
for (i=0; i < MAXSIZE; i++)
cin >> arr[i];
}
int main()
{
cout << "Enter " << MAXSIZE << " elements:" << endl;
int *arr= new int[MAXSIZE];
inputArray(arr);
cout << "Before sorting: ";
printArray(arr,MAXSIZE);
normalSort(arr,MAXSIZE,true);//True means ascending order false is descending
cout << "After sorting: ";
printArray(arr,MAXSIZE);
return 0;
}
********************************************************************************************************************************
Sample outputs:
****************************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.