Skip to content

Instantly share code, notes, and snippets.

@sammysheep
Last active April 5, 2023 13:03
Show Gist options
  • Save sammysheep/8799bc4544dca3601f3aa3650213bdb5 to your computer and use it in GitHub Desktop.
Save sammysheep/8799bc4544dca3601f3aa3650213bdb5 to your computer and use it in GitHub Desktop.
SmithClass-SumArray
// Sam Shepard - 2023
// SumArray - add numbers to a List until we are given the word "STOP"
// Creates the sum and the average of the array entered.
List<int> my_power_levels = new List<int>();
while (true)
{
Console.Write("Enter a power level ('STOP' to stop): ");
string? input = Console.ReadLine();
// For the "condition", stop if empty or any casing of the word stop
if (String.IsNullOrWhiteSpace(input) || input.ToUpper() == "STOP")
{
break;
}
else
{
my_power_levels.Add(Convert.ToInt32(input));
}
}
// Sum the array
// YOUR SUM OF THE ARRAY HERE USING FOREACH!
// Compute the average!
double count = my_power_levels.Count;
// CALCULATE AVERAGE HERE!
Console.WriteLine($"The sum of {count} power entered is {sum}");
Console.WriteLine($"The average power level was: {average}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment