I am writing a program that will take user input and run it through whichever sort they choose. Where I am having a problem is if I try to use a switch I cannot figure out how to add arguments to a switch or if I use an if statement how do I implement that with the user's input? here is the code and thank you all for your help. ''' using System;
namespace ASortAboveTheRest { internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
string myOption;
myOption = Console.ReadLine();
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i )
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i )
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
'''
CodePudding user response:
Following code should do the trick:
using System;
namespace ASortAboveTheRest {
internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
// Take Option input as an INT
int myOption;
myOption = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i )
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i )
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
//Use switch case after taking array input from the user
switch(myOption) {
case 1:
//Call Heap Sort Function and pass your array
break;
case 2:
// Call Bubble Sort Function and pass your array
break;
case 3:
//Call Shell Sort Function and pass your array
break;
default:
break;
}
}
}
}