Friday, March 21, 2014

Array with Method_Ex_7 (Wk 8) Feb 24-28, 2014

Q: Write a method to swap the first and last values.
Q: Write method to display all the values of the array in a single line.

 static void Main(string[] args)
        {
            int[] A = { 12, -5, -9, 3, 6, 19, -13, -22, 33, 1, -17, 29 };

            SwapfirstNlastvalue(A);
            DisplayArray(A);

            Console.ReadLine();
        }
        //method to swap the first and last value
        static void SwapfirstNlastvalue(int[]A)
        {
            int temp = A[0];
            A[0] = A[A.Length - 1];
            A[A.Length - 1] = temp;
        }

        //method to display array in single line
        static void DisplayArray(int[]A)
        {
            Console.WriteLine("\n**********************");
            for (int i = 0; i < A.Length; i++)
            {
                Console.Write("{0}, ", A[i]);
            }
            Console.WriteLine("\n**********************");
        }
    }
}

No comments:

Post a Comment