Struggling with arrays and for loops

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";

if(response == 'y' || response =='Y')
showArray (data, numberUsed);

return 0;
}

/***** 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;
}

void swapValues(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}

void generateArray (int a[], int size, int& numberGenerated)
{
int i;
char response;

cout << "How many random integers do you want to generate?: ";
cin >> numberGenerated;

if(numberGenerated >= size)
numberGenerated = size;

cout <<"We have generated " << numberGenerated << " random integers. \n"
<<"Would you like to see the generated array? (y/n): ";
cin >>response;

if(response == 'y' || response =='Y')
cout <<"The array generated is: \n";

srand((unsigned int)time(0));
for(i = 0; i <= (numberGenerated-1); i++)
{
a[i] = rand();
}
}

void showArray (const int a[], int numberUsed)
{
for (int i = 0; i < numberUsed; i++)
cout << a[i] << "\t";
cout <<endl;
}
There's a bunch of issues with missing braces throughout your code. Stuff like if statements and for loops not having braces after them.

I'd suggest sorting all of those out first.
Thank you for the help, I think I figured it out.
Topic archived. No new replies allowed.