question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked FranKDuc-4126 commented

initialize i and j in for loop

Hello,

I am trying to calculate a binomial:

  int X;
     int Y;
     int n = Convert.ToInt32(Console.ReadLine());   // number of horses    6
     int p = Convert.ToInt32(Console.ReadLine());   // number of try           2
     int i = 1;
     int k = 1;
        
     Console.WriteLine(" Entrez le nombre de cheveux partants:");
     Console.WriteLine(" Entrez le nombre de cheveux joués:");
          
       for (i, k; i < n && k < p; n++, k++  )
          
      {
       X *= i  / (i - k); 
       Y *= i  / (k  * (i – k) );
          
      }
          
       Console.WriteLine(" Dans l’ordre, une chance sur :{0}", X);
       Console.WriteLine(" Dans le désordre, une chance sur :{0}", Y);

I get an error: only assigment in my for loop.
Is there a way i can use two variables in the same loop?


Thank you

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered FranKDuc-4126 commented

The logic for factorial is fairly basic. Below is a lambda but the logic could also be a utility function.

 Func<int, int> factorial = (n) =>
 {
     int res = 1;
     while (n != 1)
     {
         res = res * n;
         n = n - 1;
     }
     return res;
 };
    
 int results = factorial(5);
 Console.WriteLine(results);

I am trying to calculate a binomial

Are you trying to graph a binomial? Or solve for X and Y where they cross an axis?



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Joe,

Its one of those problems you must solve in Main without utility function.

You enter the number of horses (6) on the race track and the number horses you pick (2). It suppose to return you have one chance on (??) of winning.

I came up with this but its still wrong.

 int X = 0;
      int Y = 0;
         
      int i;
      int k;
      int M = 1;
      int Mk = 1;
            
      Console.WriteLine(" Entrez le nombre de cheveux partants:");
         
              
       int n = Convert.ToInt32(Console.ReadLine());   // number of horses    6
            
         Console.WriteLine(" Entrez le nombre de cheveux joués:");
            
      int p = Convert.ToInt32(Console.ReadLine());   // number of try           2
            
         for (k = 1; k < p; k++  )
       {
    
            
           Mk *= k;
    
         }
            
         for (i = 1; i < n; i++)
       {
    
         M *= i;
    
          
     }
          X = M  / (M - Mk); 
        Y = M  / (Mk  * (M - Mk) );  
            
         Console.WriteLine(M);
         Console.WriteLine(Mk);
            
        Console.WriteLine(" Dans l’ordre, une chance sur :{0}", X);
        Console.WriteLine(" Dans le désordre, une chance sur :{0}", Y);

Thanks for the input

0 Votes 0 ·

The probability of one desired out come out of six possible outcomes is 1/6 = 16.7. Or a 1:6 chance of picking the winning horse. This assumes non-weighted outcomes.

I'm not sure why a binomial or factorial is required to solve this basic math problem.

0 Votes 0 ·

My problem is solve i found the issue. You are right i dont know either. Ask the teacher http://cours.pise.info/algo/enonces5.htm#En5.1 problem 5.11.
Its just a stupid programming task for an entry test.

0 Votes 0 ·
AgaveJoe avatar image
1 Vote"
AgaveJoe answered FranKDuc-4126 commented

The C# for...loop reference documentation explains and illustrates the for...loop usage.

The following code runs but the logic does not make sense.

 int X = 0;
 int Y = 0;
 int n = Convert.ToInt32(Console.ReadLine());   // number of horses    6
 int p = Convert.ToInt32(Console.ReadLine());   // number of try           2
 int i = 1;
 int k = 1;
    
 Console.WriteLine(" Entrez le nombre de cheveux partants:");
 Console.WriteLine(" Entrez le nombre de cheveux joués:");
    
 for (i = 0, k = 0; i < n && k < p; n++, k++)
 {
     X *= i / (i - k);
     Y *= i / (k * (i - k));
 }
    
 Console.WriteLine(" Dans l’ordre, une chance sur :{0}", X);
 Console.WriteLine(" Dans le désordre, une chance sur :{0}", Y);


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Or you can make it "for (; i < n && k < p; n++, k++)".

You can just use the separator ';' to omit the part you don't want in for statement. (Initializer, exit condition and statement to be executed after each loop)

0 Votes 0 ·

Thanks Cheong,

I saw i did a mistake in the for loop, its i++ and not n++ and i and k should be = to 1.

Still it should be i! / (i! - k!);

! for factorial

Is there a factorial in linq? i < n will return 1, 2, 3, 4 ,5 ,6

i was assuming that *= would multiply i and k in the equation.

Thanks for any input.

0 Votes 0 ·

read the Microsoft doc, everything is explained

0 Votes 0 ·