Solved

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

Posted on 2011-03-25
4
329 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

24 Experts available now in Live!

Get 1:1 Help Now