I am stuck on a problem. I am given code and I am suppose to do the follwong sway fucntion.
The last three lines of code in the function selection_sort perform an essential operation that is commonly found in sort algorithms. Replace those three statements with a call to a function named swap that takes exactly three arguments (the array name and the two subscripts of the array whose values are to be swapped). The swap function should not return any value. Add both the prototype and the definition of swap to the program.
I am confused as to what I am being asked. Am I even close to a solution. I complied the program and I got
This is the code
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <time.h>
#include <stdlib.h>
// (Use the preprocessor to replace every occurrence of NUM_COLS by 5
// and every occurrence of MAX_SIZE by 23.)
//
#define NUM_COLS 5
#define MAX_SIZE 23
void print_array (int [], int, int);
void selection_sort (int[], int);
void swap(int [], int& [], int&[]);
void main()
{
int numbers[MAX_SIZE];
int loc;
srand(time(0)); // Initialize the random number generator
for(loc = 0; loc < MAX_SIZE; loc++)
numbers[loc] = rand() % 100 + 1;
cout << "The initial array of random numbers:" << endl;
print_array(numbers, MAX_SIZE, NUM_COLS);
selection_sort (numbers, MAX_SIZE);
cout << "\n\nThe sorted array of random numbers:" << endl;
print_array(numbers, MAX_SIZE, NUM_COLS);
swap(numbers,NUM_COLS, MAX_SIZE);
}
// Print an array of n integers, k columns of numbers per line.
//
void print_array(int a[], int n, int k)
{
int i, c;
for (i = 0; i < n; i += k) {
for (c = 0; c < k && i + c < n; c++)
cout << setw(5) << a[i + c];
cout << endl;
}
}
// Sort an array of n integers into ascending order, using
// selection sort.
//
void selection_sort(int a[], int n)
{
int i, j, smallest;
int temp;
for (i = 0; i < n-1; i++) {
smallest = i;
for (j = i + 1; j < n; j++) {
if (a[j] < a[smallest])
smallest = j;
}
int i, smallest;
int temp = a[i];
a[i] = a[smallest];
a[smallest] = temp;
}
void swap(int& [], int& [], int&[])
{
}
Here is the compile errors
1>------ Rebuild All started: Project: lab10_a, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'lab10_a', configuration 'Debug|Win32'
1>Compiling...
1>Lab10a.cpp
1>.\Lab10a.cpp(36) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(36) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(43) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>.\Lab10a.cpp(53) : error C2664: 'swap' : cannot convert parameter 2 from 'int' to 'int *[]'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(89) : error C2601: 'swap' : local function definitions are illegal
1> .\Lab10a.cpp(72): this line contains a '{' which has not yet been matched
1>.\Lab10a.cpp(91) : fatal error C1075: end of file found before the left brace '{' at '.\Lab10a.cpp(72)' was matched
1>Build log was saved at "file://c:\Users\moporho\Documents\HO8\lab10_a\Debug\BuildLog.htm"
1>lab10_a - 8 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========