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

im not looking for an exact answer, just how to do it! if you do give an answer

ID: 3548336 • Letter: I

Question

im not looking for an exact answer, just how to do it! if you do give an answer please explain how you did it.


Define the following functions in order for this program to run to completion.


Consider the following main function and prototypes:

void copyArray(int[], int[], int);
bool isEqual(int[], int[], int);
void selectionSort(int[], int);

int main()
{
   const int SIZE = 50;
   int values[SIZE];

   srand(time(0));

   for(int index = 0; index < SIZE; index++)
      values[index] = 1 + rand() % 1000;

   int valuesCopy[SIZE];

   copyArray(valuesCopy, values, SIZE);

   if (isEqual(values, valuesCopy, SIZE))
      cout <<

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
// function to copy the array
void copyArray(int[], int[], int);
// function to check whether two arrays equal or nto
bool isEqual(int[], int[], int);
// function to sort arrays.
void selectionSort(int[], int);

int main()
{
const int SIZE = 50;
int values[SIZE];
srand(time(0));
for(int index = 0; index < SIZE; index++)
values[index] = 1 + rand() % 1000;
int valuesCopy[SIZE];
copyArray(valuesCopy, values, SIZE);
if (isEqual(values, valuesCopy, SIZE))
cout << "You have successfully copied the array ";
else
cout << "The array was not properly copied ";
cout << "Here is the unsorted array ";
for (int index = 0; index < SIZE; index++)
cout << values[index] << " ";
selectionSort(valuesCopy, SIZE);
cout << " Here is the sorted array ";
for (int index = 0; index < SIZE; index++)
cout << valuesCopy[index] << " ";
cout << endl;
return 0;
}

// function to copy the array
void copyArray(int new_array[], int old_array[], int size)
{
// start copying values one by one from old array to new array
for(int i=0; i<size; i++)
new_array[i] = old_array[i];
}
// function to check whether two arrays equal or nto
bool isEqual(int new_array[], int old_array[], int size)
{
// intially assume that arrays are same:
bool is_equal = true;
// start comparing one by one and return the result;
for(int i=0; i<size; i++)
{
// if values are not same at same index then change is_equal to false.
if(new_array[i] != old_array[i])
{
is_equal = false;
// we already found array is false so break the for loop.
break;
}
}
return is_equal;
}
// function to sort arrays.
void selectionSort(int array[], int size)
{
int toFill,smallPos,pos,temp;
//STEP 1: repeat each position toFill from 0 to size-2
for(toFill=0; toFill<=size-2; toFill++)
{
//STEP 2.1:intialize smallPos to toFill
// assuming that first one is small.
smallPos = toFill;
// STEP 2.2: repeat pos ranging from toFill to size-1
for(pos = toFill; pos<=size-1; pos++)
{
//STEP 2.3: if array[pos] is smaller than array[smallPos]
if(array[pos]<array[smallPos])
//STEP 2.4 copy pos to smallPos
smallPos = pos;
} // end for
// STEP 3: if smallPos not equal to toFill swap list[smallPos] to list[toFill]
if(smallPos!=toFill)
{
temp = array[smallPos];
array[smallPos] = array[toFill];
array[toFill] = temp;
}
} //end for
}