anyone , help.

7.37 (Find the Minimum Value in an Array) Write a recursive function recursiveMinimum that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element of the array. The function should stop processing and return when the starting subscript equals the ending subscript.


CAN ANYONE HELP ME TO FIX THIS CODE PLEASE , I'M NOT EVEN BEING ABLE TO EXECUTE IT "Debug"

this is what I have so far .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAXRANGE = 1000;
int recursiveMinimum( const int [], int, int );

int main()
{
	const int SIZE = 10;
	int array [ SIZE ];
	int smallest;

	srand( time( 0 ) );
	
	// initialize elements of array to random numbers
	for ( int loop = 0; loop < SIZE; loop++ )

		array[ loop ] = 1 + rand() % MAXRANGE;

	// display array
	cout << "Array members are:\n";

	for ( int k = 0; k < SIZE; k++ )
		cout << setw( 5 ) << array[ k ];

	// find and display smallest array element
	cout << '\n';
	smallest = recursiveMinimum( array, 0, SIZE - 1 );
	cout << "\nSmallest element is: " << smallest << endl;
  } // end main

  // function to recursively find minimum array element
  int recursiveMinimim( const int array[], int low, int high )
  {
	  static int smallest = MAXRANGE;

	  // if first element of array is smallest so far
	  // set smallest equal to that element
	  if ( array[ low] < smallest )
		  smallest = array[ low ];

	  // if only one element in array; return smallest
	  // else recursively call recursiveMinimum with new subarray
	  return low == high ?
         smallest : recursiveMinimum( array, low + 1, high );

  } // end function recursiveMinimum 

Why you cannot execute it? What is the error?
closed account (j3Rz8vqX)
recursiveMinimum != recursiveMinimim
1
2
3
4
5
6
7
8
//Prototype:
int recursiveMinimum( const int [], int, int );

//Calling:
smallest = recursiveMinimum( array, 0, SIZE - 1 );

//Definition:
int recursiveMinimim( const int array[], int low, int high )

Spelling error.
Still not working :-(

I don't know what else to do :-(
keskiverto wrote:
Why you cannot execute it? What is the error?


Edit: Compiles, and works fine for me. It says the smallest element is 316.
My guess is that the console is closing on you, see this: http://www.cplusplus.com/forum/beginner/1988/

In case by "not working" you mean it still isn't compiling, your compiler *might* be complaining that you don't return 0; in your main function.
Last edited on
can you post what you have ? I'm new to C++

having a hard time to figure it out .
the smallest element is supposed to be 72 .
srand( time( 0 ) ); the smallest element will be different each time?
Oh right it would be different each time, saying 316 doesn't help much I guess.

can you post what you have ? I'm new to C++

You need to explain exactly what's wrong. Is the code not compiling, or does the program immediately close when you run it? Is there an executable file of your program somewhere?

Anyway, my program prints on console:
1
2
3
4
Array members are:
  455  486  368  260  121  626  534  714  477  366

Smallest element is: 121
Last edited on
Here is the answer I'm trying to get .

1
2
3
Array members are:
309 893 72 270 109 830 338 487 240 505
Smallest element is: 72
THE CODE IS NOT COMPILING . . . and I'm not being able to fix it . :-(
What's the error that your compiler is saying? Are you using an IDE (like Dev-C++ or CodeBlocks, Visual Studio, etc) or just a shell?
Also, you have a time seed for your rand() function, so it's going to be different each time.
Last edited on
Visual Studio .

it says : There were build errors. would like to continue and run the last successful build ? after yes : unable to start program , the system cannot find file specified.
Try cleaning the project. If all else fails, just copy the code into a new project.
Is there an "error list" somewhere? Maybe try reading this. http://social.msdn.microsoft.com/Forums/vstudio/en-US/684f5f63-5a53-412b-ab65-1ff7886a31db/c-compiler?forum=vsdebug
Can't help more, I don't use VS.
Last edited on
closed account (j3Rz8vqX)
There were build errors. would like to continue and run the last successful build ?
That isn't important, we need the warnings/errors.
Last edited on
closed account (z05DSL3A)
juliocesarnyc wrote:
it says : There were build errors. would like to continue and run the last successful build ? after yes : unable to start program , the system cannot find file specified.
There really is no point to answering yes to that question. If the code you are trying to build fails to build, say no and fix the errors.

For the error, do a spot the difference on the signature for lines 8 and 36...Somehow managed to miss a big chunk of this thread...
Last edited on
The problem is that I don't know where the errors are, meaning I don't know how to fix it . new to C++ here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAXRANGE = 1000;
int recursiveMinimum( const int [], int, int );

int main()
{
	const int SIZE = 10;
	int array [ SIZE ];
	int smallest;

	srand( time( 0 ) );
	
	// initialize elements of array to random numbers
	for ( int loop = 0; loop < SIZE; loop++ )

		array[ loop ] = 1 + rand() % MAXRANGE;

	// display array
	cout << "Array members are:\n";

	for ( int k = 0; k < SIZE; k++ )
		cout << setw( 5 ) << array[ k ];

	// find and display smallest array element
	cout << '\n';
	smallest = recursiveMinimum( array, 0, SIZE - 1 );
	cout << "\nSmallest element is: " << smallest << endl;
  } // end main

  // function to recursively find minimum array element
  int recursiveMinimim( const int array[], int low, int high )
  {
	  static int smallest = MAXRANGE;

	  // if first element of array is smallest so far
	  // set smallest equal to that element
	  if ( array[ low] < smallest )
		  smallest = array[ low ];

	  // if only one element in array; return smallest
	  // else recursively call recursiveMinimum with new subarray
	  return low == high ?
         smallest : recursiveMinimum( array, low + 1, high );

  } // end function recursiveMinimum  



Edit & Run
closed account (j3Rz8vqX)
You didn't apply the correction.

Change line 36 to the correct name.
recursiveMinimum != recursiveMinimim.

Change line 36's name to recursiveMinimum as the third post suggested!
http://www.cplusplus.com/forum/beginner/136597/#msg726503
Thanks all , I appreciate all the help.

everything is working . :-) .
everything is working

I'm not so sure about that.

You had a chance to learn to find the error messages given by your build tools (a linker error in this case) and to read them in a way that leads you to the source of error. You have not shown that you did learn.

You did not even read the comments posted to this thread carefully enough; Dput had to repeat his post.


PS. Your "recursive" algorithm might yield the expected result, but effectively it does merely iterate. You should think for other approaches (for educational purposes).
Topic archived. No new replies allowed.