Program C#

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy