have a program in which I used arrays in functions. It wants me to enter up to a 100000 random numbers and sort them. When I run my program it keeps looping and I'm not sure why. Can anyone please look at the program and help me, thanks.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
/**************************** function prototypes ******************************/
//performs selection sort
void selectionSort(int a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
void swapValues(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. References array elements have values.
//Returns the index i such that a[i] is the smallest of the values
//a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].
void generateArray(int a[], int size, int& numberGenerated);
void showArray(const int a[], int numberUsed);
const int SIZE = 100000;
/****************************** main function ********************************/
int main()
{
int data[SIZE], numberUsed = 0;
char response;
generateArray(data, SIZE, numberUsed);
showArray(data, numberUsed);
long beforeSort = (long) time(0);
selectionSort (data, numberUsed);
long afterSort= (long) time(0);
cout <<"Sorting the array using selection sort...\n"
<<"Time taken to sort " <<numberUsed <<" numbers is " <<afterSort - beforeSort <<endl;
cout <<"seconds. " <<"Would you like to see the sorted array? (y/n): ";
cin >>response;
cout <<"The array after being sorted is: \n";
/***** supplied function definitions (Do not modify codes below this line) *****/
void selectionSort(int a[], int numberUsed)
{
int indexOfNextSmallest;
for (int start = 0; start < numberUsed - 1; start++)
{ //start is the starting index of the unsorted segment
indexOfNextSmallest = indexOfSmallest(a, start, numberUsed);
swapValues(a[start], a[indexOfNextSmallest]);
}
}
int indexOfSmallest(const int a[], int startIndex, int numberUsed)
{
int min = a[startIndex],
indexOfMin = startIndex;
for (int index = startIndex + 1; index < numberUsed; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
//min is the smallest of a[startIndex] through a[index]
}
return indexOfMin;
}