Write a program named ArrayDemo that stores an array of 10 integers. int[] numbers = {7, 6, 3, 2, 10, 8, 4, 5, 9, 1}
Until the user enters a sentinel value, allow the user four options:
(1) to view the list in order from the first to last position in the stored array
(2) to view the list in order from the last to first position (without permanently changing in order of the array)
(3) to choose a specific position to view
(4) to quit the application.
Yes, use the num array given above.
Inside, include your name and a description of what the program does and how to use it, as well as pertinent comments. Use meaningful variable names. Put your name in the project name and in the name of the .cs file. Failure to do these things will result in my not grading your submission.
/*
name: Slassh
User enters options.
Until the user enters a sentinel value 4 , program will execute.
If enter number 1 - view the list in order from the first to last position.
If enter number 2 - view the list in order from the last to first position (without permanently changing in order of the array).
If enter number 3 - view specific position.
If enter number 4 - quit program.
*/
using System;
namespace Slassh
{
class Program
{
public static void Main(string[] args)
{
int[] numbers = {7, 6, 3, 2, 10, 8, 4, 5, 9, 1};
int option = 1;
int position; // for specific position
while (option != 4) // repeat until the entered option number is not 4
{
Console.WriteLine("Options:");
Console.WriteLine("(1) view the list in order from the first to last position");
Console.WriteLine("(2) view the list in order from the last to first position");
Console.WriteLine("(3) choose a specific position to view");
Console.WriteLine("(4) quit the application");
Console.Write("Enter option number: ");
option = Convert.ToInt32(Console.ReadLine());
switch (option) // analizing option number
{
case 1: // view the list in order from the first to last position
for (int i = 0; i < 10; i++)
Console.Write("{0} ", numbers[i]);
Console.WriteLine();
break;
case 2: // view the list in order from the last to first position
for (int i = 9; i >= 0; i--) // i = 9 - array indexing from 0 to 9
Console.Write("{0} ", numbers[i]);
Console.WriteLine();
break;
case 3: // view specific position
Console.Write("Enter position (1..10): ");
position = Convert.ToInt32(Console.ReadLine());
Console.Write("{0} ", numbers[position - 1]); // position-1 - array indexing from 0
Console.WriteLine();
break;
default: // if incorrect option number
if (option != 4) Console.WriteLine("Invalid option number");
break;
}
}
}
}
}
Comments
Leave a comment