-1

I created a 2d array of type string, which stores name, marks of 3 subjects and total marks. I need to display the records on basis of the rank i.e the student with higher total marks should be shown first. How can i achieve this in C#? Please help.....

class Program
    {
        static void Main(string[] args)
        {
            int noOfStudents, totalMarks, position = 1;
            string studentName, engMarks, mathMarks, computerMarks;

            Console.Write("Enter Total Students: ");
            noOfStudents = Convert.ToInt32(Console.ReadLine());
            string[,] studentInfo = new string[noOfStudents, 5];

            studentInformation(studentInfo);

            for(int i = 0; i < studentInfo.GetLength(0) + 1; i++)
            {
                int temp, count = 0;
                for(int j = 0; j < studentInfo.GetLength(1); i++)
                {
                    if (int.Parse(studentInfo[i,4]) < int.Parse(studentInfo[i+1, 4]))
                    {
                        temp = int.Parse(studentInfo[i, 4]);
                        studentInfo[i, 4] = studentInfo[i + 1, 4];
                        studentInfo[i + 1, 4] = temp.ToString();
                    }
                    else
                    {
                        count++;
                    }
                }
                    Console.WriteLine("Student Name: {0}, Position: {1}, Total: {2}/300", studentInfo[i,0], position, studentInfo[i, 4]);
                    position++;

            }

            Console.ReadKey();
                void studentInformation(string[,] a) {
                for(int i = 0; i < noOfStudents; i++)
                {
                    Console.Write("Enter Student Name: ");
                    studentName = Console.ReadLine();
                    studentInfo[i, 0] = studentName;

                    Console.Write("Enter English Marks(Out of 100): ");
                    engMarks = Console.ReadLine();
                    studentInfo[i, 1] = engMarks;

                    Console.Write("Enter Maths Marks(Out of 100): ");
                    mathMarks = Console.ReadLine();
                    studentInfo[i, 2] = mathMarks;

                    Console.Write("Enter Computer Marks(Out of 100): ");
                    computerMarks = Console.ReadLine();
                    studentInfo[i, 3] = computerMarks;

                    Console.WriteLine("************************************");
                    totalMarks = (int.Parse(engMarks) + int.Parse(mathMarks) + int.Parse(computerMarks));
                    studentInfo[i, 4] = totalMarks.ToString();
                } 
            }
        }
    }
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Sajid Ansari
  • 17
  • 1
  • 2
  • 1
    While trying the above way... it gives me an index out of bound error – Sajid Ansari Jul 24 '19 at 10:17
  • Try this : https://stackoverflow.com/questions/232395/how-do-i-sort-a-two-dimensional-rectangular-array-in-c?noredirect=1&lq=1 – Hamza Jul 24 '19 at 10:19
  • 1
    `i < studentInfo.GetLength(0) + 1` : why the `+ 1` ? – H H Jul 24 '19 at 10:21
  • 5
    Did your curriculum already cover "creating/ using classes"? Because instead of a multidimensional string array, you *should* be using a list (or maybe array) of "Student" classes having appropriate properties – Hans Kesting Jul 24 '19 at 10:22
  • See also https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f – Peter Duniho Jul 28 '19 at 17:33

1 Answers1

0

It depends of what you are expected to do :

If your goal is to sort the table and then print it

An easy approach is to parse the entire student list as many time as there are students in your list, then on each parse compare each student with the next one. If they aren't in the correct order i.e a student with lower grade is higher in the list, you just exchange them.

In this case, that would give you something like that :

class Program
{
    static void Main(string[] args)
    {
        int noOfStudents, totalMarks, position = 1;
        string studentName, engMarks, mathMarks, computerMarks;

        Console.Write("Enter Total Students: ");
        noOfStudents = Convert.ToInt32(Console.ReadLine());
        string[,] studentInfo = new string[noOfStudents, 5];

        studentInformation(studentInfo);

        // Sorting
        for (int i = 0; i < noOfStudents; i++)
        {
            for (int j = 0; j < (noOfStudents - 1); j++)
            {
                if (Int32.Parse(studentInfo[j, 4]) < Int32.Parse(studentInfo[j +1, 4]))
                {
                    // Temporary element used to store the sorted data
                    string[,] temp = new string[1, 5];

                    // Exchange every grade + name from the higher one to the lesser one
                    for (int k = 0; k < 5; k++)
                    {
                        // Copy one to temp element
                        temp[0, k] = studentInfo[j, k];
                        // Copy the other in the first one
                        studentInfo[j, k] = studentInfo[j + 1, k];
                        // Copy back temp in the other one
                        studentInfo[j + 1, k] = temp[0, k];
                    }
                }
            }
        }

        for (int i = 0; i < noOfStudents; i++)
        {
            Console.WriteLine("Student Name: {0}, Position: {1}, Total: {2}/300", studentInfo[i, 0], position, studentInfo[i, 4]);
        }



        Console.ReadKey();
        void studentInformation(string[,] a)
        {
            for (int i = 0; i < noOfStudents; i++)
            {
                Console.Write("Enter Student Name: ");
                studentName = Console.ReadLine();
                studentInfo[i, 0] = studentName;

                Console.Write("Enter English Marks(Out of 100): ");
                engMarks = Console.ReadLine();
                studentInfo[i, 1] = engMarks;

                Console.Write("Enter Maths Marks(Out of 100): ");
                mathMarks = Console.ReadLine();
                studentInfo[i, 2] = mathMarks;

                Console.Write("Enter Computer Marks(Out of 100): ");
                computerMarks = Console.ReadLine();
                studentInfo[i, 3] = computerMarks;

                Console.WriteLine("************************************");
                totalMarks = (int.Parse(engMarks) + int.Parse(mathMarks) + int.Parse(computerMarks));
                studentInfo[i, 4] = totalMarks.ToString();
            }
        }
    }
}

According to your code you've got some problems, let's focus on them :

  • You only exchange the average grade, which will result in student having average grade that aren't theirs.
  • Your main loop here : for(int i = 0; i < studentInfo.GetLength(0) + 1; i++) goes from 0 to length (as you are using '<' and length+1). However a table index goes from 0 to length - 1. So you have an error when you try to access the last row, as it doesn't exist.

But is it really a good way to do

Well... no.

As mentionned in the comments, a better way to deal with this would have to use objects. For instance, an object student wich will have a List of grade and an average grade.

This way it would have been really easier to code.

Grégory L
  • 612
  • 6
  • 17