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
C# Exercise Example
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
}
}
}