C # where the size of the index and array elements from the user is entered and then a specific element is searched

I am trying to create a program in which the size of the index of the array and its elements will be entered by the user. The program then prompts the user to search for a specific item and displays where it is located.

I already came up with the code:

using System;

namespace ConsoleApplication1
{

class Program
{

  public static void Main(String [] args)
  {
    int a;
    Console.WriteLine("Enter size of index:");
    a= int.Parse(Console.ReadLine());
    int [] index = new int [a];
    for (int i=0; i<index.Length;i++)
    {
      Console.WriteLine("Enter number:");
      index[i]=int.Parse(Console.ReadLine());
    }

  }
}
}

The problem is that I cannot display the numbers entered, and I do not know how to look for an array element. I am thinking about using an if statement.

Another thing, after entering the elements, the program should display numbers, such as Number 0: 1

Is this correct Console.WriteLine("Number"+index[a]+":"+index[i]);:?

And where should I put the expression? after a for loop or inside it?

+5
source share
5 answers

Console.WriteLine(index[i]);? , .

( , ), :

for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

, , :

// The user is entering the numbers (code copied from your question).
for (int i = 0; i < index.Length; i++)
{
    Console.WriteLine("Enter number: ");
    index[i] = int.Parse(Console.ReadLine());
}

// Now display the numbers entered.
for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

// Finally, search for the element and display where it is.
int elementToSearchFor;
if (int.TryParse(Console.ReadLine(), out elementToSearchFor))
{
    // TODO: homework to do.
}

, , , Linq TakeWhile(). ( , Linq, .)

+2

. , , . , ! : -).

+4
+2
        int a;
        Console.WriteLine("Enter size of Array:-");
        a = int.Parse(Console.ReadLine());
        int[] array = new int[a];
        Console.WriteLine("Enter the Elements of the Array:-");
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("\nThe Elemets of the Array are:-");
        for (int j = 0; j < array.Length; j++)
        {
            Console.WriteLine(array[j]);
        }
+1

, ,

, :

1) ()
2) ()
3) (TODO)
4) , (TODO)

, , , , # 3 # 4

An if statement can come into play when looking for the location of an item specified by the user.

0
source

All Articles