Search In Array - C# Programming Exercise

In this C# exercise, you are asked to write a program that checks if a given data belongs to a previously created list.

The steps to follow are:

- Ask the user how many data they will enter.
- Reserve space for that number of floating-point numbers.
- Request the data from the user.

Later, the program should repeat as follows:

- Ask the user for a number (the program ends when they enter "end" instead of a number).
- Indicate whether that number is listed or not.

This exercise must be done in pairs, but you must provide a single source file that contains both programmers' names in a comment. This exercise is useful for practicing lists in C#, using loops, and how to interact with the user to efficiently receive and process data.

 Category

Arrays, Structures and Strings

 Exercise

Search In Array

 Objective

Write a C# 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.

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Comment with the names of both programmers
        // Programmers: [Your Name] & [Partner's Name]

        // Ask the user how many data points they want to enter
        Console.Write("How many numbers will you enter? ");
        int count = Convert.ToInt32(Console.ReadLine());  // Read and store the number of data points

        // Create an array to store the floating point numbers
        double[] numbers = new double[count];

        // Ask the user for the data and store it in the array
        Console.WriteLine("Enter the numbers:");
        for (int i = 0; i < count; i++)
        {
            Console.Write($"Enter number {i + 1}: ");  // Prompt the user for a number
            numbers[i] = Convert.ToDouble(Console.ReadLine());  // Convert the input to a double and store it in the array
        }

        // Start checking if the entered numbers exist in the list
        while (true)
        {
            Console.Write("\nEnter a number to search for (or 'end' to quit): ");
            string input = Console.ReadLine();  // Read user input

            // Check if the user wants to end the program
            if (input.ToLower() == "end")
            {
                break;  // Exit the loop if the user types "end"
            }

            // Convert the input to a double and check if it exists in the array
            double searchValue;
            bool isValid = double.TryParse(input, out searchValue);  // Check if the input can be parsed as a double

            if (isValid)
            {
                // Check if the number exists in the array
                bool found = false;
                foreach (double number in numbers)
                {
                    if (number == searchValue)
                    {
                        found = true;
                        break;
                    }
                }

                // Display whether the number is found in the array or not
                if (found)
                {
                    Console.WriteLine($"The number {searchValue} is in the list.");
                }
                else
                {
                    Console.WriteLine($"The number {searchValue} is not in the list.");
                }
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a valid number or 'end' to quit.");
            }
        }

        Console.WriteLine("Goodbye!");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Array of even numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 integer numbers and displays the even ones. This exercise w...

  •  Array of positive and negative numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 real numbers and displays the average of the positive ones and the average of t...

  •  Many numbers and sum

    In this C# exercise, you are asked to write a program that asks the user for several numbers (until they enter the word "end") and then displays the sum of the...

  •  Two dimensional array

    In this C# exercise, you are asked to write a program that asks the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and then disp...

  •  Statistics V2

    In this C# exercise, you are asked to create a statistical program that allows the user to perform the following actions: - Add new data - See all data ...

  •  Struct

    In this C# exercise, you are asked to create a struct to store data of 2D points. The fields for each point will be: - x coordinate (short)