Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- public static void PrintHelp()
- {
- Console.WriteLine("Coding Conundrums Problem 1.2 Solution: Number Shuffle");
- Console.WriteLine("------------------------------------------------------");
- Console.WriteLine("Problem Description:");
- Console.WriteLine(" I want a program that will print outthe numbers 1,2,3,4,5,6,7,8,9 in a");
- Console.WriteLine(" shuffled order. The order must be different each time the program runs. Note");
- Console.WriteLine(" that the same number must be different each time. It should be possible to");
- Console.WriteLine(" extend this to work with 52 numbers, in which case I can make a shuffled");
- Console.WriteLine(" deck of cards.");
- Console.WriteLine();
- Console.WriteLine("Use it like this: ");
- Console.WriteLine(" numbershuffle.exe <count-to>");
- Console.WriteLine();
- Console.WriteLine("Paramters:");
- Console.WriteLine(" <count-to>: The number to count up to when generating the initial sequence of numbers.");
- }
- public static void Main(string[] args)
- {
- if(args.Length != 1)
- {
- PrintHelp();
- return;
- }
- int numberCount;
- if(!int.TryParse(args[0], out numberCount))
- {
- PrintHelp();
- return;
- }
- int[] numbers = new int[numberCount];
- for(int i = 0; i < numberCount; i++)
- {
- numbers[i] = i + 1;
- }
- int swapsCount = numberCount * 2;
- int temp, a, b;
- Random random = new Random();
- for(int i = 0; i < swapsCount; i++)
- {
- a = random.Next(numbers.Length);
- b = random.Next(numbers.Length);
- temp = numbers[a];
- numbers[a] = numbers[b];
- numbers[b] = temp;
- }
- for(int i = 0; i < numbers.Length; i++)
- {
- Console.Write("{0} ", numbers[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement