1

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);





        }
    }
}
user1174357
  • 63
  • 1
  • 3
  • 11

3 Answers3

1

From a quick glance you have used ref where you should be using out. I think InputData should pass the parameters as out not ref as you are setting them in there. The other methods aren't changing these arrays so don't need to be passed as ref.

Mike Miller
  • 16,195
  • 1
  • 20
  • 27
  • Thank you much for the reply, I will definatly check into this when I get off work and back home. :D The comments are taken directly from the pseudocode that was provided with the lab. – user1174357 Feb 07 '12 at 16:30
1

You are currently passing your variables to InputData using ref. ref requires your variable to be initialized before, i.e. string[] nameList = new string[N]; must be called before using ref for your variable nameList.

As you wrote nameList = new string[100]; in your InputData method, you could replace ref by out:

static void InputData(out string[] nameList, out int[] playerScore, ref int count)
...
InputData(out nameList, out playerScore, ref count);
  • out doesn't require initialization, it's more like "I don't care about the previous value of this variable, it'll be overwritten by my method, and the variable is purely here because it is the output of my method".
  • ref is very similar to out, but it's more like "Use the value of the variable, modify it, and returns it as the output of my method"

See here and here for a more complete "ref vs out" explanation.

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Interesting, I'll have to check that when I get back home to the laptop. All the comments in the code (except my error comment) are directly from the pseudocode that was provided with this lab. This is why I was trying for the ref route. I will definatly crack this when i get home. – user1174357 Feb 07 '12 at 16:29
  • I have corrected your initial error just like Ken2k mentioned ..Out does not require initialization .. but if you were wanting to initialize the string[] or int[] you could use the empty initializer {} it's awesome for populating arrays at runtime when you don't know the initial length that you want to declare it as ..also keeps you away from Resize(ref...) and newing all the time.. it's just my preference.. good luck with your lab work – MethodMan Feb 07 '12 at 16:55
1

Here is a simple fix for your problem since you are newing the string[] and the int[] in your InputData method you can decare the

        string[] nameList = {}; //in your Main
        int[] playerScore = {}; // in your Main
        int count = 0;

as dynamic Arrays.. not many developers are aware that you can do this but this should fix your issue.. I just tested this on my local my self and it compiled

You could have also declared it as out but I don't see a need to do that as long as you declare the string[] and initialize it a {} that will fix your problem and I've never had any coding or progamming issues using it.. especially if you need to size an array and you don't know how big it needs to be.. it's quick and clean and fairly easy to understand.. happy Coding..!

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • That is a new method. I've seen it before with the{} but that was when you were already filling it at the same time {5, 6, 7, 8} I was under the impression that you had to declare a size for an array in C#. – user1174357 Feb 07 '12 at 16:51
  • no this is what they call a Dynamic array.. not many developers are aware of this construct.. lol how all is well.. – MethodMan Feb 16 '12 at 15:50