Challenge in C. doubt

Asked

Viewed 42 times

-1

The challenge is this::

At the university of hackerlandia has the following policy of grades :

  • Every student receives a note which may vary from 0 to 100.
  • All note less than 40 is a red note.

Sam is a professor at the university and likes to round up notes of each student according to these rules:

  • If the difference between the note and the next multiple of 5.

  • If the value of note is less than 38, no rounding occurs as the note will be red.

For example, note = 84 will be rounded to 85, but 29 will not be rounded since it is less than 40.

The program entries are N number of banknotes and in a the value of the banknotes.

My program does not work when the variable N is greater than or equal to 4. If anyone can help me, I’d appreciate it.

#include <stdio.h>
#include <stdlib.h>

int main() {
//    define as variáveis N : numero de notas, num: valor das notas, r : resto da divisão por 5
    int N, num[N], r;

    scanf ("%i", &N);

    for (int i = 0; i < N; i++) {
        scanf ("%i", &num[i]);
    }

//    essa estrutura 'for' serve para ver qual 'num' (valor da nota) será arredondado
    for (int i = 0; i < N; i++) {
//            define o espaço em que 'num' está contido (0 <= num <= 100)
            if (num[i] < 0 || num[i] > 100) {
            printf ("erro");
            return (0);
        }
        if (num[i] >= 38) {
            r = num[i] % 5;
            if (r > 5) {
                r = r - 5;
            }
            switch (r) {
            case (0) :
                break;
            case (1) :
                break;
            case (2) :
                break;
            case (3) :
                num[i] = num[i] + 2;
                break;
            case (4) :
                num[i] = num[i] + 1;
                break;
            }

        }
        else
            num[i] = num[i];
    }

//    imprime o resultado das notas depois de arredondadas quando preciso
    for (int i = 0; i < N; i++) {
            printf ("%i\n", num[i]);
    }


    return (0);

}
  • 1

    Post the statement int num[N] after the reading of N. This test: r = num[i] % 5; if (r > 5) { r = r - 5;} it doesn’t make sense because r will always vary between 0 and 4 (see module operator definition %).

1 answer

0

The error occurs because you are declaring the array variable size before initializing this variable. Follow the corrected code.

#include <stdio.h>
#include <stdlib.h>

int main() {
  //    define as variáveis N : numero de notas, num: valor das notas, r : resto
  //    da divisão por 5
  int N, r;

  scanf("%i", &N);

  int num[N];

  for (int i = 0; i < N; i++) {
    scanf("%i", &num[i]);
  }

  //    essa estrutura 'for' serve para ver qual 'num' (valor da nota) será
  //    arredondado
  for (int i = 0; i < N; i++) {
    //            define o espaço em que 'num' está contido (0 <= num <= 100)
    if (num[i] < 0 || num[i] > 100) {
      printf("erro");
      return (0);
    }
    if (num[i] >= 38) {
      r = num[i] % 5;
      if (r > 5) {
        r = r - 5;
      }
      switch (r) {
        case (0):
          break;
        case (1):
          break;
        case (2):
          break;
        case (3):
          num[i] = num[i] + 2;
          break;
        case (4):
          num[i] = num[i] + 1;
          break;
      }

    } else
      num[i] = num[i];
  }

  //    imprime o resultado das notas depois de arredondadas quando preciso
  for (int i = 0; i < N; i++) {
    printf("%i\n", num[i]);
  }

  return (0);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.