Answer to Question #333216 in C# for Lira Scene

Question #333216

Write a user defined method named getMark that can be used to get a valid mark from the user. 

Your method must continuously ask the user for a mark, until a valid mark is entered (valid marks 

are between 0 and 100). If a user enters an invalid mark, display an error message before asking 

for a valid mark to be entered. The method must return the valid value to the calling program.

Write a user defined method named isPass that can be used to determine whether a mark is a 

pass mark (greater or equal to 50). The method must take as input a mark, and return a boolean, 

indicating whether the mark was a pass mark.

Write a program that reads in 5 marks by using the getMark method to ensure that valid marks are 

processed. You need to calculate the sum and the average of the marks. Your program must also 

use the isPass method to determine the number of pass marks entered. When the 5 marks have 

been processed you need to display the sum, average and number of passes (from these marks).


1
Expert's answer
2022-04-24T17:15:55-0400
using System;
using System.Linq;
using System.Text;


class Program
{
    static void Main()
    {
        int[] marks = new int[5];
        Console.WriteLine("Input 5 marks.");
        for (int i = 0; i < marks.Length; i++)
        {
            Console.Write($"{i + 1}.");
            marks[i] = GetMark();
        }


        Console.WriteLine($"Marks: {string.Join(" ", marks)}");
        Console.WriteLine($"Sum: {marks.Sum()}");
        Console.WriteLine($"Avg: {marks.Average()}");
        Console.WriteLine($"Number of passes marks: {marks.Count(x => IsPass(x))}");
    }


    static int GetMark()
    {
        while (true)
        {
            Console.Write("Enter a mark (0-100): ");
            if (int.TryParse(Console.ReadLine(), out int output) && output >= 0 && output <= 100)
                return output;


            Console.WriteLine("Incorrect input! Please, try again.");
        }
    }


    static bool IsPass(int mark)
    {
        return mark >= 50;
    }
}

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