Beispiel #1
0
        static void Exo01_07()
        {
            int numbersToAdd = 0, add = 0, i;

            HelpProgram.readInteger(ref numbersToAdd);
            int[] tab = new int[numbersToAdd];
            for (i = 0; i < tab.Length; i++)
            {
                tab[i] = i + 1;
            }
            foreach (var number in tab)
            {
                add += number;
            }
            Write("The addition of the first {0} integers equals {1}", numbersToAdd, add);
            ReadKey();
        }
Beispiel #2
0
        static void Exo01_06()
        {
            int i = 0, big = 0;

            int[] tab = new int[3];
            foreach (var cell in tab)
            {
                WriteLine("Write a number: ");
                HelpProgram.readInteger(ref tab[i]);
                if (tab[i] > big)
                {
                    big = tab[i];
                }
                i++;
            }
            WriteLine("The biggest number is {0}", big);
            ReadKey();
        }
Beispiel #3
0
        static void Exo1_04()
        {
            int   a, b, size, choice;
            float c;

            WriteLine("---------------------");
            WriteLine("  AVAIBLE EXCERCISES ");
            WriteLine("---------------------");
            WriteLine("1. Static method with two arguments that returns the addition of the two values.");
            WriteLine("2. Static method with three arguments, one of which is an 'out' integer, doesn't return anything.");
            WriteLine("3. Static method that takes an array as arguments and returns the addition of the array's elements.");
            WriteLine("4. Static method that takes an undetermined nunmber of integers as arguments and returns the addition of the arguments.");
            WriteLine("\nWhich excercise do you want to execute?");
            while (!int.TryParse(ReadLine(), out choice) || choice > 4 || choice < 0)
            {
                WriteLine("Wrong input. Please, enter a valid option.");
            }
            switch (choice)
            {
            case 1:
                Write("\n\nEnter A: ");
                while (!int.TryParse(ReadLine(), out a))
                {
                    WriteLine("Wrong input. Please, enter an integer.");
                }
                Write("Enter B: ");
                while (!int.TryParse(ReadLine(), out b))
                {
                    WriteLine("Wrong input. Please, enter an integer.");
                }
                c = HelpProgram.Exo1_04_01(a, b);
                Write("C = " + c);
                ReadKey(true);
                break;

            case 2:
                Write("\n\nEnter A: ");
                while (!int.TryParse(ReadLine(), out a))
                {
                    WriteLine("Wrong input. Please, enter an integer.");
                }
                Write("Enter B: ");
                while (!int.TryParse(ReadLine(), out b))
                {
                    WriteLine("Wrong input. Please, enter an integer.");
                }
                HelpProgram.Exo1_04_02(a, b, out c);     //If we passed a reference to 'c', we would need to initialiaze 'c' at 0.
                Write("C = " + c);
                ReadKey(true);
                break;

            case 3:
                WriteLine("\n\nEnter the size of the array. (it will contain random integers inside smaller than one thousand.)");
                while (!int.TryParse(ReadLine(), out size))
                {
                    WriteLine("Wrong input. Please, enter an integer.");
                }
                int[] tab = new int[size];
                WriteLine();
                Write("This is your array: ");
                Random rand = new Random();
                for (int i = 0; i < tab.Length; i++)
                {
                    int num = rand.Next(1, 1001);
                    tab[i] = num;
                    Write("\n" + tab[i] + "\n");
                }
                WriteLine();
                size = HelpProgram.Exo01_04_03(tab);
                WriteLine("The addition of the elements in the array makes {0}.", size);
                ReadKey();
                break;

            case 4:
                int sum;
                sum = HelpProgram.Exo01_04_04(5, 8, 9, 10, 20, 19);
                Write(sum);
                ReadKey();
                break;
            }
        }