Friday, March 21, 2014

Array_Ex_4 (Wk 8) Feb 24-28, 2014

Q: Request an index from user, as well as a new value. Change the value of array at the index with new the value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Array_Ex_4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[12] { 2, -9, 8, 12, -6, -23, 33, 40, 19, -45, 18, -4 };

            //Step 1: Request the index
            int index;
            do
            {
                Console.Write("Enter index from 0 to {0}: ", array.Length - 1);
                index = int.Parse(Console.ReadLine());

            } while (index >= array.Length || index < 0);

            //Step 2: Request new integer value.
            Console.Write("Enter new integer value: ");
            int newValue = int.Parse(Console.ReadLine());

            //Step 3: Update the array
            array[index] = newValue;

            //Step 4: Display the array
            Console.WriteLine("\n");
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("{0}, ", array[i]);
            }

            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment