Solved

Help with fixing code to search and find integer values in an array list?

Posted on 2011-03-25
4
333 Views
Last Modified: 2013-12-17
Hey,

I have a code for a project that has me stumped. I need to
find what needs to be fixed or added to make the code I
was given work. I've tried to read and look up on the web on
own but no luck. My problem is the code I have needs to be
fixed so it can search and find integer values in an array.
Here is the code:


public class Search
{
    private int[] arrayToSearch = new int[20] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };

    static void main(string[] args)
    {
        Search mySearch = new Search();

        System.Console.WriteLine(mySearch.search(30));
    }

    private string search(int value)
    {
        string found = "Value was not found";

        bool wasFound = find(value, 0);

        if (wasFound)
            found = "Value was found";

        return found;
    }

    private bool find(int value, int index)
    {
        bool found = false;

        if (this.arrayToSearch[index] == value)
            found = true;
        else
            find(value, index + 1);

        return found;
    }
}

if anyone knows how to fix it, it would help me out greatly and
get me back on track with my work, thanks for any help!
0
Comment
Question by:xstaticx69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:jesusaflores
ID: 35217813
this aplication is type console??
0
 
LVL 2

Accepted Solution

by:
ericpeckham earned 500 total points
ID: 35218149
Try this:

      public class Search
      {
            private int[] arrayToSearch = new int[20] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };

            static void Main(string[] args)
            {
                  Search mySearch = new Search();

                  System.Console.WriteLine(mySearch.search(14));
            }

            private string search(int value)
            {
                  string found = "Value was not found";

                  bool wasFound = find(value);

                  if (wasFound)
                        found = "Value was found";

                  return found;
            }

            private bool find(int value)
            {
                  return Array.Exists(arrayToSearch, x => x == value);
            }
      }
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35218521
It ~looks~ like it's trying to setup a RECURSIVE search algorithm in find().

Is this a REQUIREMENTof the project?

Obviously there are many other ways to go about this!...
0
 
LVL 2

Expert Comment

by:ericpeckham
ID: 35219024
Idle_Mind is correct that there are many ways to go about this - the best way depends on what you really need.  As for your original question, the solution I provided will work to find if a value exists in your array or not.  You can do it with even less code, and you might be better off using a List<int> to store your integer values in - it's easier to add and delete from the list, and to do many other things as well.  It all depends on what all you need to do.

Let me know (accept my answer) if my solution will work for you.  As Idle_Mind suggests, I'm a bit concerned with what you were trying to do by passing in that index value and incrementing it.  Are you wanting to get the closest integer in the array if the exact value isn't found?  But even if you do, what then?  The thing is, it's making no difference to the outcome of your find method when you do the recursive find.  If you just want to know if the given value equals the array element of the given index, then it's as easy as:

            private bool find(int value, int index)
            {
                  return arrayToSearch[index] == value;
            }

If this doesn't answer it for you, please provide more details.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question