Thabang Bambo
Thabang Bambo

Reputation: 9

Student grading program (I am a beginner)

The program was running properly and now it not running anymore.

I wrote the code in Visual Studio first and the question is from Hackerrank.com, so the compiler from Hackerrank does not want to run this code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution {
    /*
     * Complete the gradingStudents function below.
     */
    static int[] gradingStudents(int[] grades) {
       int fail = 40;

       for (int i = 0; i < grades.Length; i++)
       {
           if (grades[i] < fail)
           {
               double failcheck = Math.Round((double)grades[i] / 5, MidpointRounding.AwayFromZero) * 5;

               if (failcheck < fail)
               {
                   Console.WriteLine(grades[i]);
               }
               else
               {
                   Console.WriteLine(failcheck);
               }
           }
        }

        return grades;
    }

    static void Main(string[] args) {
        TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());
 grades = new int [n];

        for (int gradesItr = 0; gradesItr < n; gradesItr++) {
            int gradesItem = Convert.ToInt32(Console.ReadLine());
            grades[gradesItr] = gradesItem;
        }

        int[] result = gradingStudents(grades);

        tw.WriteLine(string.Join("\n", result));

        tw.Flush();
        tw.Close();
    }
}

Sample Input 0

4
73
67
38
33

Sample Output 0

75
67
40
33

Upvotes: 0

Views: 971

Answers (2)

Thabang Bambo
Thabang Bambo

Reputation: 9

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution {

    /*
     * Complete the gradingStudents function below.
     */
    static int[] gradingStudents(int[] grades) {
       for (int i = 0; i < grades.Length; i++)
        {

if (grades[i] >= 38)
            {
                int nextMultiple = grades[i];
                while (nextMultiple % 5 != 0)
                {
                    nextMultiple++;
                }
                if(nextMultiple - grades[i] < 3)
                {
                    grades[i] = nextMultiple;
                }
            }


        }
        return grades;


    }

    static void Main(string[] args) {
        TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());

        int[] grades = new int [n];

        for (int gradesItr = 0; gradesItr < n; gradesItr++) {
            int gradesItem = Convert.ToInt32(Console.ReadLine());
            grades[gradesItr] = gradesItem;
        }

        int[] result = gradingStudents(grades);

        tw.WriteLine(string.Join("\n", result));

        tw.Flush();
        tw.Close();
    }
}

Upvotes: 0

Ramin Rahimzada
Ramin Rahimzada

Reputation: 492

If your problem is this , Then based on hackerrank problem description you should change method gradingStudents as:

static int[] gradingStudents(int[] grades) 
{
    for (int i = 0; i < grades.Length; i++)
    {
        var item = grades[i];
        if (item >= 38)
        {
            var diff = 5 - (item % 5);
            if (diff < 3)
                grades[i] = item + diff;
        }
    }

    return grades;
}

Upvotes: 1

Related Questions