#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//Prototypes to follow.
int populateArray (); //Function to populate the array with 10,000 random numbers.
int bubbleSort (int [], int); //Function used if bubble sort is selected by the user.
int selectionSort (int [], int); //Function used if selection sort is selected by the user.
void printArray (); //Function used to print out what is in the array.
int whichNumber (); //Function to prompt the user for which number they would like to search for.
int searchArray (int); //Function to use a binary search to look for the input set by the user.
void main ()
{
const int ARRAY_SIZE = 10000;
int selection; //Variable for the selection the user is making for how the array is to be sorted.
int originalArray [ARRAY_SIZE] = {0};
int sortedArray [ARRAY_SIZE] = {0};
srand(time(0)); //Seeds the random number generator.
cout << "This program will generate a list of 10,000 random numbers" << endl; //Program explaination for the user.
cout << "and allow you to select which method of sorting you would" << endl;
cout << "like to be done to the list. The list will be printed. " << endl;
cout << "You will then input a number to be searched for in the list" << endl;
cout << "using a binary search and the results will be returned." << endl << endl;
for (int i = 0; i < ARRAY_SIZE; i++) //Loop that calls the populatearray function 10,000 times to fill with random numbers.
{
originalArray[i] = populateArray(); //Each element of the array is defined with a number as it is incremented.
}
cout << "How would you like to sort the list? " << endl << endl;
cout << "Press 1 for bubble sort or 2 for selection sort. "; //Prompts user for how the numbers in the array are to be sorted.
cin >> selection; //User input for their selection.
cout << endl;
while (selection < 1 || selection > 2) //Input validation.
{
cout << "Please choose 1 or 2. ";
cin >> selection;
cout << endl;
}
if (selection == 1)
bubbleSort (originalArray, ARRAY_SIZE);
else
selectionSort (originalArray, ARRAY_SIZE);
cout << endl;
system("pause");
}
int populateArray () //Function that creates random numbers to fill the array.
{
int randomNumber; //Variable to hold the value of the random number generated.
randomNumber = rand(); //Generates the random number.
return randomNumber; //Returns the random number that was generated back to main.
}
int bubbleSort (int originalArray[], int ARRAY_SIZE)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (ARRAY_SIZE - 1); count++)
{
if (originalArray[count] > originalArray[count + 1])
{
temp = originalArray[count];
originalArray[count] = originalArray[count +1];
originalArray[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
int selectionSort (int originalArray[], int ARRAY_SIZE)
{
int startScan;
int minIndex;
int minValue;
for (startScan = 0; startScan < (ARRAY_SIZE - 1); startScan++)
{
minIndex = startScan;
minValue = originalArray[startScan];
for (int index = startScan + 1; index < ARRAY_SIZE; index++)
{
if (originalArray[index] < minValue)
{
minValue = originalArray[index];
minIndex = index;
}
}
originalArray[minIndex] = originalArray[startScan];
originalArray[startScan] = minValue;
}
}
returning array from a function
Page 1 of 110 Replies - 2335 Views - Last Post: 06 April 2010 - 09:49 AM
#1 Guest_ryan*
returning array from a function
Posted 06 April 2010 - 08:02 AM
Replies To: returning array from a function
#2
Re: returning array from a function
Posted 06 April 2010 - 08:07 AM
You could pass a pointer to the sorted array when you call the sort function
or you could go for the simpler approach and return an array:
int[] myFunction()
{
// use return to return the array
}
Hope that helps
#3
Re: returning array from a function
Posted 06 April 2010 - 08:08 AM
@ danny_kay1710 - that's not valid C++ syntax. This isn't Java ...
This post has been edited by sarmanu: 06 April 2010 - 08:11 AM
#4 Guest_ryan*
Re: returning array from a function
Posted 06 April 2010 - 08:15 AM
sarmanu, on 06 April 2010 - 07:08 AM, said:
@ danny_kay1710 - that's not valid C++ syntax.
Ok. Thanks sarmanu. I was pretty sure that you could not return an array directly. I haven't learned about pointers in class but I will do some reading and see if I can get it. Thanks
#5
Re: returning array from a function
Posted 06 April 2010 - 08:18 AM
#6 Guest_ryan*
Re: returning array from a function
Posted 06 April 2010 - 09:02 AM
#7
Re: returning array from a function
Posted 06 April 2010 - 09:23 AM
#8 Guest_ryan*
Re: returning array from a function
Posted 06 April 2010 - 09:37 AM
n8wxs, on 06 April 2010 - 08:23 AM, said:
hmmmm. So the original array in the main function will be sorted (after running the sort function) even without directly returning it from the sort functions?
#10
Re: returning array from a function
Posted 06 April 2010 - 09:46 AM
#11 Guest_ryan*
Re: returning array from a function
Posted 06 April 2010 - 09:49 AM
|
|

New Topic/Question
Reply
MultiQuote








|