I'm a big fan of having people that can check work no matter what it is. Whether it is essays, projects, or just habits (drawings), I like feedback to help me get better. Below is my code to a project I was working on for school. I got it pretty much finished and there is two errors inside of it. The errors are inside of Main() - GetInput() -----it is throwing two errors for the reference variables nameList and playerScore and saying that they are unassigned. Not sure why, because I have them assigned as far as I know of. however, any help with that would be great but I'm also looking more for feedback on if I could of done anything better or easier, while following the directions in the comments. I have to use arrays and I have to pass them by reference between the methods while passing the avg variable by value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneDial
{
class Program
{
// Display the player names and corresponding scores
static void DisplayPlayerData(ref string[] nameList, ref int[] playerScore, ref int count)
{
for (int i = 0; i < count; i++)
{
Console.WriteLine("{0} : {1}", nameList[i], playerScore[i]);
}
}
// Calculate the average score between all players and returns it by value to Main()
static void CalculateAverageScore(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
{
avg = playerScore.Average();
}
// Display all players whose score is below the average, with their corresponding scores
static void DisplayBelowAverage(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
{
Console.WriteLine("Players who scored below the average:");
for (int i = 0; i < count; i++)
{
if (playerScore[0] < avg)
Console.WriteLine("{0}:{1}", nameList, playerScore);
count++;
}
}
// Get player names and their scores and stores them into array for an unknown number of players up to 100
static void InputData(ref string[] nameList, ref int[] playerScore, ref int count)
{
string userInput;
nameList = new string [100];
playerScore= new int [100];
do
{
Console.Write("\nEnter a players name: ");
userInput = Console.ReadLine();
if (userInput != "Q" && userInput != "q")
{
nameList[0] = Console.ReadLine();
++count;
}
else break;
Console.WriteLine("Enter {0}'s score:", userInput);
playerScore[0] = Convert.ToInt32(Console.ReadLine());
} while (userInput != "Q" && userInput != "q");
}
//Declare variables for number of players and average score and two arrays of size 100 (one for names, one for respective scores
//Calls functions in sequence, passing necessary parameters by reference
static void Main(string[] args)
{
string[] nameList;
int[] playerScore;
int count = 0;
double avg = 0;
//InputData(), passing arrays and number of players variable by reference
//******nameList and playerScore are throwing errors; use of unassigned local variables********
InputData(ref nameList, ref playerScore, ref count);
//DisplayPlayerData(), passing arrays and number of players by reference
DisplayPlayerData(ref nameList, ref playerScore, ref count);
//CalculateAverageScore(), passing arrays and number of players by reference. Store returned value in avg variable
CalculateAverageScore(ref nameList, ref playerScore, ref count, avg);
//DisplayBelowAverage(), passing arrays and number of players variable by reference, passing average variable by value
DisplayBelowAverage(ref nameList, ref playerScore, ref count, avg);
}
}
}