Answer to Question #190316 in C# for Nkhululeko chabala

Question #190316

Write a program that does the following for 15 students:

 Reads in 3 marks for each student (marks must be between 0 and 100)

 Calculates and displays the highest mark for each student.

 Finds the highest mark for all students


1
Expert's answer
2021-05-07T10:50:46-0400
using System;

namespace Test
{
    class StudentsTest
    {
        static int Main()
        {
            const int STUDENTS_COUNT = 15;
            const int MARKS_COUNT    = 3;

            int[,] array = new int[STUDENTS_COUNT, MARKS_COUNT];

            Console.WriteLine("Enter {0} marks for {1} students ({1} lines {0} integers each. Mark range is [0; 100]):"
                             ,MARKS_COUNT, STUDENTS_COUNT);

            for(int student = 0; student < STUDENTS_COUNT; ++student)
            {
                string   line  = Console.ReadLine();
                string[] marks = line.Split();

                if(marks.Length != MARKS_COUNT)
                {
                    Console.WriteLine("Bad input");
                    return 1;
                }

                for(int mark = 0; mark < MARKS_COUNT; ++mark)
                {
                    if(!int.TryParse(marks[mark], out array[student, mark]))
                    {
                        Console.WriteLine("Bad input");
                        return 1;
                    }
                }
            }

            int highestMark = 0;

            for(int student = 0; student < STUDENTS_COUNT; ++student)
            {
                int highestStudentMark = array[student, 0]; 

                for(int mark = 1; mark < MARKS_COUNT; ++mark)
                {
                    if(highestStudentMark < array[student, mark])
                    {
                        highestStudentMark = array[student, mark];
                    }
                }

                Console.WriteLine("Student #{0} highest mark: {1}", student, highestStudentMark);

                if(highestMark < highestStudentMark)
                {
                    highestMark = highestStudentMark;
                }
            }

            Console.WriteLine("----------------------------------");
            Console.WriteLine("highest mark for all students: {0}", highestMark);

            return 0;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS