In the following problem you are completing a program that sorts an integer arra
ID: 3676580 • Letter: I
Question
In the following problem you are completing a program that sorts an integer array.
//
// main.cpp
// Exam Program
//
// Created by Roger Chambers.
// Copyright (c) 2015 Roger Chambers. All rights reserved.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void swap(int&, int&); //Will be used to swap two array values
int* createArray(int); //Will be used to generate a random array
void selectSort(int*, int); //A selection sort function
void printArray(int*, int); //Displays the array content
int main(int argc, const char * argv[]) {
int size = 0; //size of the array
//Request the size from the user
cout << "Enter array size: ";
cin >> size;
//Create the array of the user-specified size
int *set = createArray(size);
//Prints the array that was just created
printArray(set, size);
//Alert the user that the array will now be sorted
cout << " The Array will now be sorted ";
selectSort(set, size);
cout << "----Sorted Set---- ";
printArray(set, size);
cout << endl;
return 0;
}
//The selection sort function
void selectSort(int* arr, int size)
{
for (int scan = 0; scan < size; scan++)
{
int low = arr[scan],
lowPos = scan;
for (int run = scan + 1; run < size; run++)
{
if (arr[run] < low)
{
low = arr[run];
lowPos = run;
}
}
swap(arr[lowPos], arr[scan]);
}
}
There are some details that need to be filled in inorder to make this problem. Read carefully the problems carefully to complete this program.
1. The following function definition createArray uses the random generator concept to fill the array.
//This function should creates an array and return the array to the function that called it.
int* createArray(int size)
{
int *s = new int[size]; //Dynamically creates a new array
//Random values generator
srand(static_cast<unsigned int>(time(0)));
//use a for loop to insert values into the array. Values range: 1 - 1000
//-->Write loop under part 1
return s;
}
2. Define the swap function with consideration of the function prototype that requires to parameters that are reference parameters.
//--> Write swap function under part 2
3. Complete the printArray function in part 3.
//Print the array with spaces and then a new line after the the list has been completely printed
void printArray(int* arr, int size)
{
}
Explanation / Answer
//This function creates an array and return the array to the function that called it.
int* createArray(int size)
{
int *s = new int[size]; //Dynamically creates a new array
//Random values generator
srand(static_cast<unsigned int>(time(0)));
//use a for loop to insert values into the array. Values range: 1 - 1000
s[i] = (rand()%1000)+1;
return s;
}
//Print the array with spaces and then a new line after the the list has been completely printed
void printArray(int* arr, int size)
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.