0% found this document useful (0 votes)
76 views

Program C#

The document describes a C# program that performs a linear search on an integer array. It prompts the user to enter the number of elements in the array and the values for each element. It then prompts for an element to search for and loops through the array, checking each element for a match and printing the index if found. If no match is found it prints that the search was unsuccessful.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Program C#

The document describes a C# program that performs a linear search on an integer array. It prompts the user to enter the number of elements in the array and the values for each element. It then prompts for an element to search for and loops through the array, checking each element for a match and printing the index if found. If no match is found it prints that the search was unsuccessful.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Data; namespace Console_App { public class clsLinearSearch { public static void Main() { try { int[] SearchArray = new int[200]; Console.WriteLine("Program for sequential Search"); Console.WriteLine("Enter number of elements of searching array?"); int NumberCount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" "); Console.WriteLine("\nEnter integer searching array elements \n"); for (int i = 0; i < NumberCount; i++) { SearchArray[i] = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("Enter element to search:\n"); int NumbertoSearch = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < NumberCount; i++) { if (SearchArray[i] == NumbertoSearch) { Console.WriteLine(" "); Console.WriteLine("Congratulations: Element {0} found at location {1}\n", NumbertoSearch, i + 1); Console.ReadLine(); return; } } Console.WriteLine("Sorry: Search unsuccessful"); } catch (Exception ex) { //handle exception here } Console.ReadLine(); } } }

using System; using System.Text; using System.Collections; using System.Data; namespace Console_App { public class clsLinearSearch { public static void Main() { try { int[] SearchArray = new int[200]; Console.WriteLine("Program for Linear Search"); Console.WriteLine("Enter number of elements of searching array?"); int NumberCount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" "); Console.WriteLine("\nEnter integer searching array elements \n"); for (int i = 0; i < NumberCount; i++) { SearchArray[i] = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("Enter element to search:\n"); int NumbertoSearch = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < NumberCount; i++) { if (SearchArray[i] == NumbertoSearch) { Console.WriteLine(" "); Console.WriteLine("Congratulations: Element {0} found at location {1}\n", NumbertoSearch, i + 1); Console.ReadLine(); return; } } Console.WriteLine("Sorry: Search unsuccessful"); } catch (Exception ex) { //handle exception here } Console.ReadLine(); } } }

using using using using

System; System.Collections.Generic; System.Linq; System.Text; namespace forgetCode { class Program { public static void Main() { int[] a = new int[100]; Console.WriteLine("Number of elements in the array ?"); string s = Console.ReadLine(); int x = Int32.Parse(s); Console.WriteLine("-----------------------"); Console.WriteLine(" Enter array elements "); Console.WriteLine("-----------------------"); for (int i = 0; i < x; i++) { string s1 = Console.ReadLine(); a[i] = Int32.Parse(s1); } Console.WriteLine("--------------------"); Console.WriteLine("Enter Search element"); Console.WriteLine("--------------------"); string s3 = Console.ReadLine(); int x2 = Int32.Parse(s3); int low = 0; int high = x - 1; while (low <= high) { int mid = (low + high) / 2; if (x2 < a[mid]) high = mid - 1; else if (x2 > a[mid]) low = mid + 1; else if (x2 == a[mid]) { Console.WriteLine("-----------------"); Console.WriteLine("Search successful"); Console.WriteLine("-----------------"); Console.WriteLine("Element {0} found at location {1}\n", x2, mid + } } return;

1);

} Console.WriteLine("Search unsuccessful"); } }

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace forgetCode { class Program { public static void Main() { int[] a = new int[100]; Console.WriteLine("Number of elements in the array ?"); string s = Console.ReadLine(); int x = Int32.Parse(s); Console.WriteLine("-----------------------"); Console.WriteLine(" Enter array elements "); Console.WriteLine("-----------------------"); for (int i = 0; i < x; i++) { string s1 = Console.ReadLine(); a[i] = Int32.Parse(s1); } for ( int i = 0; i < x - 1; i++) { for ( int j = i + 1; j < x; j++) { if (a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } Console.WriteLine("The sorted List is:"); for ( int i = 0; i < x; i++) Console.WriteLine("{0} \t", a[i]); Console.WriteLine("--------------------"); Console.WriteLine("Enter Search element"); Console.WriteLine("--------------------"); string s3 = Console.ReadLine(); int x2 = Int32.Parse(s3); int low = 0; int high = x - 1; while (low <= high) { int mid = (low + high) / 2; if (x2 < a[mid]) high = mid - 1; else if (x2 > a[mid]) low = mid + 1;

} } }

} Console.WriteLine("Search unsuccessful");

else if (x2 == a[mid]) { Console.WriteLine("-----------------"); Console.WriteLine("Search successful"); Console.WriteLine("-----------------"); Console.WriteLine("Element {0} found at location {1}\n", x2, mid + 1); return; }

You might also like