Exemplo n.º 1
0
        /// <summary>
        /// Reads in two command-line arguments lo and hi and prints n uniformly
        /// random real numbers in [lo, hi) to standard output.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            // command-line arguments
            int n = int.Parse(args[0]);

            // for backward compatibility with Intro to Programming in Java version of RandomSeq
            if (args.Length == 1)
            {
                // generate and print n numbers between 0.0 and 1.0
                for (int i = 0; i < n; i++)
                {
                    double x = StdRandom.Uniform();
                    StdOut.Println(x);
                }
            }
            else if (args.Length == 3)
            {
                double lo = double.Parse(args[1]);
                double hi = double.Parse(args[2]);

                // generate and print n numbers between lo and hi
                for (int i = 0; i < n; i++)
                {
                    double x = StdRandom.Uniform(lo, hi);
                    StdOut.Printf("{0:f2}\n", x);
                }
            }
            else
            {
                throw new ArgumentException("Invalid number of arguments");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Interactive test of basic functionality.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            StdOut.Print("Type a string: ");
            string s = ReadString();

            StdOut.Println("Your string was: " + s);
            StdOut.Println();

            StdOut.Print("Type an int: ");
            int a = ReadInt();

            StdOut.Println("Your int was: " + a);
            StdOut.Println();

            StdOut.Print("Type a boolean: ");
            bool b = ReadBool();

            StdOut.Println("Your boolean was: " + b);
            StdOut.Println();

            StdOut.Print("Type a double: ");
            double c = ReadDouble();

            StdOut.Println("Your double was: " + c);
            StdOut.Println();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Unit tests the methods in this class.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            int n = int.Parse(args[0]);

            if (args.Length == 2)
            {
                Seed = long.Parse(args[1]);
            }
            double[] probabilities = { 0.5, 0.3, 0.1, 0.1 };
            int[]    frequencies   = { 5, 3, 1, 1 };
            string[] a             = "A B C D E F G".Split(' ');

            StdOut.Println($"seed = {Seed}");
            for (int i = 0; i < n; i++)
            {
                StdOut.Printf("{0, 2}", Uniform(100));
                StdOut.Printf("{0,10:f5}", Uniform(10.0, 99.0));
                StdOut.Printf("{0, 7}", Bernoulli(0.5));
                StdOut.Printf("{0, 9:f5}", Gaussian(9.0, 0.2));
                StdOut.Printf("{0, 3}", Discrete(probabilities));
                StdOut.Printf("{0, 3}", Discrete(frequencies));
                StdOut.Printf("{0, 13}", Uniform(100000000000L));
                StdOut.Print("  ");
                Shuffle(a);
                foreach (string s in a)
                {
                    StdOut.Print(s);
                }
                StdOut.Println();
            }
        }
Exemplo n.º 4
0
        private static void Test(string[] args)
        {
            int[] whitelist = In.ReadInts(args[0]);

            Array.Sort(whitelist);

            while (!StdIn.IsEmpty)
            {
                int key = StdIn.ReadInt();
                if (Rank(key, whitelist) == -1)
                {
                    StdOut.Println(key);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Reads in a sequence of real numbers from standard input and prints
        /// out their average to standard output.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            int    count = 0;         // number input values
            double sum   = 0.0;       // sum of input values

            // read data and compute statistics
            while (!StdIn.IsEmpty)
            {
                double value = StdIn.ReadDouble();
                sum += value;
                count++;
            }

            // compute the average
            double average = sum / count;

            // print results
            StdOut.Println("Average is " + average);
        }