Programming Course in C# ¡Free!

Search in array

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a program that says if a data belongs in a list that was previously created.

The steps to take are:

- Ask the user how many data will he enter.
- Reserve space for that amount of numbers (floating point).
- Request the data to the user
- Later, repeat:
* Ask the user for a number (execution ends when he enters "end" instead of a number).
* Say if that number is listed or not.

Must be done in pairs. but you must provide a single source file, containing the names of both programmers in a comment.

Output



Solution


using System;
public class SearchArray
{
public static void Main()
{   
Console.Write("Amount: ");
int amount = Convert.ToInt32( Console.ReadLine() );

float number;
float[] list = new float[amount];

for(int i=1; i <= amount; i++)
{
Console.Write("Enter a number {0}: ", i);
list[i] = Convert.ToSingle( Console.ReadLine() );
}

Console.Write("Number a search: ");
number = Convert.ToSingle(Console.ReadLine());

while(Console.ReadLine() != "end")
{
for(int i=1; i <= amount; i++)
{
if (list[i] == number)
Console.WriteLine("The number {0} exist", number );
}
}   
}
}