How can I compare each number in 2 different arrays with each other?

I’m working on a lottery application:

  • The user enters 5 numbers between 1-25 that get saved to an array
  • Another array gets filled with 5 random numbers between 1-25

I’ve gotten to a point where I can print out both of the arrays, but I have no clue on how to compare them to see if the user won, and on how many numbers.

namespace ConsoleApp14
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] userBingo = new int[5];

            Console.WriteLine("Var vänligt och skriv in 5 tal mellan 1-25: ");

            for (int i = 0; i < userBingo.Length; i++)
            {
                int inputBingo;

                do
                {
                    string input = Console.ReadLine();

                    if (!int.TryParse(input, out inputBingo))
                    {
                        Console.WriteLine("Felaktig inmatning! Var vänlig och försök igen.");
                        continue;
                    }

                    if (inputBingo < 1 || inputBingo > 25)
                    {
                        Console.WriteLine("Var vänlig och skriv in ett tal mellan 1-25. ");
                        continue;
                    }

                    userBingo[i] = inputBingo;
                    break;
                } while (true);
            }

            Console.WriteLine("Här är dina tal!");            
                       
            foreach (int i in userBingo)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();
            Console.WriteLine("Och de vinnande talen är: ");

            int[] winningNumber = new int[5];
            Random random = new Random();

            for (int i = 0; i < winningNumber.Length; i++)
            {
                winningNumber[i] = random.Next(1,26);
            }

            foreach (int i in winningNumber)
            {
                Console.WriteLine(i);
            }

        }
    }
}

I’m not actually sure what to try, I’ve only recently started learning about arrays and know how to create them, but not compare them.

2

If order matters you can use sequenceEquals

userBingo.SequenceEquals(winningNumber);

If numbers can be in any order you could first order both lists to ensure consistent ordering:

Array.Sort(userBingo);
Array.Sort(winningNumber);
userBingo.SequenceEquals(winningNumber);

If numbers are guaranteed to be unique you could also use HashSet:

userBingo.HashSet().SetEquals(winningNumber);

Do note that your teacher may expect something you write yourself, and not simply using library methods to do all the heavy lifting. One way to do order insensitive comparing yourself is:

  1. Declare an array of length 26, lets call it histogram
  2. Loop thru both arrays, use the array value as the histogram index and increment that value, i.e. histogram[userBingo[i]]+=1
  3. Loop thru the histogram, if any values are odd the arrays are not equal.

To compare the two arrays and determine if the user has won, and on how many numbers, you can
Create a function that checks if a number exists in an array.

static bool IsNumberInArray(int[] array, int number)
{
    foreach (int n in array)
    {
        if (n == number)
        {
            return true;
        }
    }

    return false;
}

Iterate through the userBingo array and check if each number exists in the winningNumber array using the function created in step 1.
Keep a count of the matched numbers.

// Check how many numbers from userBingo exist in winningNumber
foreach (int userNumber in userBingo)
{
    if (IsNumberInArray(winningNumber, userNumber))
    {
        matchedNumbers++;
    }
}

After iterating through all the user’s numbers, check the count and determine if the user has won based on your game rules.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật