Thread: Average, Random & sort

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    1

    Average, Random & sort

    I have a project and i did some of them but other i didn't do it
    can any one help me please
    in calcualte the average, sort and randomly

    1. The program acceps
    input for one student name and the student’s GPA . Maximum number of students and GPAs that can be entered are 20.2. The program calculates the average GPA for all the students that were entered.3. The program shows all student names with their GPA’s in a listbox.4. The program groups the student names in goups depending on the required group size and shows the student names in a listbox.5. The program can select a student from the student list (array) randomly and displays the name.
    6. BONUS: The program sorts all GPAs and shows the student names with the GPAs in a sorted list
    and this is my code

    Code:
    namespace Final_Project
    {
    publicpartialclassformStudent:Form {
    // Declare and Initialize 
      double[] gpas =
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
      string[]names = {
      " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
            " ", " ", " ", " ", " ", " "};
      int counter = 0;
      public formStudent() {
        InitializeComponent();
      }
      privatevoid btnEnter_Click(object sender, EventArgs e) {
    // This method is called when we click the btn Enter buttom.
    // It will enter the student name & GPA.
    // The maximum number of student & GPA that can be entered are 20.
    // Declare and Initialize
        double gpa;
        string name = txtName.Text;
    // convert
        gpa = Convert.ToDouble(txtGPA.Text);
    // Move gpa to array
    // only if counter is lees than 21
        if (counter < 20) {
          gpas[counter] = gpa;
          names[counter] = name;
          counter++;
          MessageBox.Show("The name was entered");
        } else {
          MessageBox.Show("You cannot enter more names");
        }
      }
      privatevoid btnList_Click(object sender, EventArgs e) {
    // This method is called when we click the btn List buttom.
    // It will list the student name & GPA in the list box.
        for (int i = 0; i < counter; i++) {
          lbStudentGPA.Items.Add(names[i] + " " + gpas[i]);
        }
      }
    }
    }
    Last edited by Salem; 06-03-2012 at 06:48 AM. Reason: Fixed train-wreck of font and colour

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should probably repost your code, making sure that you post "as text".
    Whatever colouring tool you used basically trashes the code by removing necessary white-space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's all built right in for you if you have a new enough version to have LINQ:
    Code:
            static void Main(string[] args)
            {
                Entry[] entries = new Entry[] { new Entry("Steve", 2.5), new Entry("Mary", 3.2), new Entry("John", 3.1) };
                Console.WriteLine("Average GPA: {0:F1}", entries.Average(e => e.GPA));
                entries = entries.OrderByDescending(e => e.GPA).ToArray();
    
                Console.WriteLine("\nEntries sorted by GPA:");
                foreach (Entry entry in entries)
                    Console.WriteLine("  {0}", entry);
            }
    
            private struct Entry
            {
                public string Name;
                public double GPA;
    
                public Entry(string name, double gpa)
                {
                    Name = name;
                    GPA = gpa;
                }
    
                public override string ToString()
                {
                    return String.Format("{0} {1}", Name, GPA);
                }
            }
    Code:
    Average GPA: 2.9
    
    Entries sorted by GPA:
      Mary 3.2
      John 3.1
      Steve 2.5
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-20-2012, 10:38 PM
  2. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 07:14 PM
  3. Psuedo random numbers in a bubble sort.
    By Sir Andus in forum C++ Programming
    Replies: 10
    Last Post: 04-09-2007, 08:25 PM
  4. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:23 AM
  5. Random Sort
    By Trebor in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 08:32 AM