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"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constint MAXRANGE = 1000;
int recursiveMinimum( constint [], int, int );
int main()
{
constint 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( constint array[], int low, int high )
{
staticint 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
//Prototype:
int recursiveMinimum( constint [], int, int );
//Calling:
smallest = recursiveMinimum( array, 0, SIZE - 1 );
//Definition:
int recursiveMinimim( constint array[], int low, int high )
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
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.
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.
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...
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constint MAXRANGE = 1000;
int recursiveMinimum( constint [], int, int );
int main()
{
constint 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( constint array[], int low, int high )
{
staticint 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
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).