Write a test program implemented in the main() method that, given the preinitilized array of 20 integers { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20 }, displays the index where a user-specified number appears in the array, followed by the number of comparisons performed. (Hint: use a variable to count the actual number of comparisons performed).
Program should says:
Element 5 was found at position __(index) after ___(how many tries) comparisonsCODE
/*
* File: Program.cs
* Program: TestSeach
* Author: KTL
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace TestSearch
{
class Program
{
static void Main(string[] args)
{
int[] Arr = { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20};
int index;
int elem = 5;
index = Search.Linear( Arr, elem); // i or -1
if (index == -1)
Console.Out.WriteLine("Element" + elem + "was not found!" );
else
Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");
}
}
}
Added a class: Search.csCODE
/*
* File: Program.cs
* Program: TestSeach
* Author: KTL
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace TestSearch
{
class Program
{
static void Main(string[] args)
{
int[] Arr = { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20};
int index;
int elem = 5;
index = Search.Linear( Arr, elem); // i or -1
if (index == -1)
Console.Out.WriteLine("Element" + elem + "was not found!" );
else
Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");
}
}
}
How do I find how many times it took to find the element 5 in a binary search??? HELP!