Group
C# Arrays, Structures and Strings
Objective
1. Implement a menu system where the user can select different options using a number or letter.
2. Store up to 1000 numerical values in an array while keeping track of how many have been entered.
3. Implement functionality to:
- Add new data
- Display all entered data
- Search for a specific value
- Show summary statistics (count, sum, average, max, min)
4. Ensure the program runs in a loop until the user selects the exit option.
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.
The program must reserve space for a maximum of 1000 data, but keep count of how many data actually exist.
Example C# Exercise
Show C# Code
using System;
class StatisticalProgram
{
static void Main()
{
double[] data = new double[1000]; // Array to store up to 1000 values
int count = 0; // Counter for the number of elements entered
char choice;
do
{
// Display menu options
Console.WriteLine("\nStatistical Data Management");
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("Choose an option: ");
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
switch (choice)
{
case '1': // Add new data
if (count < 1000)
{
Console.Write("Enter a number: ");
if (double.TryParse(Console.ReadLine(), out double number))
{
data[count++] = number;
Console.WriteLine("Number added successfully.");
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
else
{
Console.WriteLine("Data limit reached (1000 entries).");
}
break;
case '2': // Display all entered data
if (count > 0)
{
Console.WriteLine("Data entered:");
for (int i = 0; i < count; i++)
{
Console.Write(data[i] + " ");
}
Console.WriteLine();
}
else
{
Console.WriteLine("No data has been entered yet.");
}
break;
case '3': // Find an item
if (count > 0)
{
Console.Write("Enter a number to search: ");
if (double.TryParse(Console.ReadLine(), out double searchNumber))
{
bool found = false;
for (int i = 0; i < count; i++)
{
if (data[i] == searchNumber)
{
found = true;
break;
}
}
Console.WriteLine(found ? "Number found in the list." : "Number not found.");
}
else
{
Console.WriteLine("Invalid input.");
}
}
else
{
Console.WriteLine("No data available to search.");
}
break;
case '4': // Display summary statistics
if (count > 0)
{
double sum = 0, max = data[0], min = data[0];
for (int i = 0; i < count; i++)
{
sum += data[i];
if (data[i] > max) max = data[i];
if (data[i] < min) min = data[i];
}
double average = sum / count;
Console.WriteLine($"Total data count: {count}");
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Average: {average:F2}");
Console.WriteLine($"Maximum: {max}");
Console.WriteLine($"Minimum: {min}");
}
else
{
Console.WriteLine("No data available for statistics.");
}
break;
case '5': // Exit
Console.WriteLine("Exiting program. Goodbye!");
break;
default:
Console.WriteLine("Invalid choice. Please select a valid option.");
break;
}
} while (choice != '5');
}
}
Output
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 1
Enter a number: 25
Number added successfully.
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 1
Enter a number: 40
Number added successfully.
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 2
Data entered:
25 40
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 3
Enter a number to search: 25
Number found in the list.
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 4
Total data count: 2
Sum: 65
Average: 32.50
Maximum: 40
Minimum: 25
Statistical Data Management
1. Add new data
2. See all data entered
3. Find an item
4. View statistics summary
5. Exit
Choose an option: 5
Exiting program. Goodbye!