49.7k views
3 votes
In C#Write the program SubscriptExceptionTest in which you use an array of 10 doubles. Write a try block in which you place a loop that prompts the user for a subscript value and displays the value stored in the corresponding array position or asks the user to quit the program by entering 99. Create a catch block that catches any IndexOutOfRangeException and displays the message: Index was outside the bounds of the array.

User LMH
by
3.6k points

1 Answer

1 vote

Answer:

using System;

namespace ConsoleApp3

{

class Program

{

static void Main(string[] args)

{

double[] doubleArray = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };

Console.WriteLine("Double Array values are:");

foreach (var b in doubleArray)

{

Console.WriteLine(b);

}

Console.WriteLine("Enter value to find position in Array");

var input = Console.ReadLine();

var value = Convert.ToDouble(input);

if (value == 99.0)

{

Environment.Exit(0);

}

try

{

var index = Array.IndexOf(doubleArray, value);

if (index == -1)

{

Console.WriteLine("Index was outside the bounds of the array");

Console.ReadLine();

}

else

{

Console.WriteLine("Position in Array is:" + index);

Console.ReadLine();

}

}

catch (Exception e)

{

Console.WriteLine("Index was outside the bounds of the array");

Console.ReadLine();

}

}

}

}

Step-by-step explanation:

this is the code of console program which initialize the double array from 1-10 and displays the existing values of array in console.

Then it will take input from user, convert it to double and find the value position in double array.

if 99, then program will be exist

if exist in array:display the index position

else:index was outside the bounds of the array in try catch block.

User Khajlk
by
5.1k points