Search code examples
c#variable-assignment

School assignment "lottery balls" that I don’t understand (vectors)


I have an assignment for school that is pretty vague. I have asked another forum and they didn't understand the instructions either and told me to mail my instructor. My instructor doesn't answer my mail and the task is due in two days (27th April). I have done what I think is correct, but I am not sure.

Here are the assignment instructions:

**The challenge in the task is that you must create a vector that holds ten values. The program must then iterate (repeat) for each position in the vector. This is done with a for loop. For each "turn" in the loop, you should let the user enter a number. This number will then be placed in the next free position in the vector.

For this purpose, you use a for loop.

In the next step, you must iterate through the vector that is filled with numbers.

If a number in the vector matches the entered number, you get "bingo".

The challenge can be made tougher (for grade criteria that correspond to the requirements for grade C) if you instead also create a vector for the random numbers. You will therefore work with several "balls" (random numbers). To test all the values ​​in one vector with all the values ​​in another, you will have to work with something called nested loops.

The most difficult challenge is if you implement a two-dimensional vector for the bingo tile (for grading criteria that correspond to the requirements for grade A). This vector can then better simulate a bingo tile.**

I don't really understand. Am I supposed to make two arrays? One that the user puts numbers in and one that gets 10 random numbers from 1 to 21?

After both are full with numbers, I do a for loop 10 times and compare one number in vector1 with a number in vector2. For C level. I am trying to do A level, but first, I've got to understand what to even do here.


Solution

  • You can create a multi-dimensional array to hold the numbers in a rectangular grid, generate random numbers, and add to the array. Then loop through the user input and add each number entered into a second multi-dimensional array, loop, and check if any entries are equal. Then output, bingo.

    // Declare the multi-dimensional array to hold 
    // the random numbers in a grid of size 5 by 2
    var arr1 = new int[5, 2];
    
    // Generate random numbers and assign to each index in the array
    for(int i=0; i<5; i++) {
      for(int j=0; j<2; j++) {
        // Generate a random number
        int randomNumber = random.Next(1, 22);
        arr1[i, j] = randomNumber;
      }
    }
    
    // Create an equal muli-dimensional array of size 5 by 2
    // and fill each index with values from the user
    var arr2 = new int[5, 2];
    for(int i=0; i<5; i++) {
      for(int j=0; j<2; j++) {
        // Get input from user
        arr2[i, j] = Int32.Parse(Console.ReadLine());
      }
    }
    
    // Compare elements in the same index in both 
    // arrays and output bingo if they are equal
    for(int i=0; i<5; i++) {
      for(int j=0; j<2; j++) {
        // Get input from user
        var eq = arr1[i, j] == arr2[i, j];
        if(eq)
          Console.WriteLine("Bingo!");
      }
    }