Solved

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

Posted on 2011-03-25
4
328 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
  • 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 85

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
work allocation; web development; vba; access; 4 46
Set form below another form 3 26
Get String split 5 33
COnsume rest client 6 12
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now