Friday, March 21, 2014

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

Q: Write Method to find the index of the first odd value it finds.

        static void Main(string[] args)
        {
            int[] A = { 12, -5, -9, 3, 6, 19, -13, -22, 33, 1, -17, 29 };
           
            int result = GetfirstOddIndex(A);
            Console.WriteLine("Fist Odd number is in index {0}", result);

            Console.ReadLine();
        }
        static int GetfirstOddIndex(int[] A) //Method
        {
            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] % 2 != 0)
                    return i;
            }
            return -1;
        }
    }
}
 **********************************************************************
Q: Write Method to find the index of the first even positive value it finds.

        static void Main(string[] args)
        {
            int[] A = { 12, -5, -9, 3, 6, 19, -13, -22, 33, 1, -17, 29 };
           
            int result = GetfirstEvenPositiveIndex(A);
            Console.WriteLine("Fist Even Positive number is in index {0}", result);

            Console.ReadLine();
        }
        static int GetfirstEvenPositiveIndex(int[] A)
        {
            for (int i = 0; i < A.Length; i++)
            {
                if ((A[i] % 2 == 0) && (A[i] > 0))
                    return i;
            }
            return -1;
        }
    }
}


No comments:

Post a Comment