Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

BRACKEYS - Solution to C# Tutorial 05 Challenge

13»

Comments

  • using System;


    namespace My_awesome_program

    {

      class Program

      {

        static void Main(string[] args)

        {

          Console.WriteLine("How many students do you have?");

          int nrStud = Int32.Parse(Console.ReadLine());

          string[] students = new string[nrStud];


          Console.WriteLine($"Okey, so {nrStud} students, please write their names, and ill display in alphabetical order");


          for (int i = 0; i < students.Length; i++)

          {

            students[i] = Console.ReadLine();

          }

          Console.WriteLine("--------------------------");

          Console.WriteLine("Here they are in alphabetical order");

          //Sorting in alhpabetical order

          Array.Sort(students);

          for (int i = 0; i < students.Length; i++)

          {

            Console.WriteLine(students[i]);

          }

           

          //Close program

          Console.ReadKey();

        }

      }

    }

  • That's my solution!

  • The program did work well , but there was just one error that would not go away at line 20

    using System;
    
    
    namespace MyApp // Note: actual namespace depends on the project name.
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.Title = "Students";
    
    
                Console.Write("How many students are there in your class? ");
                int numStudents = Convert.ToInt32(Console.ReadLine());
    
    
                string[] students = new string[numStudents];
    
    
                Console.WriteLine("Enter the names of the students: ");
    
    
                for (int i = 0; i < students.Length; i++)
                {
                    students[i] = Console.ReadLine();
                }
    
    
                Console.WriteLine("\nHere they are alphabetically: ");
    
    
                Array.Sort(students);
                for (int i = 0; i < students.Length; i++)
                {
                    Console.WriteLine(students[i]);
                }
    
    
                Console.ReadKey();
            }
        }
    }
    
  • This is mine:


    Console.Title = "C# Thing";


    Console.Write("How many students are in your class: ");



    string[] stdnts = new String[Convert.ToInt32(Console.ReadLine())];

    for (int i = 0; i < stdnts.Length; i++)

    {

        stdnts[i] = Console.ReadLine();    

    }

    Console.WriteLine("Input the name of your students or DIE");


    Array.Sort(stdnts);


    Console.WriteLine("------------");


    for (int X = 0; X < stdnts.Length; X++)

    {

       

        Console.WriteLine(stdnts[X]);

    }





    Console.ReadKey();

  • profitdefiantprofitdefiant Member
    edited March 9

    This is my solution to the problem using Arrays

    namespace Arrays;
    
    class Program
    {
        // Program that allows a teacher to input all the students in the class and have their names
        // printed out alphabetically. Choose between Arrays or Lists
        static void Main(string[] args)
        {
            Console.Title = "Arrays with ForLoops - Student Register";
            Console.WindowHeight = 5;
    
            int serialNumber = 0;
            int sortedSerialNumber = 0;
            string[] studentFullNames = new string[5];
    
            Console.WriteLine("Enter the names of all your students");
    
            // Writing all the names in any order
            for (int i = 0; i < studentFullNames.Length; i++)
            {
                serialNumber++;
                Console.Write(serialNumber + ": ");
                studentFullNames[i] = Console.ReadLine()!;
            }
    
            Console.WriteLine("\nBehold the sorted names");
            Array.Sort(studentFullNames);
    
            // Printing out the sorted names
            for (int i = 0; i < studentFullNames.Length; i++)
            {
                sortedSerialNumber++;
                Console.Write(sortedSerialNumber + ": ");
                Console.WriteLine(studentFullNames[i]);
            }
    
            Console.ReadKey();
        }
    }
    

    And using Lists::

    namespace Lists;
    
    class Program
    {
        // Program that allows a teacher to input all the students in the class and have their names
        // printed out alphabetically. Choose between Arrays or Lists
        static void Main(string[] args)
        {
            Console.Title = "Lists with ForLoops - Student Register";
    
            Console.Write("How many students are in your class: ");
            int numberOfStudents = Convert.ToInt32(Console.ReadLine()!);
    
            Console.WriteLine("Enter the names of all your students");
            List<string> studentNames = new List<string>();
    
            // Writing all the names in any order
            for (int i = 0; i < numberOfStudents; i++)
            {            
                studentNames.Add(Console.ReadLine()!);
            }
    
            Console.WriteLine("\nBehold the sorted names");
            studentNames.Sort();
    
            // Looping and printing out the sorted names
            for (int i = 0; i < studentNames.Count; i++)
            {
                Console.WriteLine(studentNames[i]);
            }
    
            Console.ReadKey();
        }
    }
    
Sign In or Register to comment.