I have been working on this assignment for a couple days now. I have written all the code myself. I'm not looking to cheat or have someone do my work for me, but for the life of me, I can't get this to work correctly.
The problem I'm having is that when I try to average numbers in an array, instead of dividing by just the number of entries, it's dividing by the total allowed entries into the array.
For example. I enter 2 values into an array that can hold 100 values, instead of dividing by 2, it divides by 100.
How do I get it to divide by just the number of entries? Here is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void InputData(string[] player, int[] score, ref int numPlayer)
{
// input items up to the number of the array size
while (numPlayer < player.Length)
{
Console.Write("Enter player name (Q to quit): ");
player[numPlayer] = Console.ReadLine();
if ((player[numPlayer] == "Q") || (player[numPlayer] == "q"))
{
Console.WriteLine();
break;
}
else
{
Console.Write("Enter score for " + player[numPlayer] + ": ");
score[numPlayer] = Convert.ToInt32(Console.ReadLine());
numPlayer++;
}
}
}
static void DisplayPlayerData(string[] player, int[] score, int numPlayer)
{
Console.WriteLine("Name Score");
for (int i = 0; i < numPlayer; i++)
Console.WriteLine("{0, -16} {1, 8}", player[i], score[i]);
}
static double CalculateAverageScore(int[] score, ref int numPlayer)
{
double avgScore;
double total = 0;
for (int i = 0; i < numPlayer; i++)
{
total += score[i];
}
avgScore = total / score.Length;
Console.WriteLine("Average Score:" + avgScore);
return avgScore;
}
static void DisplayBelowAverage(string[] player, int[] score, int numPlayer)
{
double avgScore = CalculateAverageScore(score, ref numPlayer);
Console.WriteLine("Players who scored below average");
Console.WriteLine("Name Score");
for (int i = 0; i < numPlayer; i++)
{
if (score[i] < avgScore)
{
Console.WriteLine("{0, -16} {1}", player[i], score[i]);
}
}
}
static void Main(string[] args)
{
//Variables
string[] player = new string[100];
int[] score = new int[100];
int numPlayer = 0;
InputData(player, score, ref numPlayer);
DisplayPlayerData(player, score, numPlayer);
CalculateAverageScore(score, ref numPlayer);
DisplayBelowAverage(player, score, numPlayer);
Console.ReadLine();
}
}
}
You have a numPlayer variables that stands for a number of entered players.
Just use it.
Replace
avgScore = total / score.Length;
with
avgScore = total / numPlayer;
Your code has some very strange points.
For example, you call CalculateAverageScore(score, ref numPlayer); in a Main(). However, you are not using a return value. This method is properly called in a DisplayBelowAverage method.
In general, it looks wrong - refs, static-sized array with dynamic number of values, non-format console writeline etc.
Just for you information. Read this once. Maybe some code lines will help you. Maybe, you won't find something new, unknown or interesting.
This is how I would solve this problem:
public class Program
{
private const string InputTerminationString = "Q";
public static void Main()
{
List<Player> players = new List<Player>(); // p. 1, 4
while (true)
{
Console.Write("Enter player name ({0} to quit): ", InputTerminationString);
string name = Console.ReadLine();
if (name == InputTerminationString) break; // p. 2
Console.Write("Enter score for {0}: ", name); // p. 3
int score = int.Parse(Console.ReadLine());
players.Add(new Player { Name = name, Score = score });
}
Console.WriteLine("Name Score");
players.ForEach(x => Console.WriteLine("{0, -16} {1, 8}", x.Name, x.Score)); // p. 5
double average = players.Average(x => x.Score); // p. 6
Console.WriteLine("Average score: {0:F2}", average); // p. 3
Console.WriteLine("Players who scored below average");
Console.WriteLine("Name Score");
players
.Where(x => x.Score < average) // p. 7
.ToList()
.ForEach(x => Console.WriteLine("{0, -16} {1, 8}", x.Name, x.Score)); // p. 5
}
}
public class Player
{
public string Name { get; set; }
public int Score { get; set; }
}
Now, step by step:
Use Player class. It is pretty inconvenient to pass two independent arrays of names and scores. Moreover, it is not safe and proper in general. A name and a score of a player are properties if a single player and should be stored together either in struct or class.
Use constants. If you need to change 'Q' termination string to 'Exit', you will be able to do this in a second without looking through the code.
Use formatted Console.WriteLine. It works liks String.Format and you don't need to concatenate strings.
Some info here: https://msdn.microsoft.com/en-us/library/828t9b9h(v=vs.110).aspx
Use dynamic collection List. Array is good for storing and rapid accessing to range of values of known length. Since a user enters values one by one, you never know if he will enter 0, 1 or 90000 values. List collection will help you with it.
Some info here: http://www.dotnetperls.com/list
You can use ForEach method which executes the given code for every item in a collection. Using ForEach with lambda-expressions makes code shorter and more readable.
Some info here: https://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx
Use Average function which calculates the average of a collection. In case of int[] you can use arr.Average(). However, in case of Class object, you need to describe the logic of calculation of average value using lambda-expression.
Some info here: http://www.dotnetperls.com/average
LINQ Where expression lets you filter your collection.
Some info here: http://www.dotnetperls.com/where
Instead of writing CalculateAverageScore the you can make use of LINQ's Average method:
double avgScore = score.Average();
Console.WriteLine("Average Score:" + avgScore);
Forthermore it is better not to mix calculating with I/O, so:
static double CalculateAverageScore(int[] score) {
return score.Average();
}
Or since you use an array, use the sum:
static double CalculateAverageScore(int[] score, ref int numPlayer) {
return (double) score.Sum()/numPlayer;
}
and do the printing in the Main:
static void Main(string[] args) {
//Variables
string[] player = new string[100];
int[] score = new int[100];
int numPlayer = 0;
InputData(player, score, ref numPlayer);
DisplayPlayerData(player, score, numPlayer);
double avgScore = CalculateAverageScore(score, ref numPlayer);
Console.WriteLine("Average Score:" + avgScore);
DisplayBelowAverage(player, score, numPlayer);
Console.ReadLine();
}
online Ideone demo.
You furthermore better modify the array to a List. Here there is also no reason to use the ref keyword in all these methods, nor to make them static.
Related
The goal is to create an array size from user input via how many test scores. Then create a loop that will populate an array by prompting the user for each test score from 0 to 100.
Finally display the results, typically using another loop.
Question: Why is it when the test scores are entered example "50" it adds 50 elements of 0 to the array?
any assistance would be grateful, thank you. I've seen a few similar posts but couldn't resolve this issue. Also, one was in Spanish.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
// prompt user to ask how many test scores to build the size of the array
Write("How many test scores total: ");
string sSize = ReadLine();
int i = Convert.ToInt32(sSize);
int[] score = new int[i];
// create the loop of asking the test scores limited to the array sSize
for (int a = 1; a < i + 1; a++)
{
Write("Please enter a test score " + a + " from 0 to 100: ");
string testArray = ReadLine();
int g = Convert.ToInt32(testArray);
int[] tests = new int[g];
//create loop to display all test scores
foreach (var item in tests)
Console.WriteLine(item);
}
}
}
}
int[] tests = new int[g];
Here you are assigning the size of the array given by the user rather than populating the array, you are missing the populating statement or query for that.
Because you create a new array inside of your loop that is the size of the "score" the user entered and then you loop over it. The values are all zero because when you create an array it is populated with the default value of the type, in this case 0. The second loop should be after the first one and you shouldn't be creating arrays inside of the first loop, just populating the original array (score) that you created to begin with.
Here's what you actually want. Note that you should index starting at 0 and not 1.
Write("How many test scores total: ");
string sSize = ReadLine();
int i = Convert.ToInt32(sSize);
int[] score = new int[i];
// create the loop of asking the test scores limited to the array sSize
for (int a = 0; a < i; a++)
{
Write("Please enter a test score " + (a + 1) + " from 0 to 100: ");
string testArray = ReadLine();
int g = Convert.ToInt32(testArray);
score[a] = g;
}
//create loop to display all test scores
foreach (var item in score)
Console.WriteLine(item);
You may also want to consider using int.TryParse so you can determine if the user enters an invalid value.
I think you copied pieces of code from different places and haven't been able to put them together properly.
You are creating an unwanted array here : int[] tests = new int[g];. And then trying to use it just worsens your situation.
Also, you haven't handled your indexes properly. When you are learning to program, using proper formatting and good variable names, will help you understand your own code (potentially put together from different places) a lot better - improving your "debugging" skills.
I have a "fixed" version of your code which should be self-explanatory
using System;
using static System.Console;
namespace ConsoleApp3 {
class Program {
static void Main(string[] args) {
// prompt user to ask how many test scores to build the size of the array
Write("How many test scores total: ");
string testCountStr = ReadLine();
int testCount = Convert.ToInt32(testCountStr );
int[] testScores = new int[testCount];
// create the loop of asking the test scores limited to the array sSize
for (int i = 0; i < testCount; i++) {
Write($"Please enter score for test {i + 1} from 0 to 100: ");
string scoreStr = ReadLine();
int score = Convert.ToInt32(scoreStr);
if (score > 100 || score < 0) {
//TODO: handle your error
WriteLine("Invalid score. Please try again");
--i;
continue;
}
testScores[i] = score;
}
WriteLine();
// create loop to display all test scores
for (int i = 0; i < testScores.Length; ++i)
WriteLine($"Your score for test {i + 1} is {testScores[i]}");
}
}
}
I have to write a simple program where you have to input student names and their average grade and then print out the highest average grade and who it belongs to. There are several topics here on how to find if value appears in array. The thing i'm struggling with is what to do if there are more than 1 students with the max average grade.
Here's what I have so far:
Console.WriteLine("Enter the overall count of students.");
int stuCount = Convert.ToInt32(Console.ReadLine());
string[] name = new string[stuCount];
double[] avg = new double[stuCount];
for (int i = 0; i < stuCount; i++)
{
Console.WriteLine("Enter the name of student # {0}.", i + 1);
name[i] = Console.ReadLine();
Console.WriteLine("Enter the average grade of {0}.", name[i]);
avg[i] = Convert.ToDouble(Console.ReadLine());
}
// Finding the max average
double maxAvg = avg[0];
for (int i = 1; i < stuCount; i++)
{
if (avg[i] > maxAvg)
{
maxAvg = avg[i];
}
}
// Displaying the max average
Console.WriteLine("The highest average grade is {0}.", maxAvg);
So, can I use Array.IndexOf() method to find multiple indices?
Thank you.
Consider using a class to represent the grades as so;
class Grade {
public String Name {get;set;}
public double Average {get;set;}
}
Then your code can be more like;
Console.WriteLine("Enter the overall count of students.");
int stuCount = Convert.ToInt32(Console.ReadLine());
List<Grade> allGrades = new List<Grade>();
for (int i = 0; i < stuCount; i++)
{
Console.WriteLine("Enter the name of student # {0}.", i + 1);
var name = Console.ReadLine();
Console.WriteLine("Enter the average grade of {0}.", name[i]);
var avg = Convert.ToDouble(Console.ReadLine());
Grade current = new Grade(){
Name = name,
Average = avg
};
allGrades.Add(current);
}
// Finding the max average
double maxAvg = allGrades.Max(g => g.Average);
var highestGrades = allGrades.Where(g => g.Average == maxAvg);
Console.WriteLine("The following Student(s) have the highest average grade:");
foreach(var grade in highestGrades){
// Displaying the max average
Console.WriteLine("Student: {0}. Grade: {1}.", grade.Name, grade.Average);
}
}
Another way is creating a class containing two properties (name and average grade), then generate a List and fill it in a for cycle. The next step is order by descending the list through average grade and select the first N element equals. After that you can simply print the result using a ForEach
After you've found the highest average just loop again through the averages array to see which ones are the max ones and print the stud using the index.
Console.WriteLine("The highest average grade is {0}.", maxAvg);
Console.WriteLine("The students with this average grade are");
for (int i = 0; i < stuCount; i++)
{
if (avg[i] == maxAvg)
{
Console.WriteLine(name[i]);
}
}
I was asked to solve a problem in C# to get the sum of 'n' user inputs from console, which is separated by space, in a single line.
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
arr[i] = Convert.ToInt32(Console.ReadLine());
sum += arr[i];
}
Console.WriteLine("{0}",sum);
How can I modify this code to get the expected output from the space separated input?
Also the values need to be stored in array.
Input:
5
1 2 3 4 5
Output:
15
int result = Console.ReadLine().Split().Select(int.Parse).Sum();
You'll of course have to handle any bad user input as needed.
Per your added requirements:
int[] items = Console.ReadLine().Split().Select(int.Parse).ToArray();
int sum = items.Sum();
You could do something like this:
int result = input.Split(' ').Take(n).Select(int.Parse).Sum();
But it seems to me that you could avoid asking the user for the count, and just add together all the lines that they typed in:
string input = Console.ReadLine();
int result = input.Split(' ').Select(int.Parse).Sum();
Console.WriteLine(result);
(Note that this code does no error checking.)
EDIT: It seems that you want to have an array of ints. In that case, you can do it like this (again, with no error checking):
using System;
using System.Linq;
namespace Demo
{
internal class Program
{
private static void Main()
{
int n = int.Parse(Console.ReadLine());
int[] arr = Console.ReadLine().Split(' ').Take(n).Select(int.Parse).ToArray();
int sum = arr.Sum();
Console.WriteLine("{0}", sum);
}
}
}
You need to use the split function in C#. When you read the line, you get the whole line. This means you're attempting to say "sum = 0 plus Convert.ToInt32('5 1 2 3 4 5')" which doesn't work.
You need an array of integers equal to
Console.ReadLine().Split(" ");
String.Split function:
https://msdn.microsoft.com/en-us/library/b873y76a.aspx
using arr[i] = Convert.ToInt32(Console.ReadLine()) inside the loop will cause the program to expect an input on a different line and not on a single line.
What you can do is to take the input as a string and then split based on space, which produces an array of the inputed values. You can then sum them
Considering the fact that Naveen seems a student, I think some more code can explain better and let him learn some more from this exercise:
I've made a sample that gets some numbers separated by space or an enter and when the user enters a T calculates the sum in a traditional way using a loop and using Linq. I'm sure there are plenty of better ways to do the same, but maybe something a little bit more structured can be better to begin understanding C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SumTheNumbers
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Insert the numbers separated by a space or [Enter]> key, when finished write T and press [Enter] and you will have the result");
List<int> values = new List<int>();
while (true)
{
string inputData = Console.ReadLine();
if (inputData.ToUpper().StartsWith("T")) break;
string[] svals = inputData.Split(' ');
for (int i = 0; i < svals.Length; i++)
{
int x = 0;
int.TryParse(svals[i], out x);
if (x != 0) values.Add(x);
}
}
//Traditional mode
int total = 0;
for (int i = 0; i < values.Count; i++)
{
total = total + values[i];
}
//Linq mode
int totalLinq = values.Sum();
Console.WriteLine("The sum is");
Console.Write("Total: ");
Console.WriteLine(total.ToString());
Console.Write("Total linq: ");
Console.WriteLine(totalLinq.ToString());
Console.WriteLine("Press a key to end...");
Console.ReadKey();
}
}
}
Below is the code I have written. What I'm trying to do is say enter 3 players names and the number of goals they have scored. Then the application should output the players name with the most goals and his number of goals. Also the application should output the lowest number of goals scored and the average goals scored. I can get the max goals scored but I cant figure out how to link the players name to goals scored. Also when I try and get the lowest goals scored I keep getting a value of 0. Thanks for the help.
{
string []player = new string[20];
double [] goalsScored = new double[20];
int numberOfPlayers;
Console.WriteLine("Enter the number of players:");
numberOfPlayers = int.Parse(Console.ReadLine());
for (int i = 0; i < numberOfPlayers; i++)
{
Console.WriteLine("Enter a players name:");
player[i]= Console.ReadLine();
Console.WriteLine("Goals scored so far this season");
goalsScored [i] = int.Parse(Console.ReadLine());
}
double max = goalsScored.Max();
double sum = goalsScored.Sum();
double average = sum / numberOfPlayers;
Console.WriteLine("Top player is: {0} with {1} goals", player, max);
Console.WriteLine("The average goals scored was:{0}",average);
double min = goalsScored.Min();
Console.WriteLine("The lowest goals scored was: {0}", min);
Console.ReadLine();
}
}
}
You should get the number of players first, then create your arrays using that number.
For example:
string[] player = new string[numberOfPlayers];
This may explain why you're always getting zero as the minimum score. If your array contains empty spots (if you entered less than 20 players/scores).
To "link" the player's name with his goals scored, you should really use a Player class so you can just have a single array of Player objects and use it for both scores and names. Then you'd end up with something like:
Console.WriteLine("Top player is: {0} with {1} goals", topPlayer.name, topPlayer.score);
You can't do this easily using arrays. You could use a Dictionary, which would allow you to look up a player's name and get the goals out. Indeed this sort of pairing of values is kind of what dictionaries are for:
//declare a dictionary string, int. The "string" is the name which we look up to get the
//second data point of "int", which will be the number of goals
Dictionary<string, int> playerGoals = new Dictionary<string, int>();
//add a couple of players, and their goal tally
playerGoals.Add("Gianfranco Zola", 44);
playerGoals.Add("Wayne Rooney", 77);
//use the string name to retrieve the int value
int rooneyGoals = playerGoals["Wayne Rooney"];
//rooneyGoals is now 77
Quicky whipped through your original code - something like this should work:
Dictionary<string, int> playerGoals = new Dictionarty<string, int>()'
int numberOfPlayers;
Console.WriteLine("Enter the number of players:");
numberOfPlayers = int.Parse(Console.ReadLine());
for (int i = 0; i < numberOfPlayers; i++)
{
Console.WriteLine("Enter a players name:");
string playerName = Console.ReadLine();
Console.WriteLine("Goals scored so far this season");
int goalsScored = int.Parse(Console.ReadLine());
playerGoals.Add(playerName, goalsScored);
}
double max = playerGoals.Values.Max();
double sum = playerGoals.Values.Sum();
double average = sum / numberOfPlayers;
Console.WriteLine("Top player is: {0} with {1} goals", player, max);
Console.WriteLine("The average goals scored was:{0}",average);
double min = playerGoals.Values.Min();
Console.WriteLine("The lowest goals scored was: {0}", min);
Console.ReadLine();
Linq solution :)
var players = player.Zip(goalsScored, (n,g) => new { Name = n, Goals = g });
var topGoalscorer = players.OrderByDescending(p => p.Goals).First();
var misser = players.OrderBy(p => p.Goals).First();
topGoalscorer.Goals // max goals
topGoalscorer.Name // player name
Using goals value:
string playerName = player[Array.IndexOf(goalsScored, min)];
Alternative solution:
int maxScoredGoals = -1;
string topGoalscorer = "";
for(int i = 0; i < numberOfPlayes; i++)
{
if (goalsScored[i] <= maxScoredGoals)
continue;
maxScoredGoals = goalsScored[i];
topGoalsocrer = player[i];
}
Suggestions: use single data structure to hold related data, i.e. player name and scored goals
public class Player
{
public string Name { get; set; }
public int Goals { get; set; }
}
Also use generic collection instead of arrays:
List<Player> players = new List<Player>();
Filling collection:
for(int i = 0; i < numberOfPlayers; i++)
{
var player = new Player();
player.Name = Console.ReadLine();
player.Goals = Int32.Parse(Console.ReadLine());
players.Add(player);
}
Getting top goalscorer:
var topGoalscorer = players.OrderByDescending(p => p.Goals).First().Name;
Or with MoreLINQ:
var topGoalscorer = players.MaxBy(p => p.Goals).Name;
You can use indirect sorting using Array.Sort(goalsScored, players); to keep a second array in sync while sorting an array. Other issues are addressed in the comments in the following code.
var numberOfPlayers = Int32.Parse(Console.ReadLine());
// Allocate the arrays with the correct size; in your code numberOfPlayers
// larger than 20 will cause trouble.
var players = new String[numberOfPlayers];
var goalsScored = new Double[numberOfPlayers];
// Fill the arrays as in your code.
// This will sort the goalsScored array and simultaneously
// bring the players array into the same order; that is called
// indirect sorting.
Array.Sort(goalsScored, players);
// The maximum number of goals scored and the associated player
// are now in the last array position.
var topPlayer = players.Last();
var maxNumberGoals = goalsScored.Last();
// You always got 0 because you always allocated arrays of size
// 20 and if you have less than 20 players all the superfluous
// entries in the array just have their default value of 0 making
// the minimum in the array 0.
var minNumberGoals = goalsScored.First();
var totalNumberGoals = goalsScored.Sum();
var averageNumberGoals = goalsScored.Average();
Unfortunately there is only out of the box support for this with arrays while it might be better to use lists and just add new players and scores until the users says that there are no more players to be entered instead of forcing the user to enter the number of players in advance.
Other answer suggest to use a more object oriented design and combine players and scored goals into a class or at least use a dictionary and I really suggest the same but I won't bother duplicating answers already here.
I have a method that needs to do a calculation based upon the length of an array. I am using the .length method for the calculation, but the method is doing arithmetic with the max length of the array (which I have declared as 10). This is the loop I am using to get data from the user. I know it isn't the ideal way to sort array data, but this is for a homework assignment, and it revolves around using the .Split method correctly (which isn't the problem I'm having).
for (int i = 0; i < MAX; i++)
{
Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
string input = Console.ReadLine();
if (input == "")
{
// If nothing is entered, it will break the loop.
break;
}
// Splits the user data into 2 arrays (integer and string).
string[] separateInput = input.Split();
name [i] = separateInput[0];
score [i] = int.Parse(separateInput[1]);
}
Here is the method I am using to calculate the average score:
static void CalculateScores(int[] score)
{
int sum = 0;
int average = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
average = sum / score.Length;
}
Console.WriteLine("The average score was {0}", average);
I am calling the method like this:
CalculateScores(score);
Edit: My arrays are declared:
int[] score = new int[MAX]; //MAX == 10.
string[] name = new string[MAX];
The CalculateScores method is doing the math as though score.Length is always 10, no matter how many different combinations of scores I input to the console. I can't figure out if it's because my loop to gather input has been done incorrectly, or my CalculateScores method is flawed. Thanks in advance.
Edit: to clarify, I am just confused at why I can't get the correct value out of CalculateScores.
Length always represents the size of the array, which if you've instantiated as 10, then it will always be 10, regardless of how many items you've filled.
There are lots of ways of solving your problem, but I'd go with the simple one of not using length in your calculation, but rather just storing the number of items in a separate variable:
int numItems = 0;
for(int i=0;i<MAX;i++)
{
Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
string input = Console.ReadLine();
if (input == "")
{
break; // if nothing is entered, it will break the loop
}
numItems++;
...
}
static void CalculateScores(int[] score, int numItems)
{
// don't use Length at all, use numItems instead
}
Arrays are generally used for fixed sized data, so the Length property reflects how many items the array can hold rather than the amount of elements in the array. The simplest fix would be to use a List(T), which is used for variadic data, instead.
// A nice abstraction to hold the scores instead of two separate arrays.
public class ScoreKeeper
{
public string Name { get; set; }
public int Score { get; set; }
}
var scores = new List<ScoreKeeper>();
for (int i = 0; i < MAX; i++)
{
Console.Write("Enter a name and a score for player #{0}: ", (i + 1));
string input = Console.ReadLine();
if (input == "")
{
// If nothing is entered, it will break the loop.
break;
}
// Splits the user data into 2 arrays (integer and string).
string[] separateInput = input.Split();
scores.Add(new ScoreKeeper { Name = separateInput[0], Score = int.Parse(separateInput[1]) });
}
static void CalculateScores(ICollection<ScoreKeeper> scores)
{
// We take advantage of Linq here by gathering all the
// scores and taking their average.
var average = scores.Select(s => s.Score).Average();
Console.WriteLine("The average score was {0}", average);
}
checking maually:
int sum = 0;
int average = 0;
int length;
for (int i = 0; i < MAX; i++) {
if(name[i]!=string.empty) {
sum += score[i];
length=i+1;
}
}
average = sum / length;
Console.WriteLine("The average score was {0}", average);