GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2005, 22:11
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Finding Minimum Value in Recursive Arrays


I have an assignment that asks me to find the minimum value in an array.

Here are the instructions given by my instructor:
Write a C++ program having a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.

I got the program to sort the elements from smallest to largest, but I believe my code contains logical errors. I'm having trouble with the latter part of the assignment. The part that says... "The function should stop processing and return when it receives an array of 1 element". Can someone help me with this situation?

Here is my written code:
CPP / C++ / C Code:
 
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;


int main ()
{


void recursiveMinimum (int);	
	
	
	const int arraySize = 13; // size of array a
	int a[ arraySize ] = { 10, 55, 77, 3, 8, 54, 22, 11, 16, 18, 100, 25, 29 };
	int hold; // temporary location used to swap array elements
	int e;

	cout << "Data items in original order:\n";

	// output original array
	for ( int i=0; i < arraySize; i++ )
		cout << setw(4) << a[i];

	// bubble sort
	// loop to control number of passes
	for ( int pass = 0; pass < arraySize - 1; pass++ )
			
		// loop to control number of comparisons per pass
		for ( int j = 0; j < arraySize - 1; j++ )

			// compare side-by-side elements and swap them if 
			// first element is greater than second element
			if ( a[j] > a[j + 1] ) {
				hold = a [j];
				a [j] = a [j + 1];
				a [j +1] = hold;

			} // end if

		
		cout << endl << "The data items in sorted order is: \n";
		// output sorted array
		for (int k=0; k < arraySize; k++ )
			cout << setw(4) << a[k];

				
		recursiveMinimum ( a[0]);
		cout << endl << endl << "The smallest element of the array is: ";
		cout << a[0] << endl << endl;

				
		
		return 0;	// indicates sucessful termination
}

void recursiveMinimum (int e)
{
    
	 e *= 1;

} // end main 
  #2  
Old 16-Nov-2005, 23:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Finding Minimum Value in Recursive Arrays


Quote:
Originally Posted by thulaotzu
Here are the instructions given by my instructor:
Write a C++ program having a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.

I got the program to sort the elements from smallest to largest, but I believe my code contains logical errors.
Your logic problem is sorting the array. Nowhere in the instructions does it say sort. You need to simply create a function that recursively tests the array elements and returns the lowest value.

Based on my interpretation of the instructions, the function accepts the array and a size. Use the size as an index into the array to test the last element. You can then subtract one from the size and call the function again. When the size gets to one, you start the return phase of the recursion...
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 17-Nov-2005, 04:55
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Re: Finding Minimum Value in Recursive Arrays


Do you think I shoud ask the user to input numbers?

For example,

CPP / C++ / C Code:
 int numbers [20];

cout << "Enter 20 numbers";
cin >> numbers; 

Is that how we should approach the program?.
  #4  
Old 17-Nov-2005, 12:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Finding Minimum Value in Recursive Arrays


Quote:
Originally Posted by thulaotzu
Do you think I shoud ask the user to input numbers?
Has no bearing on the problem. Get the information in the array any way you want. At first
CPP / C++ / C Code:
int numbers[6] = { 23, 41, 2, 14, 56, 8}; 
would be adequate.
Once you have the program running, switch to input.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 17-Nov-2005, 14:35
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Finding Minimum Value in Recursive Arrays


Please notice that you where given a clue about the function prototype ...
The function gets the array and the array size and returns the minimal value. You used void function, so you do not comply to all of the instructions.

try something like :
CPP / C++ / C Code:
int recursiveMinimum(int my_array[],unsigned array_size);

And keep in mind that your function should stop when the array size is 1 (still, you need to check that last argument - don't forget it), so your recursive calls will decrement the array size value along your algorithm.

Keep us posted,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #6  
Old 18-Nov-2005, 01:06
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Re: Finding Minimum Value in Recursive Arrays


In the function:
CPP / C++ / C Code:
int recursiveMinimum(int my_array[],unsigned array_size);

Why is array_size "unsigned"?.. Can someone explain why it's unsigned?

Also, how can I compare the elements so that I can display the smallest element of the array?
  #7  
Old 18-Nov-2005, 01:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Finding Minimum Value in Recursive Arrays


Quote:
Originally Posted by thulaotzu
In the function:
CPP / C++ / C Code:
int recursiveMinimum(int my_array[],unsigned array_size);

Why is array_size "unsigned"?.. Can someone explain why it's unsigned?
Short answer -- because that's how Kobi defined it.
Long answer -- Why not? Can the size of an array be negative? It doesn't have to be unsigned, but it doesn't hurt.

Quote:
Originally Posted by thulaotzu
Also, how can I compare the elements so that I can display the smallest element of the array?
Essentially, as the instructions state, you return when the array size becomes 1. You return the current array element (which is the 1st element). This becomes the initial guess at the minimum value.

This continues the previous instance of the function (array size == 2). Compare the returned value with the current (2nd) element. return the lower of the two values.

Continue....
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #8  
Old 18-Nov-2005, 02:21
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Finding Minimum Value in Recursive Arrays


Quote:
Originally Posted by thulaotzu
Why is array_size "unsigned"?.. Can someone explain why it's unsigned?

1. Because the index of an array >=0.
If you get used to passing the "logical" argument, then you may be warned by the compiler about possible logical errors (I'm using VC6 with warning level 4).
2. Because I think this is the correct way to define the requested function.

About your other question : Comparing the elements. I see Walt answered you and I can only suggest you look at recursion examples.
Post the code you wrote untill now.

Best regards,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #9  
Old 18-Nov-2005, 07:39
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Re: Finding Minimum Value in Recursive Arrays


I'm almost at the point where I can begin the program.. I'm still stuck on something. For example, the following..

Quote:
Originally Posted by WaltP
Has no bearing on the problem. Get the information in the array any way you want. At first
CPP / C++ / C Code:
int numbers[6] = { 23, 41, 2, 14, 56, 8}; 
would be adequate.
Once you have the program running, switch to input.

I'm not sure how to "switch to input".. Can someone show me how I would do this?

Also, how many for loops would I need in this program?
  #10  
Old 18-Nov-2005, 07:47
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Finding Minimum Value in Recursive Arrays


Thu, you misunderstood what Walt meant.
He meant that, for testing, go with the sample array with size 6.

Once you have successfully created the recursion function, and verified the results, you can get the input from the user.

So, dont worry about input for now...

Quote:
Originally Posted by Thu
Also, how many for loops would I need in this program?
That depends on how you write the program. This is irrelevant.

Anyway, Here is how you get the input for the array: (go with the sample array for testing...)

For that, get the the input from the user about what is the size of the array.
then create the array using the new operator.
Get the input using a loop and a cin statement.

Here is one example:
CPP / C++ / C Code:
int size;

cout<<"Enter the size of the array..";
cin >> size;

int numbers[] = new int[size];

cout<<"Enter the numbers one by one.." << endl;

for( int i = 0; i < size; i++ )
{
   cin >> numbers[i];
}


After doing this, the numbers will be stored in the array.


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob question on c arrays and functions brett C Programming Language 1 20-Apr-2005 04:59
Major problem with recursive function, help.. kakamuti C Programming Language 4 19-Dec-2004 08:47
Knight tour (arrays help needed) dilmv C++ Forum 7 18-Oct-2004 15:31
Using an array and finding the element number (subscript) tommy69 C Programming Language 27 05-Apr-2004 13:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:15.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.