Is there something wrong with the sorting of array?

-4

Here is my whole code, but I think the only problem lies with the case 4 and I've been racking my brains to find out whats wrong with it.. to no avail. Everything else runs perfectly but the moment I try to key in 4, the whole code just freeezes and it shows process returned -1073741819 (0xC0000005)

int main() {

  int FHA[] = {
    2632,
    2341,
    2303,
    3348,
    3149
  };
  int NoticeFSO[] = {
    2164,
    2634,
    2403,
    3069,
    3748
  };
  int CourtProsecution[] = {
    49,
    74,
    58,
    70,
    91
  };
  int i = 0;
  int total = 0;
  int enforcementYear[] = {
    2011,
    2012,
    2013,
    2014,
    2015
  };
  int difference;
  int max = 0;
  int j, a, n;
  char subuserInput;

  char userInput;
  printf("Enter 1 to display the total number of cases for the selected enforcement type.\nEnter 2 to display the highest number of case for the Fire Hazard Abatement.\nEnter 3 to find the yearly difference between Fire Hazard Abatement and Notice of Fire Safety Offences for each year from 2011-2015.\nEnter 4 to display the number of cases for Court Prosecution enforcement from the highest to the lowest with their corresponding years");
  scanf(" %c", & userInput);

  switch (userInput) {
  case '1':
    printf("Case A is for Fire Hazard Abatement.\nCase B is for Notice of Fire Safety Offenses.\nCase C is for Court Prosecution.\n");
    printf("Please enter your selection \n");

    scanf(" %c", & subuserInput);

    switch (subuserInput) {
    case 'A':
    case 'a':
      for (i = 0; i <= 4; i++) {
        total = total + FHA[i];
        printf("\n TOTAL: %d", total);
      }
      printf("\nTotal number of cases for Fire Hazard Abatement is %d.\n", total);
      break;

    case 'B':
    case 'b':
      for (i = 0; i <= 4; i++) {
        total = total + NoticeFSO[i];
      }
      printf("\ntotal number of cases for Notice of Fire Safety Offenses is %d.\n", total);
      break;

    case 'C':
    case 'c':
      for (i = 0; i < 5; i++) {
        total = total + CourtProsecution[i];
      }
      printf("\ntotal number of cases for court prosecution is %d.\n", total);
      break;

    }
    break;

  case '2':

    if ((FHA[0] > FHA[1]) && (FHA[0] > FHA[2]) && (FHA[0] > FHA[3]) && (FHA[0] > FHA[4]))
      max = FHA[0];
    if ((FHA[1] > FHA[0]) && (FHA[1] > FHA[2]) && (FHA[1] > FHA[3]) && (FHA[1] > FHA[4]))
      max = FHA[1];
    if ((FHA[2] > FHA[0]) && (FHA[2] > FHA[1]) && (FHA[2] > FHA[3]) && (FHA[2] > FHA[4]))
      max = FHA[2];
    if ((FHA[3] > FHA[0]) && (FHA[3] > FHA[1]) && (FHA[3] > FHA[2]) && (FHA[3] > FHA[4]))
      max = FHA[3];
    if ((FHA[4] > FHA[0]) && (FHA[4] > FHA[1]) && (FHA[4] > FHA[2]) && (FHA[4] > FHA[3]))
      max = FHA[4];

    printf("the highest number of cases for the Fire Hazard Abatement is %d", max);

    break;

  case '3':
    for (i = 0; i < 5; i++) {
      difference = FHA[i] - NoticeFSO[i];

      printf("Case difference is: %d and the year is %d\n", difference, enforcementYear[i]);
      difference = 0;
    }
    printf("Highest difference is : -599 and the year is 2015");
    break;

  case '4':

    for (i = 0; i < n; ++i) {
      for (j = i + 1; j < n; ++j) {
        if (CourtProsecution[i] > CourtProsecution[j]) {
          a = CourtProsecution[i];
          CourtProsecution[i] = CourtProsecution[j];
          CourtProsecution[j] = a;
        }
      }
    }
    printf("The numbers arranged in ascending order order are given below : %d\n", CourtProsecution[i]);
    break;

  }
  return 0;
}
c
asked on Stack Overflow Jan 3, 2020 by Lukie • edited Jan 4, 2020 by Lukie

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0