-2

This is the problem statement:

HackerLand University has the following grading policy:

Every student receives a 'grade' in the inclusive range from 0 to 100. Any 'grade' less than '40' is a failing grade.

Sam is a professor at the university and likes to round each student's 'grade' according to these rules:

If the difference between the 'grade' and the next multiple of 5 is less than 3, round 'grade' up to the next multiple of 5. If the value of 'grade' is less than 38, no rounding occurs as the result will still be a failing grade.

For example ' grade= 84 ', will be rounded to 85 but 'grade = 29' will not be rounded because the rounding would result in a number that is less than '40'.

Given the initial value of 'grade' for each of Sam's 'n' students, write code to automate the rounding process.

#include<iostream>
using namespace std;

int main()
{
  int n,x;
  cin >> n;
  for (int i = 0; ++i; i < n)
  {
    if(x >= 38)
    {
      cin >> x;
      int y = x;
      while(1)
      {
        if (y % 5 == 0)
          break;
        y++;
      }
      if (y - x <= 2)
      {
        x=y;
      }
    }

    cout << x << "\n";
  }

  return 0;
}

The code runs fine every time i input a value more than or equal to 38 but as soon as I input a lesser value an infinite loop is encountered displaying the input number .

Please tell me why i am i getting this error and how to fix it.

payo
  • 4,501
  • 1
  • 24
  • 32

3 Answers3

1

Your this part is wrong.

for (int i = 0; ++i; i<n)
  {
    if(x >= 38)
    {
      cin >> x;
      int y = x;

Make it to this:

for (int i = 0; i < n; i++)
  {
    cin >> x;
    if(x >= 38)
    {
      int y = x;

Two things you did wrong:

  1. You swapped the test expression and the increment parts. So the for loop works as: setup -> loop-part(your code) -> increment -> test-expression. Your test-expression was always true since ++i basically returns the reference to i after incrementing(since it's not zero, it's evaluated to true).

  2. You need to take the input before putting the if check on it.

theWiseBro
  • 1,439
  • 12
  • 11
0

You're making it harder than it needs to be:

#include<iostream>

int main()
{
  int n, x;
  std::cin >> n;
  for(int i = 0; i < n; ++i)
  {
    std::cin >> x;
    x = (x - 5 * (x / 5) < 3) || x < 38 ? x : 5 * ((x + 2) / 5);
    std::cout << x << std::endl;
  }
  return 0;
}
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • @Dialecticus it's a bit "code golf" but it encapsulates the rounding algorithm in a single line – Allan Cameron Feb 13 '20 at 12:16
  • I like short code, but I like readable code even more. That long expression may cause lots of headache later, when someone tries to understand, fix or change it. Even for an experienced coder it is not trivial to parse that one quickly. – SKCoder Feb 13 '20 at 12:17
  • Maybe I'm missing something, but why do we even need the `(x - 5 * (x / 5) < 3)` part? Doesn't rounding cover all cases above 38? – Dialecticus Feb 13 '20 at 12:23
  • @Dialecticus the OP only mentions rounding up, not rounding down. Without this bit you'll also round down above 38. – Allan Cameron Feb 13 '20 at 12:40
0
public static List<Integer> roundingNumbers(List<Integer> grades) {

    List<Integer> roundedGrades = new ArrayList<>();

    for (int i = 0; i < grades.size(); i++) {
        int nextMultiple = 0;      
        if ((grades.get(i) % 5) == 0) {
            nextMultiple = (grades.get(i) / 5) * 5;
        }
        else {
            nextMultiple = ((grades.get(i) / 5) + 1) * 5;
        }
        if(grades.get(i)>=38 && ((nextMultiple-grades.get(i))<3 )) {
            roundedGrades.add(nextMultiple);
        }else {
            roundedGrades.add(grades.get(i));
        }          
    }
    return roundedGrades;

}
vikram
  • 23
  • 4