<<mod edit: threads merged>>

I need to write a recursion function recursiveMinimum that takes an integer array and the array size as arguments and returns the element with the smallest value in the array. The function should stop processing and return control to the caller if it receives a one-element array.

Any help would be great.

<<mod edit: threads merged>>

I need to write a recursion function recursiveMinimum that takes an integer array and the array size as arguments and returns the element with the smallest value in the array. The function should stop processing and return control to the caller if it receives a one-element array.

Any help would be great.

#include <iostream.h>
int recursiveMinimum (int [] , int);
int main()
{
 int integer,smallest, counter = 0;
 int ArraySize;
 int elements[] ={0};
    while(integer != -1)
 {
  cout<<"Enter the integer: ";
  cin>>integer;
 
  counter++;
 }  
 ArraySize=counter;
    elements[counter]=integer;
 
 smallest = recursiveMinimum (elements, ArraySize);
 
 cout << "The smallest element of the array has the value of: " << smallest << endl;
 return 0;
}
int recursiveMinimum (int b [], int sizeOfArray)
{
 int min;
 if (sizeOfArray == 1)
  return b[sizeOfArray - 1];
 else
  min = recursiveMinimum (b, sizeOfArray - 1);
 if (min > b[sizeOfArray - 1])
  min = b[sizeOfArray - 1];
 return min;
}

I cant get the program to get the smallest value.. it gives me a wrong return i.e. "null" or "some huge number that doesnt exist". I would be grateful if anyone can post the solution so that I can submit as fast as I can, as it already due.

This is the result of the program as a picture file.

[IMG]http://img106.imageshack.us/img106/8305/outputoftheprogram9ex.jpg[/IMG]

Thanks in advance.:mrgreen:

>> int elements[] ={0};

This array only has one elelement and its value is 0

>> while(integer != -1)

I'm supprised this loop works at all! variable integer is unitialized and contains some random value.

>> elements[counter]=integer;
you can't resize an array like that. If you want to make the array dynamic then make it a pointer and use new (if this is a c++ program) or malloc (c programs) to adjust it to the desired size.

int* elements = 0;
...
...
   elements = new int[counter];

// or
   elements = malloc(counter * sizeof(int));

<<mod edit: threads merged -- original context is now somewhat wacky>>

why don't you get with Omher to solve your common assignment?

Thanks for your help.. but can you give me an idea how to set up the program as I am getting lost over here... Sorry if i am troubling any of you people.

Thanks in advance.

maybe this will get you started. iostream.h is obsolete -- if you are uisng a recently new compiler then use iostream (without the .h extension)

#include <iostream>
#include <ctime>
using namespace std;
int recursiveMinimum (int [] , int);
int main()
{
 int i,smallest, counter = 0;
 int ArraySize = 10;
 int elements[ArraySize];
  // fill the array with some random numbers
  srand(time(0));
  for(i = 0; i < ArraySize; i++)
       elements[i] = rand();  
  smallest = recursiveMinimum (elements, ArraySize);
 // remainder of program not shown

Hi ,

Try the code below.
I have modified your code so that it should run properly.
I have commented the problematic code of yours and added mine.

I think this will solve your purpose

Atul.

#include <iostream.h>
int recursiveMinimum (int [] , int);
int main()
{
 int integer,smallest, counter = 0;
 
 int ArraySize;
 file://int elements[] ={0};                    //ur code
 int elements[3];
 
    while(integer != -1)
 {
  cout<<"Enter the integer: ";
  cin>>integer;
 elements[counter]=integer; file:            //mine code
 counter++;
 }  
 counter --;
 ArraySize=counter;
  //  elements[counter]=integer;           //ur code 
 
 smallest = recursiveMinimum (elements, ArraySize);
 
 cout << "The smallest element of the array has the value of: " << smallest << endl;
 return 0;
}
 
int recursiveMinimum (int b [], int sizeOfArray)
{
 int min;
 if (sizeOfArray == 1)
  return b[sizeOfArray - 1];
 else
  min = recursiveMinimum (b, sizeOfArray - 1);
 if (min > b[sizeOfArray - 1])
  min = b[sizeOfArray - 1];
 return min;
}

Hi ,

Try the code below.
I have modified your code so that it should run properly.
I have commented the problematic code of yours and added mine.

I think this will solve your purpose

So now its the blind leading the blind:mrgreen:

So now its the blind leading the blind:mrgreen:

Are you losing your eyesight already. I did not think you were that old Ancient Dragon? ;)

Are you losing your eyesight already. I did not think you were that old Ancient Dragon? ;)

No -- but the code posted by Atul also has lots of problems/bugs. But then of course I'm perfect -- I was only wrong once in my life and that's when I thought I was wrong, but I was wrong about that.:mrgreen:

No -- but the code posted by Atul also has lots of problems/bugs. But then of course I'm perfect -- I was only wrong once in my life and that's when I thought I was wrong, but I was wrong about that.:mrgreen:

Can u let me know the bugs in my code Ancient Dragon ?
Except the line "file://int elements[] ={0}; //ur code".
coz i want to be perfect like u .... :lol:

Can u let me know the bugs in my code Ancient Dragon ?
Except the line "file://int elements[] ={0}; //ur code".
coz i want to be perfect like u .... :lol:

Worry about doing your own homework, instead of other people's...then you will be perfect like me. :cheesy:

Worry about doing your own homework, instead of other people's...then you will be perfect like me. :cheesy:

right sir ....thanks for ur valuable suggestion. :cool:

Can u let me know the bugs in my code Ancient Dragon ?
Except the line "file://int elements[] ={0}; //ur code".
coz i want to be perfect like u .... :lol:

For starters:

1. variable integer is being used without having been initialized. It will just contain some random value -- whatever happens to be on the stack at the time -- including -1.

2. There are no bounds check to insure that counter does not exceed the number of elements allocated to the array. I can type in as many numbers as I wish.

3. The value of ArraySize after the end of the previous loop may exceed the size of the array due to #2 above.

Once you have the C++ errors corrected I think you will find that the program logic needs a little tweaking as well. If so, post a program with the C++ errors corrected and I can post pseudocode for what I think is some improved logic.

This article has been dead for over six months. Start a new discussion instead.