?
Solved

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

Posted on 2011-03-25
4
Medium Priority
?
335 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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