Statistics V2 - C# Programming Exercise

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 entered
- Find an item to check whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program
These options should appear as an interactive menu, where each option will be selected by a number or letter. The program must be able to store up to 1000 data entries, but also keep track of how many data have actually been entered.
This exercise will allow you to work with arrays to store the data, as well as control structures such as the interactive menu and the options to perform basic statistical calculations like sum, average, maximum, and minimum of the entered data. It will also help you practice data input handling and organizing menus in a console application.
This exercise is a great opportunity to learn how to structure interactive programs and perform statistical calculations in C#.

 Category

Arrays, Structures and Strings

 Exercise

Statistics V2

 Objective

Write a C# statistical program which will allow the user to:

- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program

These options must appear as a menu. Each option will be chosen by a number or a letter.

Create a statistical program which will allow the user to:

- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program

These options must appear as a menu. Each option will be chosen by a number or a letter.

The program must reserve space for a maximum of 1000 data, but keep count of how many data actually exist.

 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
    {
        double[] data = new double[1000];  // Create an array to store up to 1000 data points
        int count = 0;  // Variable to keep track of the number of data entries
        bool running = true;  // Variable to control the program's main loop

        // Display the menu to the user
        while (running)
        {
            Console.Clear();  // Clear the console screen before displaying the menu
            Console.WriteLine("Statistics Program");
            Console.WriteLine("1. Add new data");
            Console.WriteLine("2. See all data entered");
            Console.WriteLine("3. Find an item");
            Console.WriteLine("4. View statistics summary");
            Console.WriteLine("5. Exit");
            Console.Write("Please choose an option (1-5): ");  // Ask the user to choose an option

            string choice = Console.ReadLine();  // Get the user's choice

            switch (choice)  // Perform an action based on the user's choice
            {
                case "1":
                    // Add new data
                    if (count < 1000)  // Check if there's space for more data
                    {
                        Console.Write("Enter a number to add: ");  // Ask the user for a number
                        while (!double.TryParse(Console.ReadLine(), out data[count]) || data[count] < 0)  // Ensure valid input
                        {
                            Console.WriteLine("Invalid input. Please enter a valid number.");  // Handle invalid input
                            Console.Write("Enter a number to add: ");  // Prompt again for a valid input
                        }
                        count++;  // Increment the count after adding new data
                        Console.WriteLine("Data added successfully!");  // Inform the user that the data was added
                    }
                    else
                    {
                        Console.WriteLine("Maximum data limit reached.");  // Inform the user if the limit is reached
                    }
                    break;

                case "2":
                    // See all data entered
                    if (count > 0)  // Check if there is any data entered
                    {
                        Console.WriteLine("Entered data:");  // Inform the user that the entered data will be displayed
                        for (int i = 0; i < count; i++)  // Loop through the data array
                        {
                            Console.WriteLine(data[i]);  // Display each data entry
                        }
                    }
                    else
                    {
                        Console.WriteLine("No data entered yet.");  // Inform the user if no data was entered
                    }
                    break;

                case "3":
                    // Find an item
                    Console.Write("Enter a number to search for: ");  // Ask the user for a number to search
                    double searchValue;  // Variable to store the search value
                    while (!double.TryParse(Console.ReadLine(), out searchValue))  // Ensure valid input
                    {
                        Console.WriteLine("Invalid input. Please enter a valid number.");  // Handle invalid input
                        Console.Write("Enter a number to search for: ");  // Prompt again for a valid input
                    }
                    bool found = false;  // Variable to track if the number is found
                    for (int i = 0; i < count; i++)  // Loop through the data array
                    {
                        if (data[i] == searchValue)  // Check if the number is in the data
                        {
                            found = true;  // Set found to true if the number is found
                            break;  // Exit the loop once the number is found
                        }
                    }
                    if (found)  // If the number is found
                    {
                        Console.WriteLine("The number was found in the data.");  // Inform the user that the number was found
                    }
                    else
                    {
                        Console.WriteLine("The number was not found in the data.");  // Inform the user that the number was not found
                    }
                    break;

                case "4":
                    // View statistics summary
                    if (count > 0)  // Check if there is any data entered
                    {
                        double sum = 0, max = data[0], min = data[0];  // Initialize variables for sum, max, and min
                        for (int i = 0; i < count; i++)  // Loop through the data array
                        {
                            sum += data[i];  // Add the current data point to the sum
                            if (data[i] > max)  // Check if the current data point is greater than the current max
                                max = data[i];  // Update the max value if necessary
                            if (data[i] < min)  // Check if the current data point is less than the current min
                                min = data[i];  // Update the min value if necessary
                        }
                        double average = sum / count;  // Calculate the average

                        Console.WriteLine($"Data count: {count}");  // Display the number of data entries
                        Console.WriteLine($"Sum: {sum}");  // Display the sum of the data
                        Console.WriteLine($"Average: {average:F2}");  // Display the average with 2 decimal places
                        Console.WriteLine($"Maximum: {max}");  // Display the maximum value
                        Console.WriteLine($"Minimum: {min}");  // Display the minimum value
                    }
                    else
                    {
                        Console.WriteLine("No data entered yet.");  // Inform the user if no data was entered
                    }
                    break;

                case "5":
                    // Exit the program
                    running = false;  // Set running to false to exit the loop
                    break;

                default:
                    Console.WriteLine("Invalid option. Please choose a valid option.");  // Handle invalid menu option
                    break;
            }

            Console.WriteLine("\nPress any key to continue...");  // Prompt the user to press a key to continue
            Console.ReadKey();  // Wait for the user to press a key before displaying the menu again
        }
    }
}

 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#.

  •  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)

  •  Array of struct

    In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points usin...

  •  Array of struct and menu

    In this C# exercise, you are asked to expand the previous exercise (array of points) so that the program displays an interactive menu. The menu should a...

  •  Books database

    In this C# exercise, you are asked to create a small database that will be used to store information about books. For each book, the following data should be kept:

  •  Triangle V2

    In this C# exercise, you are asked to create a program that asks the user for their name and then displays a triangle made from that name, starting with 1 letter and ...

  •  Rectangle V3

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from that name.