Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static void Main(string[] args)
{
    //change the number of arrays to 10!
    string[] studentNames = new string[3];
    int[] finalScores = new int[3];

    int minIndex, minGoes, temp;

    for (int i = 0; i < studentNames.Length; i++)
    {
        Console.Write("Enter student name " + ": ");
        studentNames[i] = Convert.ToString(Console.ReadLine());
        Console.Write("Enter student score " + ": ");
        finalScores[i] = Convert.ToInt32(Console.ReadLine());
    }

    minGoes = 0;
    minIndex = 0;

    for (int j = 1; j < finalScores.Length; j++)
    {
        if (finalScores[j] > finalScores[minIndex])
        {
            minIndex = j;
        }
        temp = finalScores[minIndex];
        finalScores[minIndex] = finalScores[minGoes];
        finalScores[minGoes] = temp;

        for (int i = 0; i < finalScores.Length - 1; i++)
        {
            minIndex = i;
            for (int j = i + 1; j < finalScores.Length; j++)
            {
                if (finalScores[j] > finalScores[minIndex])
                {
                    minIndex = j;
                }
                temp = finalScores[minIndex];
                finalScores[minIndex] = finalScores[i];
                finalScores[i] = temp;
            }
            Console.WriteLine("Student Scores: ");
            for (int i = 0; i < finalScores.Length; i++)
            {
                Console.Write(finalScores[i] + "\n ");
            }


What I have tried:

So far I have it showing the test scores in highest to lowest. I cant figure out how to get names to be right with the highest to lowest test scores ?

Also how could I incorporate a few methods ?
Posted
Updated 1-Dec-16 7:00am
v3
Comments
Richard MacCutchan 1-Dec-16 13:01pm    
Since each name goes with its score, you change the name entries at the same time as you change the score entries.

What does your second question mean?

1 solution

The two arrays are not linked in any way. You would have better luck if you used a List of student objects (that contained properties for the name and the score), and then sort the list on the name property.

Other tips:

0) Never concatenate strings in .Net with the "+" operator. Use string.Concat() instead.

1) There's no need to use Console.Write("...\n") when you can use Console.WriteLine("...").

BTW, questions that included the use of the Console object are pretty much always related to homework. Another clue is that no real programmer in his right mind would consider using two arrays instead of a list of objects. If your instructor is mandating the two array paradigm, he's an idiot.
 
Share this answer
 
v2
Comments
Philippe Mori 1-Dec-16 23:02pm    
Well, I would not say that the professor is idiot... It should be obvious that if one want both array to be updated, he just need to add a few lines to move also name (in the other array) when sorting. It would be idiot if the alternative of using objects is not shown at some point in the course.
Matt T Heffron 2-Dec-16 17:17pm    
Re: Tip 0: This is pointless advice.
The string "+" operator compiles into calls to string.Concat() [at least with VS2013, Release mode]
On the other hand, don't use string concatenation (either "+" or string.Concat()) to build up a string piecewise sequentially.
(This will create lots of intermediate string instances that are almost immediately dropped.)
Use a System.Text.StringBuilder in this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900