Using Visual Studio, create a simple program that will have the following features:
-ask the user to input the desired size of an array
-let the user input values to be saved in the declared array
-display all the values saved in the created array
using System;
namespace ArrayDemo
{
internal class Program
{
static void Main(string[] args)
{
int dim=0;// array dimension
bool InpErr;
do
{
InpErr = false;
Console.Write("Input the desired size of an array: ");
try
{
dim = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Invalid value. Re-enter array size");
InpErr = true;
}
}
while (InpErr);
int[] arr = new int[dim];
Console.WriteLine();
string str;
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("Input the next array element. For exit press \"X\".");
try
{
str = Console.ReadLine();
if ((str == "X") | (str == "x"))
{
break;
}
else
{
arr[i] = Convert.ToInt32(str);
}
}
catch
{
Console.WriteLine("Invalid value. Re-enter array element");
i--;
}
}
Console.WriteLine();
Console.WriteLine("Values saved to array are...");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine();
Console.WriteLine("Press any key to exit app");
Console.ReadKey();
}
}
}
Comments
Leave a comment