Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 32
Database Connections Not being returned to Connection Pool 7 27
C# winforms programmitically move panels 6 29
Powershell File Sort 8 31
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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