Search code examples
cif-statementnumbersmaxmin

Display the maximum and the minimum of a row of numbers the ending signal is a 0


I've found a French exercise online. This is a translation of the wording:

Write a program to display the maximum and the minimum of a row of integers (the row is one or more inputs by the user). The inputs aren't kept in memory. The row ends by 0.

I've written some code and every time I try to compile I get an error message saying that I'm missing a ";" right at the end of my else. Bizarre.

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

void main(int argc, char *argv[]) {
  int max;
  int min;

  int nombre;

  printf("entrez un entier\n");
  scanf("%d", &nombre);
  max = min = nombre;
  while (nombre != 0) {
    if (nombre > max) {
      max = nombre;
    } else(nombre < min) {
      min = nombre;
    }
    printf("entrez un entier\n");
    scanf("%d", &nombre);
  }
  printf("le max est %d et le min est %d", max, min);
}

Solution

  • The else statement doesn't take a condition. The statements within it are executed if and only if all the if and else if conditions before it are false. So, it don't take a condition itself. So, you need to replace the else with an else if like that:

        if (nombre > max) {
          max = nombre;
        } else if(nombre < min) {
          min = nombre;
        }