This program is producing correct output for "Belt" and "Tie" input. But for the other inputs ("Scarf" and "Cufflinks") program output is "No match found". Anyone can fix the error.
// Debug06-01
// Lists items for sale and their prices
using System;
public class DebugSix01
{
public static void Main()
{
string[] items = { "Belt", "Tie", "Scarf", "Cufflinks"};
double[] prices = { 29.00, 35.95, 18.50, 112.99 };
Console.Write("Items for sale: ");
string enterItem = Console.ReadLine();
int arrayPosition = Array.BinarySearch(items, enterItem);
if (arrayPosition < 0)
Console.WriteLine("No match found");
else
Console.WriteLine("Price {0}", prices[arrayPosition].ToString("c"));
Console.ReadKey();
}
}
Loading
Posted Oct 3, 2012, 2:38 PM
VulpesPosted Oct 3, 2012, 2:35 PM
As it would be awkward to sort the items array in such a way that the prices array still corresponded, I'd use Array.IndexOf instead: