0

I have this function to initiate a two dimensional array:

static Array Matrix(int Rows, int Columns) 
{
    int[,] LotteryArray = new int[Rows,Columns];

    for (int i = 0; i < LotteryArray.GetLength(0); i++) 
    {
        for (int j = 0; j < LotteryArray.GetLength(1); j++)
        {
            LotteryArray[i, j] = RandomNum(1, 46);  
            Console.Write("{0,3},", LotteryArray[i, j]); 
        }
        Console.WriteLine();
    }
    return LotteryArray;
}

Then I have this which is supposed to give me a one dimensional array and see how many numbers in the winning array are in the matrix:

int RowNum = 1;
int Prediction = 0;
Console.WriteLine("Your winning numbers are!");
Console.WriteLine("------------------------");
int[] Winner = new int[6];
for (int i = 0; i < Winner.Length; i++)  //this loop is to initiate and print the winning numbers
{
    Winner[i] = RandomNum(1, 46);        //the numbers are supposed to be between 1 and 45, so i tell it to do it until 46 because the upper limit is exclusive
    Console.Write("{0,3},", Winner[i]);
}

Console.WriteLine();
Console.WriteLine("------------------------"); //these two lines are for aesthetics 
Matrix(Rows, Columns);


foreach (int i in Winner) 
{
    for (int j = 0; j<LotteryArray; j++)
    {
        if (Winner[i] == j)
        {
            Prediction++;
            if (j % 6 == 0) { RowNum++; }
        }
        Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
        RowNum = 1;
   }
}

It's telling me LotteryArray doesn't exist in the current context.

2 Answers 2

3

LotteryArray is a variable within another method. You cannot access it in the scope you are showing.

You can do get the return from your method into a variable and then use it.

var LotteryArray = Matrix(Rows, Columns);

foreach (int i in Winner) 
    {
        for (int j = 0; j<LotteryArray; j++)
        {
            if (Winner[i] == j)
            {
                Prediction++;
                if (j % 6 == 0) { RowNum++; }
            }
            Console.WriteLine("you got {0} correct prediction in row number {1}",Prediction,RowNum);
            RowNum = 1;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

ah i see thank you. i'm still get an unhandled exception because the index is out of bounds but i'll try and figure that one on my own
@Muffinator The debugger is your best friend for the index out of bounds error :) However, looking at your code, you may want to try this for (int i = 0; i<Winner.Length; i++)
0

LotteryArray is a variable declared in Matrix method, and is not visible outside.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.