Saturday, March 22, 2014

Parallel Array With Method (Wk 9) March 3-7, 2014

Q: Create two arrays, string[] and int[] of the same size. Populate both arrays with names and grades. 
1. Write a method that returns the index of the given name.
2. Get a name and increment the students grade by 5%.
3. Display both names & grades side by side.

namespace Parallel_Arrays_Ex_1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "Zau", "Jill", "Jimmy", "Hallen", "Harold", "Hillary", "Bill", "Sharon", "Sam" };
            int[] grades = { 100, 99, 74, 88, 87, 81, 88, 97, 90 };

            int selection;
            do
            {
                Console.Clear();
                Console.WriteLine("Name list: \n\n" + String.Join(", ", names) + "\n\n");
                DisplayMenu();
                Console.Write("\nEnter your selection: ");
                selection = int.Parse(Console.ReadLine());

                switch (selection)
                {
                    case 1:
                        Console.Write("Select a name to find its index: ");
                        string nameSelection = Console.ReadLine().ToLower();
                        int index = IndexOfGivenName(nameSelection, names);

                        if (index != -1)
                            Console.WriteLine("{0} is at index {1}", names[index], index);
                        else
                            Console.WriteLine("Name doesn't exist in the array");
                        break; //=====================================================


                    case 2:
                        Console.Write("Select a name from array to add 5% to their grade: ");
                        nameSelection = Console.ReadLine().ToLower();
                        index = IndexOfGivenName(nameSelection, names);

                        if (index != -1)
                            Console.WriteLine("{0}'s grade is now: {1}", names[index], grades[index] += 5);
                        else
                            Console.WriteLine("Name doesn't exist in array");
                        break; //=====================================================


                    case 3:
                        DisplayArrays(names, grades);
                        break; //=====================================================

                    default:
                        break;
                }
                Console.ReadKey();
            } while (selection != 0);
        }
        //*********************************************************************************
        static void DisplayMenu()
        {
            Console.WriteLine("\tSelect an exercise from the menu below:\n");
            Console.WriteLine("\t1. Write a method that returns the index of given name.");
            Console.WriteLine("\t2. Get a name and increment the students grade by 5%");
            Console.WriteLine("\t3. Display both names & grades side by side.");
            Console.WriteLine("\t9. Press 9 to exit");
        }
       static int IndexOfGivenName(string name, string[] array) //Method for 1 & 2 (Efficient way)
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (name == array[i].ToLower())
                {
                    return i;
                }
            } return -1;
        }
        static void DisplayArrays (string[] name, int [] grade)//Method for 3
       {
           for (int i = 0; i < name.Length; i++)
           {
               Console.WriteLine("Name: {0} \tGrade: {1}", name[i], grade[i]);
           }
       }
    }
}

No comments:

Post a Comment