In: Computer Science
#C.
Write a program that accepts any number of homework scores ranging in value from 0 through 10. Prompt the user for a new score if they enter a value outside of the specified range. Prompt the user for a new value if they enter an alphabetic character. Store the values in an array. Calculate the average excluding the lowest and highest scores. Display the average as well as the highest and lowest scores that were discarded.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeWorkDemo
{
class Program
{
static void Main(string[] args)
{
string score;
int score1;
int nums;
Console.Write("How many Homework scores do you want to enter? :
");
nums = Convert.ToInt32(Console.ReadLine());
int[] scores = new int[nums];
for(int i=0;i< nums;i++)
{
Console.Write("Enter Score"+(i+1)+": ");
score = Console.ReadLine() ;
int p;
if (int.TryParse(score, out p))
{
score1 = Convert.ToInt32(score);
if (score1 >= 0 && score1 <= 10)
{
scores[i] = score1;
}
else
{
while (true)
{
Console.Write("Please Enter valid Score" + (i + 1) + ": ");
score1 = Convert.ToInt32(Console.ReadLine());
if (score1 >= 0 && score1 <= 10)
{
scores[i] = score1;
break;
}
}
}
}
else
{
while (true)
{
Console.Write("Please Enter valid Score" + (i + 1) + ": ");
score1 = Convert.ToInt32(Console.ReadLine());
if (score1 >= 0 && score1 <= 10)
{
scores[i] = score1;
break;
}
}
}
}
Console.WriteLine("\nStored Values of the Array: ");
for (int i = 0; i < scores.Length; i++)
{
Console.Write(scores[i] + " ");
}
int max = scores[0];
int min = scores[0];
for (int i = 0; i < scores.Length ; i++)
{
if (max < scores[i])
{
max = scores[i]; //storing maximum value in max
}
if (min > scores[i])
{
min = scores[i]; //storing minimum value in min
}
}
int sum = 0;
double avg;
int c = 0;
for (int i = 0; i < scores.Length; i++)
{
if (!(scores[i] == min || scores[i] == max))
{
sum = sum + scores[i]; //sum of stored values except highest and
lowest
c++;
}
}
avg = (double)sum / c;
Console.WriteLine("\n\nThe Average: "+Math.Round(avg,2));
Console.WriteLine("Highest Score: "+max);
Console.WriteLine("Lowest Score: "+min);
Console.ReadKey();
}
}
}
output: