Skip to content

C# Program CheckZIPS

Asked Mar 9, 2007, 12:57 PM EST2 answers
Share
LinkedInX

Program checkZIPS is working well with all the zip code except "BK 87946". When I entered BK 87946 the output was “Zip BK 87946 not found”. That means program is behaving very strangely. Please explain. I am attaching the program.

C#
using System;
public
class CheckZIPS
{

public static void Main()
{

string[] zips = {"BF 45633", "GJ 76895", "BK 87946", "NM 38657"};

int x;

string enterzip;
Console.Write("Enter Zip code: ");
enterzip = Console.ReadLine();
x = Array.BinarySearch(zips, enterzip);

if(x < 0)
Console.WriteLine("Zip {0} not found", enterzip);

else
Console.WriteLine("Zip {0} was found at position {1}",
enterzip, x);
}
}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You must sort an array if you want to use binarysearch.
    Array.Sort(zips);

    Or you can use

    x = Array.IndexOf(zips, enterzip);

    Good Luck, Alex

    0
  • 0
    Thank you very much for your answer
    0