3
Answers

Console exam application

Photo of Mike

Mike

12y
2.1k
1
Hello, I just signed up for this forum because I'm an IT student and we program in C# and I'm extremely desperate! I've got my programming final on monday and I have no idea what to do. I've been absent for 7 weeks because I was diagnosed with hepatitis A. While I was absent my class has seen new parts of programming and I can't catch up!! :((
So this is what the program looks like that we have to make. What the class has seen so far are methods, randomizers, loops, if/else, arrays and arraylists

Step 1: You give 6 numbers between 1 and 42
Resized to 76% (was 666 x 340) - Click image to enlargePosted Image


Step 2: The program goes through all possible outcomes untill all 6 numbers appear at the same time
Resized to 76% (was 659 x 347) - Click image to enlargePosted Image


Step 3: It shows how many times the program needed to guess the right numbers

If somebody can make this for me, you have my eternal gratitude. Please, I am extremely desperate at the moment

Answers (3)

0
Photo of Vulpes
NA 98.3k 1.5m 12y
OK, if they don't have to be in a specific order, then that cuts down the possibilities by a factor of 720 though it'll still be slow to execute as you now need to sort the random numbers both for display purposes and to (efficiently) compare them with the numbers input.

'count' is just a variable name but, if you don't like it, change it to something else such as 'n'.

Console.ReadKey() is there to prevent the console from closing before you've had time to read the output. It can be replaced by Console.ReadLine() which requires the enter key, rather than any key, to be pressed.

int.TryParse can be replaced with with either int.Parse or Convert.ToInt32. However, if someone enters a non-number (such as 'abc'), the program will terminate with an exception.

Anyway, here's the revised code:

using System;

class Program
{
   static void Main()
   {
      int[] numbers = new int[6];
      string[] ordinals = {"eerste", "tweede", "derde", "vierde", "vijfde", "zesde"};
      Console.WriteLine("Geef je geluksgetallen (tussen 1 en 42");
      int n = 0;

      while (n < 6)
      {
         Console.WriteLine("Geef je {0} geluksgetal", ordinals[n]);
         numbers[n] = int.Parse(Console.ReadLine());
         if (numbers[n] < 1 || numbers[n] > 42)
         {
            Console.WriteLine("Moet tussen 1 en 42. Probeer het opnieuw.");
         }
         else
         {
            n++; 
         }          
      }   

      Array.Sort(numbers);

      ulong guess = 0;
      Random rand = new Random();
      int[] randomNumbers = new int[6];

      while (true)
      {
         for(int i = 0; i < 6; i++)
         {
            randomNumbers[i] = rand.Next(1, 43);
         }
       
         Array.Sort(randomNumbers); 
         for(int i = 0; i < 6; i++)
         {
            Console.Write("{0, -8}", randomNumbers[i]);
         }

         Console.WriteLine();
         guess++;
         bool areEqual = true;
 
         for(int i = 0; i < 6; i++)
         {
            if (randomNumbers[i] != numbers[i])
            {                       
               areEqual = false;
               break;
            } 
         }
                        
         if (areEqual)
         {
            Console.WriteLine("{0} gissingen waren nodig om de juiste cijfers weer te geven", guess);
            Console.ReadLine();
            break;
         }
      }
   }
}










Accepted
0
Photo of Mike
NA 3 2.1k 12y
No you've got it wrong, they don't have to be in specific order. Btw we have never seen "count" in ordians[count] and neither have we seen Console.Readkey or TryParse. So could you please remake it without those? I really appreciate this
0
Photo of Vulpes
NA 98.3k 1.5m 12y
Well, as it's not too difficult, I'll do it for you this one time.

Note that:

1. As the chances of guessing 6 numbers, between 1 and 42, in the correct order (I assume this is required?) are astronomic, the program is going to take a long time to run - particularly as all guesses have to be printed to the console.

2. I don't speak Dutch so if the translations are horrible, blame Google Translate :)

using System;

class Program
{
   static void Main()
   {
      int[] numbers = new int[6];
      string[] ordinals = {"eerste", "tweede", "derde", "vierde", "vijfde", "zesde"};
      Console.WriteLine("Geef je geluksgetallen (tussen 1 en 42");
      int count = 0;

      while (count < 6)
      {
         Console.WriteLine("Geef je {0} geluksgetal", ordinals[count]);
         bool isValid = int.TryParse(Console.ReadLine(), out numbers[count]);

         if (!isValid)
         {
            Console.WriteLine("Geen geldig getal. Probeer het opnieuw.");
         }
         else if (numbers[count] < 1 || numbers[count] > 42)
         {
            Console.WriteLine("Moet tussen 1 en 42. Probeer het opnieuw.");
         }
         else
         {
            count++; 
         }          
      }   

      ulong guess = 0; 
      Random rand = new Random();
      int[] randomNumbers = new int[6];

      while (true)
      {
         for(int i = 0; i < 6; i++)
         {
            randomNumbers[i] = rand.Next(1, 43);
            Console.Write("{0, -8}", randomNumbers[i]);
         }
         
         Console.WriteLine();
         guess++;
         bool areEqual = true;

         for(int i = 0; i < 6; i++)
         {
            if (randomNumbers[i] != numbers[i])
            {                       
               areEqual = false;
               break;
            } 
         }
                        
         if (areEqual)
         {
            Console.WriteLine("{0} gissingen waren nodig om de juiste cijfers weer te 
geven", guess);
            Console.ReadKey();
            break;
         }
      }
   }
}