Biggest, Smallest, sort of an array in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • habiballahafg
    New Member
    • Dec 2014
    • 2

    Biggest, Smallest, sort of an array in c#

    Hi
    It's my first post in this forum. :D
    I am learning c# language and I want to do below project:
    1- Input 10 numbers.
    2- Sort them from smallest to biggest number.
    3- Output biggest and smallet number.
    How can I do it.
    I did it like below but it was incorrect:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] numbers = new int[4];
                
                for(int i=0;i<numbers.Length;i++) {
                    int j = Convert.ToInt32(Console.ReadLine());
                    int small = numbers.Min();
                    int large = numbers.Max();
                    Console.WriteLine("The biggest: " + large);
                    Console.WriteLine("The smallest: " + small);
                    
                    
                }
                
                
    
                Console.ReadLine();
                
            }
        }
    }
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    "I did it like below but it was incorrect: "
    what was incorrect, this example does do what you programmed.

    When entering a number, i.e. 12, the minimum of the numbers in 'numbers' is 0. This will always be the case because the numbers in 'numbers' will never change in your program.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      It seems to me that you are missing a few steps and that you are doing calculations in the wrong place...

      In your for loop you should be populating your numbers array with numbers provided by your user.

      After you have finished populating the array, you should be sorting the array.

      Then after that you can calculate and then display the largest and smallest number...

      All of this should be done outside/after the loop that collects the data is finished.

      -Frinny

      Comment

      • habiballahafg
        New Member
        • Dec 2014
        • 2

        #4
        Hi and thanks for your answer Frinavale.
        Can you show me the sample?
        thanks in advance

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          after line#16 add:
          Code:
          numbers[i] = j;
          this will populate the array.

          Your biggest number will start to work (unless you enter only negative numbers......)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I wonder if you got confused by my use of the phrase "populate the array".

            When you create an array of something it allocates space for the items but the array doesn't actually contain items.

            In the case of numbers, everything is set 0 because that is the default value of an integer.

            If you were to declare an array of Object types, everything in the array will be set to null (or Nothing in VB.NET).

            You therefore need to actually add items that you want in your array to the array at the index that you want them to go in.

            In other words, you need to "populate the array" with data and in your case the data is coming from the user (but in other systems it may come for a database, or a file, or from some stream etc.)

            So, in your for-loop, you need to add the items you retrieved from the user to the to the array at the index that is specified as the iterator for your loop.

            Luuk beat me to the example. Please see his post for an example of what I'm talking about.

            -Frinny
            Last edited by Frinavale; Dec 8 '14, 08:12 PM.

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              @Frinny: Thanks, i am trying to learn c# too. the idea to declare an array of Object is nice, i will try that tomorrow.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Oh that is a general statement.

                Say I had the following class:
                Code:
                class student{
                 enum Courses{Programming=1, Networking=2, Database=3, Web=4};
                
                 public string Name{get; set;}
                 public Courses Course{get; set;}
                
                 public student(string name, Courses course){
                    this.Name = name;
                    this.Course = course;
                 }
                }
                And I declared an array of students in my class:
                Code:
                Student[] studentsInCourse = new Student[30];
                If I were to access studentsInCours e[0] the value would initially be null because I haven't populated the array with instantiated students.

                I would have to add a student to the array at the index I am going to access in order to properly use the array.

                Like this:
                Code:
                studentsInCourse[0] = new Student("Luuk", Courses.Programming);
                Now when I access studentsInCours e[0] it will actually be a student object that has information readily available for me to use.


                -Frinny

                Comment

                Working...