# Thread: Average, Random & sort

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]);
}
}
}
}```

2. 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.

3. 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```