Skip to content

My teacher said that i am missing i hould sort the array once the data is entered what does that mean?

Jenni Witzel

Asked Oct 24, 2008, 9:22 PM EDT2 answers
Share
LinkedInX
Here is how you should sort the array once the data is entered and has been printed out once. The only reason we are sorting the array is to make finding the largest and smallest elements easier to do.

Array.Sort(myList);

Once the arrary is sorted, the largest value will be in the last position of the array (subscript value of 11) and the smallest element will be in the first position (subscript value of 0).

Here is how you can print the largest element:
SC.WriteLine("largest value entered is{0}", myList[11]);

Here is how you can print the smallest element:
SC.WriteLine("smallest value entered is{0}", myList[0]);




using System;

using SC = System.Console;

public class NLastLab8
{ //Main Method

    public static void Main()
    {

        int[] myListArray = new int[12];

        myListArray[0] = myListArray[1] = 1;

        SC.WriteLine("Lab 8 – Your Name\n");

        for (int x = 0; x < 12; ++x)
        {

            SC.Write("Please enter a value for element {0} -->\t", x);

            myListArray[x] = Convert.ToInt32(SC.ReadLine());

        }


                SC.WriteLine("\n\nThe last value entered was: " + myListArray[myListArray.Length - 1]);


                SC.ReadKey();


                SC.WriteLine("The values entered were: ");

                for (int i = myListArray.Length - 1; i >= 0; i--)
            {

                SC.WriteLine(myListArray[i]);

            }


                SC.WriteLine("The values entered were: ");

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

            SC.WriteLine(myListArray[i]);

        }

        SC.WriteLine("\nLab 8 has successfully terminated for Your Name");


        SC.ReadKey();

    }

}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Alex Rivenbark
    +1

    Ok. Thecode that you need is:

    Array.Sort(myListArray); //This passes your array to the Sort(array) method of the Array class, to place the values in order from smallest to largest.

    SC.WriteLine("The smallest value entered was: {0} -->", myListArray[0]); //Since the array is sorted smallest to largest now, the smallest number will be in the 1st index.

    SC.WriteLine("The largest value entered was: {0} -->", myListArray[11]); //Once again, after sorting, the laargest number will be in the last index

     

    The code that I sent you previously was including several different criteria. Depending on what your teacher has instructed, you may want to remove any code that is not needed. Let me know if this helps. Thanks.

    +1
  • +1

    here is my orginal directions like i said b4 i fig sum of it out but then i got stuck in the middle



    PowerShell
    /********************************************************/
    /*   Your Name                                          */
    /*   CIS 218                                            */
    /*   LAB 8 - Working with an array                      */
    /*   Due Date: xx/xx/xxxx                               */
    /*                                                     
    */
    /*   Description: This program prompts for 12
    integers, */
    /*                prints a list of them as well
    as the  */
    /*                largest and smallest values.
    Data is  */
    /*                stored in an array.                   */
    /*                                                     
    */
    /********************************************************/
    using System;
    using SC =
    System.Console;
    public class
    FLastLab8
    { //Main Method
        public static void Main()
        {
            int[] myList = new int[12];
    
            SC.WriteLine("Lab 8 – Your Name\n");
    
            for (int x = 0; x < 12; ++x)
            {
                SC.Write("Please enter a value
    for element {0} -->\t", x);
                myList[x] =
    Convert.ToInt32(SC.ReadLine());
            }

     

     


            SC.WriteLine(“\n\nThe values entered were: {0}”, myList[0]);

     

     

     

     


          

           

     

           

     

     

     

     

     

     

     


    Code
            SC.WriteLine("\nLab 8 has
    successfully terminated for Your Name");
        }
    }
    +1