Monday, March 24, 2014

Question No 40_45(Random, Array_/Method)

Ex 40.    Within a loop, you are to generate random values between 100 and
     200, until the sum of all the random values exceed 2000. Within
    the loop displays the random value as well as the accumulated
    sum.
        static void Main(string[] args)
        {
            Random rand = new Random();
            int rdNo;
            int sum = 0;
            int counter = 1;
            while (sum <= 2000)
            {
                rdNo = rand.Next(100, 201);
                sum += rdNo;
                counter++;
                Console.WriteLine("Radom No: {0  }  Accumulated Sum: {1}", rdNo, sum);
            }
            Console.ReadKey();
        }
//==================code for question no 41 & 42================
Ex 41.    Create a one dim array A and initialize it with the values: 4,
     33, -21,-3, 43, 1, -5
Ex 42.    Create a one dim array B and initialize it with the values: 2.4,
     4.3, 5.6, 6.4, 9.5, 3.6, 7.3
        static void Main(string[] args)
        {
            //==================Ex 41==============================
            int[] arrayA = { 4, 33, -21, -3, 43, 1, -5 };

            //==================Ex 42==============================
            double[] arrayB = { 2.4, 4.3, 5.6, 6.4, 9.5, 3.6, 7.3 };
        }
//==================code for question no 43 & 44================
Ex 43.    Write a method 'getPositiveCount' that takes a one-dimensional
     array, counts the number of values in array that are greater
     than 0 and returns that count.

Ex 44.    Write a method 'getRangeCount' that takes a one-dimensional
     array, counts the number of values in the array that are between
     1 and 6 (both inclusive), then returns the count.
        static void Main(string[] args)
        {
            int[] arrayA = { 4, 33, -21, -3, 43, 1, -5 };

            //Ex 43: call method
            int result43 = GetPositiveCount(arrayA);
            Console.WriteLine("Positive count is: {0}", result43);

            //Ex 44: call method
            int result44 = getRangeCount(arrayA);
            Console.WriteLine("Range count is: {0}", result44);


            Console.ReadLine();
        }
        //=================Ex 43===========================
        static int GetPositiveCount(int [] arrayA)
        {
            int countPositive = 0;
            for (int i = 0; i < arrayA.Length; i++)
            {
                if (arrayA[i] > 0)
                    countPositive++;
            }
            return countPositive;
        }
        //=================Ex 44===========================
        static int getRangeCount(int [] arrayA)
        {
            int countInRange = 0;
            for (int i = 0; i < arrayA.Length; i++)
            {
                if (arrayA[i] >= 1 && arrayA[i] <= 6)
                    countInRange++;
            }
            return countInRange;
        }
    }
}
//==================code for question no 45================
Ex 45.    Write a method that takes 3 integer parameters and returns the
     average of the highest 2 parameters.

        static void Main(string[] args)
        {

            Console.Write("X: ");
            int X = int.Parse(Console.ReadLine());
            Console.Write("Y: ");
            int Y = int.Parse(Console.ReadLine());
            Console.Write("Z: ");
            int Z = int.Parse(Console.ReadLine());

            double result = avgOf2highParameters(X, Y, Z);

            Console.WriteLine("Avg of 2 high parameter: {0}", result);

            Console.ReadLine();

        }
        static double avgOf2highParameters(int X, int Y, int Z)
        {
            double average = 0;
            if (X < Y && X < Z)
            {
                average = (Y + Z) / 2.0;
            } 
            else if (Y < X && Y < Z)
            {
                average = (X + Z) / 2.0;
            }
            else if (Z < X && Z < Y)
            {
                 average = (X + Y) / 2.0;
            }
            else
            {
                Console.WriteLine("Do not repeat the three value");
            }
            return average;
        }

No comments:

Post a Comment