0

I have it print the array of grades after sorting them but displays the wrong names with the grades.

class Program
{
    static void Main(string[] args)
    {
        int namelength;
        string names;
        double grade, max, min, testgradeaverage;
        int nameindex = 0;
        int gradeindex = 0;

        Console.WriteLine("Please enter the amount of names you wish to enter: ");
        namelength = Convert.ToInt32(Console.ReadLine());
        string[] name = new string[namelength];
        double[] testgrades = new double[namelength];

        for (int i = 0; i < testgrades.Length; i++)
        {
            Console.WriteLine("Please enter the names of each student");
            names = Convert.ToString(Console.ReadLine());
            name[i] += names;
            nameindex = i;

        }
        for (int i = 0; i < name.Length; i++)
        {
            Console.WriteLine("Please enter the final grade for " + name[i]);

            grade = Convert.ToDouble(Console.ReadLine());
            testgrades[i] += grade;
            gradeindex = i;

        }
        max = testgrades.Max();

        min = testgrades.Min();
        testgradeaverage = testgrades.Average();

        Console.WriteLine("The highest grade is: " + max);
        Console.WriteLine("The Lowest Grade is:" + min);
        Console.WriteLine("The class average is: " + testgradeaverage);

        for (int k = 0; k < namelength; k++)
        {
            sorted(ref testgrades);
            Console.WriteLine(name[nameindex] + testgrades[k]);
        }




        Console.ReadLine();

    }
    public static void sorted(ref double [] testgrades)
    {
        double temp = 0;
        for (int write = 0; write < testgrades .Length; write++)
        {
            for (int sort = 0; sort < testgrades.Length - 1; sort++)
            {
                if (testgrades [sort] <= testgrades [sort + 1])
                {
                    temp = testgrades[sort + 1];
                    testgrades [sort + 1] = testgrades [sort];
                    testgrades [sort] = temp;
                }
            }
        }


    }
6
  • You are only sorting grades in place, which means your respective names stay in the same place even the grades move around. Commented Dec 11, 2016 at 23:20
  • so i have to sort the names to ? Commented Dec 11, 2016 at 23:21
  • Not exactly. What you need to do is move the respective namr when you move the grade associated with it. Commented Dec 11, 2016 at 23:24
  • how would i do something like that? Commented Dec 11, 2016 at 23:25
  • Whenever you move an element in the grades array, do the exact same thing to the element in the names array at the index. Either that, or change your program to use a Tuple, a KeyValuePair, or a custom type. Commented Dec 11, 2016 at 23:30

3 Answers 3

3

There is an overload of Array.Sort that accepts two arrays; one for the keys, and one for the items. The items of both are sorted according to the keys array:

Array.Sort(testgrades, names);

If you couldn't get your results from this overload, you can create a custom comparer and pass it as third argument.

Sign up to request clarification or add additional context in comments.

Comments

0

Short answer change this:

for (int k = 0; k < namelength; k++)
{
    sorted(ref testgrades);
    Console.WriteLine(name[nameindex] + testgrades[k]);
}

to this. Since you have the same number of items in each array:

sorted(testgrades, names);
for (int k = 0; k < namelength; k++)
{
    Console.WriteLine(name[k] + testgrades[k]);
}

And your sorted method to this:

public static void sorted(double[] testgrades, string[] names)
{
    double temp = 0;
    string nameTemp = "";
    for (int write = 0; write < testgrades.Length; write++)
    {
        for (int sort = 0; sort < testgrades.Length - 1; sort++)
        {
            if (testgrades[sort] <= testgrades[sort + 1])
            {
                temp = testgrades[sort + 1];
                testgrades[sort + 1] = testgrades[sort];
                testgrades[sort] = temp;

                // If you move the grade to a different index, you need to move the corresponding grade
                // as well
                nameTemp = names[sort + 1];
                names[sort + 1] = names[sort];
                names[sort] = nameTemp;
            }
        }
    }
}

Long answer:

A few things:

  1. You do not need to pass the testGrades array by ref. C# will pass this array by reference for you since arrays are not value types.
  2. Do not call sorted method from within a loop since you keep sorting the same values over and over.
  3. I think your instructor probably requires you to do the sort yourself instead of using a library call, but if you are allowed to use Linq, since you have used it, then you can also sort your arry using Linq. Use the OrderBy which will sort ascendingly and use the OrderByDescending which will sort descendingly.

You have this code:

for (int i = 0; i < testgrades.Length; i++)
{
     Console.WriteLine("Please enter the names of each student");
     names = Convert.ToString(Console.ReadLine());
     name[i] += names;
     nameindex = i;
}

See the variable nameindex will have the last i value after looping. Therefore, when you are writing names later, after sorting, you will always have the same name showing (the very last name entered).

4 Comments

How the short answer works? Are you sorting only one array? It makes no sense for me.
There is just one array for grades. And the other arrays have the same number of items.
This answer seems wrong. I have no idea how this got accepted. @CodingYoshi Could you explain how you can find the respective names without touching name array?
Yes on my way home I realized this oversight then I also saw the comments here as well.
0

Create custom type, e.g:

public class NameGrade
{
    public string Name;
    public double Grade;
}

Then use only one array of that type. Fill it with data and then sort elements. Instead of using your own sorting method, you can use extension method (using System.Linq): NameGrade[] sorted = myArray.OrderBy (x => x.Grade).ToArray ();

EDIT:

Looks like Array.Sort (keys, values) do the job. However it is better to use custom type (better code readability).

3 Comments

Array.Sort will likely be faster than OrderBy in this instance.
How so? Both Array.Sort and OrderBy names are self-explanatory, and both use a comparison delegate. If anything, I'd argue that OrderBy would be more confusing because of the necessary ToArray() afterwards, which can confuse people new to LINQ, whereas Array.Sort syntax is just Array.Sort(arrayToBeSorted, comparisonDelegate).
@Abion47: You are right. Linq is harder to understand than Comparison delegate. So I deleted my comment. But better solution is to use different overload Array.Sort (keys, values) (like in @YaserAdelMehraban answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.