DEV Community

Ankur Soni
Ankur Soni

Posted on

student subject marks are lowest marks (C#)

Statement :-
Given a list of N students, every student is marked for M subjects. Each student is denoted by an index value. Their teacher Ms. Margaret must ignore the marks of any 1 subject for every student. For this she decides to ignore the subject which has the lowest class average.
Your task is to help her find that subject, calculate the total marks of each student in all the other subjects and then finally return the array of the total marks scored by each student.

Input Specification:
input1:
An integer value N denoting number of students

input2:
An integer value M denoting number of subjects

input3:
A 2-D integer array of size N'M containing the marks of all students in each subject.

Output Specification:

Return an integer array of size N containing the total marks of each student after deducting the score for that one subject.

*Answer: *

static void Main(string[] args)
        {
            Program program = new Program();
            int totalStudent = 3;
            int totalSubject = 5;
            int[,] marks = {{75,76,65,87,87},
                            {78,76,68,56,89},
                            {67,87,78,77,65}};

            var totalValueArray = program.avgValue(totalStudent, totalSubject, marks);
            foreach (var i in totalValueArray)
            {
                Console.WriteLine(i);
            }
        }
        public int[] avgValue(int input1, int input2, int[,] input3)
        {
            double[] avgSubjectMarks = new double[input2];
            for (int j = 0; j < input2; j++)
            {
                avgSubjectMarks[j] = 0;
                for (int i = 0; i < input1; i++)
                {
                    avgSubjectMarks[j] += input3[i, j];
                }
                avgSubjectMarks[j] = avgSubjectMarks[j] / input1;
            }
            double[] avgSubjectMarksSorted = new double[input2];
            for (int i = 0; i < input2; i++)
            {
                avgSubjectMarksSorted[i] = avgSubjectMarks[i];
            }
            Array.Sort(avgSubjectMarksSorted);
            int shortestSubjectArrayIndex = Array.IndexOf(avgSubjectMarks, avgSubjectMarksSorted[0]);
            int[] result = new int[input1];
            for (int i = 0; i < input1; i++)
            {
                result[i] = 0;
                for (int j = 0; j < input2; j++)
                {
                    if (j != shortestSubjectArrayIndex)
                    {
                        result[i] += input3[i, j];
                    }
                }
            }
            return result;
        }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)