Answer to Question #176766 in C# for Patrick Mabunay

Question #176766

Write a program using one-dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list.


Sample input/output dialogue:


Enter a list of numbers: 5 4 8 2 6

Enter a number to be searched: 2 2 found in location 4


1
Expert's answer
2021-03-29T16:30:27-0400
using System;
using System.Text.RegularExpressions;

class Program
{
  static void Main()
  {
      Console.Write("Enter a list of numbers: ");
      string[] ints = Regex.Split(Console.ReadLine(), " ", RegexOptions.CultureInvariant);
      int[] arr = new int[ints.Length];
      for (int i = 0; i < ints.Length; ++i)
      {
          arr[i] = Convert.ToInt32(ints[i]);
      }
      Console.Write("Enter a number to be searched: ");
      int num = Convert.ToInt32(Console.ReadLine());
      int pos = Array.FindIndex(arr, (obj) => { return obj == num; }) + 1;
      if (pos != 0)
      {
          Console.WriteLine($"{num} found in location {pos}");
      }
      else
      {
          Console.WriteLine($"{num} not found");
      }
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS