0

I am making a lottery game that asks the user for 10 numbers and then check it against random numbers that i have created in an array. I need to compare the two but I am not allowed to use the contains method.

I think I need to use a foreach loop to compare the arrays but really I am at a loss of what to do. I have been piecing it together from the little I know and would like to know if I am on the right track.

Is a foreach loop the correct way to compare the two arrays?

This is my code so far.

using System;

namespace lotto2
{
class Program
{
    static void Main(string[] args)
    {

        //an array named "input" to hold the users' 10 guesses
        int[] inputs = new int[10];

        //an array named "lotNum" to hold 10 random numbers
        int[] lotNums = new int[10];

        //a for loop to loop over the inputs array. each loop will ask the user for a number
        Console.WriteLine("Enter your 10 lottery numbers one at a time. The numbers must be between 1 and 25.");
        for (int i = 0; i < inputs.Length; i++)
        {
            inputs[i] = Convert.ToInt32(Console.ReadLine());
        }

        //a random number generator  
        Random ranNum = new Random();

        //loop to call the random generator 10 times and store 10 random numbers in the "lotNum" array
        for (int i = 0; i < 10; i++)
        {
            lotNums[i] = ranNum.Next(1, 26); //chooses random numbers between 1 and 25
        }

        //writes out the randomly generated lotto numbers
        Console.Write("\nThe lottery numbers are: ");
        for (int i = 0; i < 10; i++)
        {
            Console.Write("{0}  ", lotNums[i]);
        }

        //loop for checking users inputs against random generated numbers..
        //foreach loop maybe?
        foreach (var input in lotNums)
        {
        }

        //print out if there are any matches, which numbers matched

        //declared integer for the correct numbers the user guessed
        int correct;

        //end progam
        Console.WriteLine("\n\nPress any key to end the program:");
        Console.ReadKey();
    }
}

}

8
  • What's wrong with using LINQ? Is this homework? Commented Sep 10, 2018 at 1:23
  • Yes, I am taking an online course and this is an assignment. Commented Sep 10, 2018 at 1:31
  • And is that is why you can't use LINQ? Commented Sep 10, 2018 at 1:34
  • 1
    Consider sorting them first. Another option would be to write your own Contains method. Commented Sep 10, 2018 at 1:34
  • In this assignment I am not allowed to use LINQ. Commented Sep 10, 2018 at 1:42

4 Answers 4

1

Here's a program that correctly does what you want. It even ensures that you don't have duplicate lotto numbers.

void Main()
{
    const int count = 10;
    const int max = 25;

    //an array named "input" to hold the users' 10 guesses
    int[] inputs = new int[count];

    //a for loop to loop over the inputs array. each loop will ask the user for a number
    Console.WriteLine("Enter your {0} lottery numbers one at a time. The numbers must be between 1 and {1}.", count, max);
    for (int i = 0; i < inputs.Length; i++)
    {
        inputs[i] = Convert.ToInt32(Console.ReadLine());
    }

    //a random number generator  
    Random ranNum = new Random();

    //an array named "allNums" to hold all the random numbers
    int[] allNums = new int[max];
    for (int i = 0; i < allNums.Length; i++)
    {
        allNums[i] = i + 1;
    }

    //shuffle
    for (int i = 0; i < allNums.Length; i++)
    {
        int j = ranNum.Next(0, allNums.Length);
        int temporary = allNums[j];
        allNums[j] = allNums[i];
        allNums[i] = temporary;
    }

    //an array named "lotNum" to hold 10 random numbers
    int[] lotNums = new int[count];

    Array.Copy(allNums, lotNums, lotNums.Length);

    //writes out the randomly generated lotto numbers
    Console.Write("\nThe lottery numbers are: ");
    for (int i = 0; i < lotNums.Length; i++)
    {
        Console.Write("{0}  ", lotNums[i]);
    }

    int correct = 0;
    Console.Write("\nThe correct numbers are: ");
    for (int i = 0; i < lotNums.Length; i++)
    {
        for (int j = 0; j < inputs.Length; j++)
        {
            if (lotNums[i] == inputs[j])
            {
                Console.Write("{0}  ", lotNums[i]);
                correct++;
            };
        }
    }

    Console.Write("\nYou got {0} correct. ", correct);

    Console.WriteLine("\n\nPress any key to end the program:");
    Console.ReadLine();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're on the right way.

My implementation would be:

foreach (var input in inputs)
{
    for (int i = 0; i < lotNums.Length; i++){
        if(input == lotNums[i]){
            Console.WriteLine(lotNums[i]);
        }
    }
}

This will compare every number of the input array with the lottery array. I'm printing every match, but you can set a variable to True if it finds a match or add every matching number into an array if you need it.

1 Comment

This is great. Of course it something so simple. I kept thinking i needed it to begin "foreach (var input in lotnums)" for some reason. I should've taken a break and come back to it.
0

This is what I have tried.I hope it makes sense?

static void LottoMethod(int[] randNums,int[] userNums)
    {
        Console.WriteLine("Guess 10 numbers");
        for(int i = 0; i <= userNums.Length-1; i++)
        {
            userNums[i] = Int32.Parse( Console.ReadLine());
        }
        Console.WriteLine("The numbers you entered: ");
        foreach(int k in userNums)
        {
            Console.Write(k+"   ");
        }
        //generate 10 numbers randomly
        Random rnds = new Random();
        for(int k = 0; k <= randNums.Length - 1; k++)
        {
            randNums[k] = rnds.Next(1, 26);
        }
        Console.WriteLine("Random Numbers");
        foreach(int i in randNums)
        {
            Console.Write(i + "   ");
        }
        int correctNums = 0;
        //Check if random numbers correspond with entered numbers
        try
        {
            for(int i = 0; i <= randNums.Length-1; i++)
            {
                for(int j = 0; j <= userNums.Length-1; j++)
                {
                    if (randNums[i] == userNums[j])
                    {
                        correctNums++;
                    }
                }
            }
            Console.WriteLine($"There are {correctNums} numbers ");
        }
        catch(Exception e) {
            throw new Exception(e.ToString());
        }
    }

Comments

0

You have to calculate intersection of two sequences. You have three options:

  • Double foreach loop. This is something to avoid as it has time complexity O(m*n). It it not a problem for 10 items, but we should make programs that scale.
  • Using hash join. You can use HashSet for this and it would be my preferred method. But as it inherently implies using Contains, it is not the option here.
  • Merging sorted sequences. This would be the way to go here.

The program is rather self explanatory, it produces and intersects two random sequences.

static Random rnd = new Random((int)DateTime.Now.Ticks);

static int[] GetRandomArray(int arrSize, int minNumber, int maxNumber)
{
    int[] tmpArr = new int[maxNumber - minNumber + 1];
    for (int i = 0; i < tmpArr.Length; ++i)
    {
        tmpArr[i] = i + minNumber; // fill with 1, 2, 3, 4,...
    }

    int[] ret = new int[arrSize];
    for (int i = 0; i < ret.Length; ++i)
    {
        int index = rnd.Next(tmpArr.Length - i); //choose random position
        ret[i] = tmpArr[index];
        tmpArr[index] = tmpArr[tmpArr.Length - 1 - i]; //fill last of the sequence into used position
    }

    return ret;
}

static IEnumerable<int> GetMatches(int[] a, int[] b)
{
    Array.Sort(a);
    Array.Sort(b);

    for (int i = 0, j = 0; i < a.Length && j < b.Length;)
    {
        if (a[i] == b[j])
        {
            yield return a[i];
            ++i;
            ++j;
        }
        else if (a[i] > b[j])
        {
            ++j;
        }
        else
        {
            ++i;
        }
    }
}

static void Main(string[] args)
{
    var a = GetRandomArray(5, 3, 7);
    var b = GetRandomArray(10, 1, 25);

    Console.WriteLine("A: " + string.Join(", ", a));
    Console.WriteLine("B: " + string.Join(", ", b));
    Console.WriteLine("Matches: " + string.Join(", ", GetMatches(a, b)));
    Console.ReadKey();
}

The result is something like:

A: 7, 4, 6, 3, 5
B: 17, 1, 8, 14, 11, 22, 3, 20, 4, 25
Matches: 3, 4

You can think about what would happen if one or both of the sequences contain duplicities.

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.