Answer to Question #330981 in C# for Kun

Question #330981

Write a program that does the following for 15 students:

 Reads in 3 marks for each student, however, you need to make provision that the user

might be typing invalid values – when an invalid mark is entered, the user should be prompted to re-enter a valid mark. This should happen continuously until you have three valid marks for each student (valid marks are marks between 0 and 100; 0 and 100 included). Your prompt message to the user should indicate which number is being requested

 Calculates and displays the highest mark for each student.

Finds and displays the highest mark in the class.

 Adapt your code to also read in the name of each student, and add the name to the display

of the highest mark for each student

 When displaying the overall highest mark, the name of the relevant student should also be

displayed.

 Update your solution to make use of the getMark method. In this updated version, your prompt messages does

not have to indicate which mark is being requested.


1
Expert's answer
2022-04-20T12:40:37-0400
using System;
using System.Linq;


namespace ConsoleApp4
{
    class Program
    {
        static void Main()
        {
            Student[] students = new Student[2];
            for (int i = 0; i < students.Length; i++)
                students[i] = EnterStudent();


            Console.WriteLine(string.Join("\n", students.Select(x => x.ToString())));




            students = students.OrderByDescending(s => s.GetMark()).ToArray();
            Console.WriteLine($"The overall highest mark: {students.First().GetMark()}, Student name: {students.First().Name}");
        }


        static Student EnterStudent()
        {
            Console.Write("Enter student name: ");
            string name = Console.ReadLine();


            int[] marks = EnterMarks();
            return new Student(name, marks);
        }


        static int[] EnterMarks()
        {
            while (true)
            {
                Console.Write("Enter 3 marks separated by a space: ");
                string input = Console.ReadLine();


                var splitted = input.Split(' ');
                if (splitted.Length != 3)
                {
                    Console.WriteLine("Enter only 3 values separated by a space.");
                    continue;
                }


                int[] values = splitted.Select(x => int.TryParse(x, out int mark) ? mark : -1).ToArray();
                if (values.Any(x => x < 0 || x > 100))
                {
                    Console.WriteLine("Some of the values are incorrect, enter only valid marks from 0 to 100");
                    continue;
                }


                return values;
            }
        }
    }


    class Student
    {
        public string Name { get; private set; }
        private int[] marks = new int[3];


        public Student(string name, int[] marks)
        {
            Name = name;
            this.marks = marks;
        }


        public int GetMark() 
        {
            return marks?.Max() ?? 0;
        }


        public override string ToString()
        {
            return $"Name: {Name}, Marks: {string.Join(" ", marks)}, Highest mark: {GetMark()}";
        }
    }
}

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
New on Blog
APPROVED BY CLIENTS