Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# I want to use either a switch or if statement to select a sorting algorithm

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");

”’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading