10 Replies - 5262 Views - Last Post: 06 April 2010 - 09:49 AM Rate Topic: -----

#1 Guest_ryan*


Reputation:

returning array from a function

Posted 06 April 2010 - 08:02 AM

I am writing a program for my intro to C++ class that will populate an array with 10,000 random numbers and then ask the user whether they want to use a bubble sort or selection sort. Based on their input I pass the array with the random numbers to the corresponding function. I have this part down. I need to find a way to either return the sorted array directly back to the main function or how to return the sorted values individually back into a new array in main. After this I just need to do a binary search which I can figure out. Any help would be great. Thanks in advance. Here is what I have so far.....

#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;
		}
	}



Is This A Good Question/Topic? 0

Replies To: returning array from a function

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: returning array from a function

Posted 06 April 2010 - 08:07 AM

You could do this a mixture of ways.

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
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: returning array from a function

Posted 06 April 2010 - 08:08 AM

You can't return an array from a function, but you can return a pointer to an array instead. If you haven't learned about pointers, then you have to print the sorted array directly from the function.
@ 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

Was This Post Helpful? 0
  • +
  • -

#4 Guest_ryan*


Reputation:

Re: returning array from a function

Posted 06 April 2010 - 08:15 AM

View Postsarmanu, on 06 April 2010 - 07:08 AM, said:

You can't return an array from a function, but you can return a pointer to an array instead. If you haven't learned about pointers, then you have to print the sorted array directly from the function.
@ 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
Was This Post Helpful? 0

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: returning array from a function

Posted 06 April 2010 - 08:18 AM

Sorry my mistake, I'm not that big of an expert in C++ so wasn't 100% sure If it would work however I did get the pointer bit kind of right!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_ryan*


Reputation:

Re: returning array from a function

Posted 06 April 2010 - 09:02 AM

I read through the chapter on pointers but there is no good explaination on how to use them to return the values from an array back to the original function. Can anybody give me a quick example of passing an array to another function to sort it and then returning it back using pointers?
Was This Post Helpful? 0

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: returning array from a function

Posted 06 April 2010 - 09:23 AM

Array names ARE pointers. You are passing a reference to the array as an argument to the sort functions. The sort routines are sorting the array in place. No need to return anything.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_ryan*


Reputation:

Re: returning array from a function

Posted 06 April 2010 - 09:37 AM

View Postn8wxs, on 06 April 2010 - 08:23 AM, said:

Array names ARE pointers. You are passing a reference to the array as an argument to the sort functions. The sort routines are sorting the array in place. No need to return anything.


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?
Was This Post Helpful? 0

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: returning array from a function

Posted 06 April 2010 - 09:40 AM

Yes.
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: returning array from a function

Posted 06 April 2010 - 09:46 AM

yes. See Arrays

Skip down to the section titled: Arrays as parameters

Also see Pointers skipping down to the section Pointers and arrays

This post has been edited by n8wxs: 06 April 2010 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#11 Guest_ryan*


Reputation:

Re: returning array from a function

Posted 06 April 2010 - 09:49 AM

View Postsarmanu, on 06 April 2010 - 08:40 AM, said:

Yes.


Great! That is exacly what I needed to know. I do not need the original unsorted array. I just needed the sorted one so I could send it to a function to be printed and later down the road a binary search. I wish I knew it was that simple to begin with :D
Was This Post Helpful? 0

Page 1 of 1