tags:

views:

222

answers:

6

Hi guys I wrote this code and its always showing the same results why? The code is a searching method.

using System;
using System.Collections.Generic;
using System.Text;

namespace CArraySe
{
    class Program
    {
        class CArray
        {
            private int[] arr;
            private int upper;
            private int numElements;
            private int compCount;

            public CArray(int size)
            {
                arr = new int[size];
                upper = size - 1;
                numElements = 0;
                compCount = 0;
            }

            public void Insert(int item)
            {
                arr[numElements] = item;
                numElements++;
            }

            public void DisplayElements()
            {
                for (int i = 0; i <= upper; i++)
                {
                    Console.Write(arr[i]);
                    if (i == upper)
                    {
                        Console.WriteLine();
                        continue;
                    }
                    Console.Write(", ");
                }
            }

            public void Clear()
            {
                for (int i = 0; i <= upper; i++)
                    arr[i] = 0;
                numElements = 0;
            }
            public bool SeqSearch(CArray n, int sValue)
            {
                for (int index = 0; index < n.upper; index++)
                {
                    if (arr[index] == sValue)

                        return true;
                }

                compCount++;
                return false;
            }
            public int binSearch(CArray n, int value)
            {
                int upperBound, lowerBound, mid;
                upperBound = n.upper; lowerBound = 0;

                while (lowerBound <= upperBound)
                {
                    mid = (upperBound + lowerBound) / 2;

                    if (arr[mid] == value)
                        return mid;

                    else if (value < arr[mid]) upperBound = mid - 1;

                    else lowerBound = mid + 1;
                }
                compCount++;
                return -1;
            }

            static void Main(string[] args)
            {
                CArray nums = new CArray(10);
                Random rnd = new Random(100);
                for (int i = 0; i < 10; i++)
                    nums.Insert((int)(rnd.NextDouble() * 100));

                Console.WriteLine();
                Console.Write("The Binary Search Result is: ");
                Console.WriteLine(nums.binSearch(nums, 500));
                Console.WriteLine(nums.compCount);
                nums.Clear();
                for (int i = 0; i < 10; i++)
                    nums.Insert((int)(rnd.NextDouble() * 100));

                Console.Write("The Sequential Search result is: ");
                Console.WriteLine(nums.SeqSearch(nums, 500));
                Console.WriteLine(nums.compCount);
           }
       }
    }
}

Its always showing the same result even if I changed the number I'm looking for.

The output is:

The Binary Search Result is: -1
1
The Sequential Search result is: False
2
Press any key to continue . . .
+8  A: 

I think your value being searched for (500) is not being found. Try outputting the nums array and verifying that what you are looking for is in the array.

Plus, one search returns an int and the other returns a bool. Is there a specific reason for that?

Edit: Also, Binary Search only works with sorted lists.

crashmstr
Your comment on Binary Search is very good, and important.
JSBangs
Thanks. I remembered that after looking at the code again!
crashmstr
+4  A: 

Your method binSearch returns "-1" when the number isn't found. Since you're populating your array with random values, the odds are very good that the number you search for won't be found. So you're always getting "-1".

To test your binSearch method, you should populate the array with known values, then search for some value that is guaranteed to be in the array.

JSBangs
A: 

The first answer is correct. Also, even though you are using a random number, each run of the program will produce the same sequence of random numbers. You should seed it with a different number each time you run the program if you want to test the code well.

Sam
A: 

As the others have already mentioned, in the general case, there's no guarantee that the number you're searching for is in the list of randomly generated numbers. In the specific case that you posted, the number will never appear in the list because you're generating random numbers in the 0-100 range, then trying to find 500.

Jeromy Irvine
A: 

Running what you provided does not add a value above 100. If you change your add to this:

        for (int i = 0; i < 9; i++)
            nums.Insert((int)(rnd.NextDouble() * 100));
        nums.Insert(500);

The binSearch returns 9, but the SeqSearch return false because your looping search is index < n.upper and you need to do index <= n.upper to check all values. Additionally, as noted above, the binSearch only works in this case because 500 is larger than all the numbers in the array and has been placed at the last index. The binary search will only work by luck if the list its searching is not sorted. Therefore changing to:

        nums.Insert(500);
        for (int i = 0; i < 9; i++)
            nums.Insert((int)(rnd.NextDouble() * 100));

Will return -1; and true for the SeqSearch.

Timothy Carter
A: 

Although this probably doesn't answer your question directly, here are some observations which makes your code hard to understand and debug:

  • You need either one of numElements or upper, not both. In Clear(), you are setting only numElements to 0 whereas you are using upper in your loops everywhere?
  • Binary search works only with sorted arrays
  • If SeqSearch and BinSearch are receiving an instance of the array, shouldn't they be static methods instead of instance methods?
  • Chetan Sastry
    if i use staticError 1 An object reference is required for the nonstatic field, method, or property 'CArraySe.Program.CArray.arr'
    Error 3 Static member 'CArraySe.Program.CArray.SeqSearch(CArraySe.Program.CArray, int)' cannot be accessed with an instance reference; qualify it with a type name instead
    If you are converting a method to static, you should change it to not use any instance members inside it as well. You are using both n and arr, which makes it even harder to read. You should probably implement a public CArray.getElement(int index) method and use it instead of arr[index].
    Chetan Sastry