0

This is another question from my study guide for my final exam. If you all could please give me some help.

Write a value returning function called "found" with parameters of a float array a float item and a integer length, that searches the array for any value greater than the value of item. If such a value is found function returns true otherwise returns false. Length is the number of components in the array.

string found(float myarr[], float item, int length);
{
     while (int i=0; i<=(length-1);i++)
     {
          if (myarr[i] > item)
               return "TRUE";
          else
               return "FALSE";
      }
}

Edited by Nick Evan: Added code-tags

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by ukbassman88
0

You almost have it, what you need to change is :

string found(float myarr[], float item, int length);
{
  //need a for loop not a while
    for(int i=0; i<=(length-1);i++)
    {
          if (myarr[i] > item)
            return "TRUE";
     }
 //will reach here if above does not return true
  return "FALSE";

}

Next time, use code tags.

0

Thanks for the help. Is there anyway to make it into a while loop?

Sure but why ?

int cntr = 0;

while(cntr < length )
{
  //do code
  cntr++;
}
0

If you know about boolean types you could consider returning a boolean variable instead of a string variable. At least that's the type I think of when I want/need to return true/false.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.