Winning chance in lottery.

Bodor Arianna

New member
Joined
Feb 3, 2021
Messages
2
Programming Experience
Beginner
I have a problem with my code.

The exercise is the following:

You participate at the lottery 6/49 with only one winning variant(simple) and you want to know what odds of winning you have:

-at category I (6 numbers)

-at category II (5 numbers)

-at category III (4 numbers)

Write a console app which gets from input the number of total balls, the number of extracted balls, and the category, then print the odds of winning with a precision of 10 decimals if you play with one simple variant.

For example if I input:

49

6

I

The result is ok, but when I input:

45

15

III

The result is not what I expected.

It is 0.2162171866, but it has to be 0.0000001324.

Any suggestions what is wrong with my code? How can I simplify the code or correct it?

This is my code :


C#:
using System;
using System.Collections.Generic;

namespace FirstApplication
{
    class Program
    {
        public static void Main()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            int k = Convert.ToInt32(Console.ReadLine());
            string category = Console.ReadLine();
            decimal total = 0;


            switch (category)
            {
                case "I":
                    for (int z = 6; z <= 6; ++z)

                        total = bc(k, 6) * bc(n - k, k - 6) / bc(n, k);
                    decimal totalx = Convert.ToDecimal(total.ToString("0.0000000000"));
                    Console.WriteLine(totalx);
                    return;

                case "II":
                    for (int z = 4; z <= 5; ++z)

                        total = bc(k, z) * bc(n - k, k - z) / bc(n, k);
                    decimal totalx1 = Convert.ToDecimal(total.ToString("0.0000000000"));
                    Console.WriteLine(totalx1);


                    return;
                case "III":
                    for (int z = 4; z <= 4; ++z)

                        total = bc(k, z) * bc(n - k, k - z) / bc(n, k);
                    decimal totalx2 = Convert.ToDecimal(total.ToString("0.0000000000"));
                    Console.WriteLine(totalx2);

                    return;
            }
            Console.Read();
        }

        public static Dictionary<(int N, int K), decimal> knownValues = new Dictionary<(int N, int K), decimal>();
        public static decimal bc(int n, int k)
        {
            var key = (n, k);
            if (!knownValues.ContainsKey(key))
            {
                if (k == 0 || k == n)
                {
                    knownValues.Add(key, 1);
                }
                else
                {
                    knownValues.Add(key, bc(n - 1, k - 1) + bc(n - 1, k));
                }
            }
            return knownValues[key];
        }
    }
}
 
The result is not what I expected.
As a de4veloper, you don't just look at the result. You need to debug your code. Set a breakpoint at the top of the code and then step through it line by line, examining the state at every step. You should know exactly what you expect to happen before you step and you can see whether it did happen after the step. As soon as reality differs from expectation, you have found an issue and you can investigate that specifically. If you still can't work out why or what to do, at least you can provide us with much more relevant information.
 
As a de4veloper, you don't just look at the result. You need to debug your code. Set a breakpoint at the top of the code and then step through it line by line, examining the state at every step. You should know exactly what you expect to happen before you step and you can see whether it did happen after the step. As soon as reality differs from expectation, you have found an issue and you can investigate that specifically. If you still can't work out why or what to do, at least you can provide us with much more relevant information.
I know, finally I solved it. :)
 
i have the same problem, could you give me a hint on how to make the dictionary work? when i copy paste it in my compiler it gives INDENTIFIER EXPECTED error and many more.
If it didn't work then you did it wrong. If you don't show us what you did then we can't tell you what's wrong with it. If you get an error message telling you that an identifier was expected then obviously you didn't provide an identifier where one was expected so the solution is to provide an appropriate identifier. That's all we can tell you unless you provide the actual code that generated the error. Also be sure to point out exactly where in the code the error was generated.
 
If it didn't work then you did it wrong. If you don't show us what you did then we can't tell you what's wrong with it. If you get an error message telling you that an identifier was expected then obviously you didn't provide an identifier where one was expected so the solution is to provide an appropriate identifier. That's all we can tell you unless you provide the actual code that generated the error. Also be sure to point out exactly where in the code the error was generated.
the code is an exact full copy paste of Bodor's code, i have been trying to do it my own way and cant seem to find out why and tought of trying out someone elses solution in order to get a hint to fix mine. The problem that i have with my code is that for case I and II it works fine but for case III it just stays at the console screen and then im assuming it times out after taking too long. for input 49 6 III it works and give the right result but if i input 45 15 III it just does nothing.
C#:
using System;

namespace ConsoleApplication35
{
    class Program
    {
        static int bc(int n, int k)
        {
            if (k == 0 || k == n)
            {
                return 1;
            }

            return bc(n - 1, k - 1) + bc(n - 1, k);
        }
        public static void Main()
        {

            int n = Convert.ToInt32(Console.ReadLine());
            int k = Convert.ToInt32(Console.ReadLine());
            string category = Console.ReadLine();

            int b = 0;
            switch (category)
            {
                case "I":
                    b = 6;
                    decimal sol = Convert.ToDecimal(bc(k, b)) * Convert.ToDecimal(bc(n - k, k - b)) / Convert.ToDecimal(bc(n, k));
                    decimal round = Math.Round(sol, 10);
                    Console.WriteLine(round);
                    break;

                case "II":
                    b = 5;
                    if (k == b)
                    {
                        b = 4;
                    }
                    decimal sol1 = Convert.ToDecimal(bc(k, b)) * Convert.ToDecimal(bc(n - k, k - b)) / Convert.ToDecimal(bc(n, k));
                    decimal round1 = Math.Round(sol1, 10);
                    Console.WriteLine(round1);
                    break;

                case "III":

                    b = 4;
                    if (k == b)
                    {
                        b = 3;
                    }
                    double w = Convert.ToDouble(bc(k, b));
                    double l = Convert.ToDouble(bc(n - k, k - b));
                    double t = Convert.ToDouble(bc(n, k));
                    double sol2 = w * (l / t);
                    double round2 = Math.Round(sol2, 10);
                    Console.WriteLine(round2);
                    break;
            }
        }
    }

}
 
Last edited:
The problem that i have with my code is that for case I and II it works fine but for case III it just stays at the console screen and then im assuming it times out after taking too long.
That's not what you said earlier, so there's that. As for the current issue, don't assume. Debug your code. Set a breakpoint, step through the code line by line and examine the state at each step to see what actually happens and how it compares to what you expect.
 
Back
Top Bottom