Alexandros Mylonas
Alexandros Mylonas

Reputation: 37

Problem with finding minimum and maximum values in C

I have an issue in finding the maximum and minimum value in an array with 31 objects.

I tried to find the maximum and minimum value by putting an if statement in a for statement, as it is below. Also, I wanted to find the sum of all values of the array, but none of these three targets was finally successful

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

int main() {
    double july[30], a, b, c;
    a = 0;
    b = 0;
    c = 0;
    int i;
    for (i = 0; i < 31; i++) {
        scanf("%lf", &july[i]);
        a = a + july[i];
        if (july[i + 1] <= july[i]) {
            b = july[i + 1];
        } else
        if (july[i + 1] >= july[i]) {
            c = july[i + 1];
        }
    }
    printf("%lf\n%lf", a, b, c);
    return 0;
}

When I run the code, I write all the values into the array, but in the end the result is a void, having an output of: "Process exited after 14.42 seconds with return value 3221225477 Press any key to continue . . ." and nothing else

Thank you in advance for your help

Upvotes: 0

Views: 292

Answers (1)

bruno
bruno

Reputation: 32586

Having double july[30] the valid indexes are from 0 up to 29 but having for(i=0;i<31;i++) the forms july[i] access to the indexes up to 30 and the forms july[i+1] access to the indexes up to 31, with an undefined behavior.

Furthermore inside the loop only the entries from 0 up to i can be set, so the forms july[i+1] access in the best case to a non initialized entry of the array. Out of that doing

if(july[i+1]<=july[i]){
    b=july[i+1];
    }
else if(july[i+1]>=july[i]){
    c=july[i+1];

does not allow to find the min/max, you need to compare the new entry with b and c

Just above I said can be set because you do not check the value return by scanf, if a non double representation is enter scanf stops definitively to set the entries, in case of an error you need to flush the invalid input and to redo the scanf.

In printf("%lf\n%lf",a,b,c); you want to print 3 values ( sum, min and max ) so a %lf is missing in the format.


A proposal can be :

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

int main() {
  double july[30];
  double sum = 0;
  double min = INFINITY;
  double max = -INFINITY;

  for (size_t i = 0; i != (sizeof(july) / sizeof(july[0])); ++i) {
    printf("enter value #%zu : ", i);

    while (scanf("%lf", &july[i]) != 1) {
      fputs("invalid value, reenter it : ", stderr);
      /* flush all the line */
      int c;

      while ((c = getchar()) != '\n') {
        if (c == EOF) {
          fputs("end of file, abort\n", stderr);
          return -1;
        }
      }
    }

    sum += july[i];

    if(july[i] < min)
      min = july[i];
    if (july[i] > max)
      max = july[i];
  }

  printf("sum=%f\nmin=%f\nmax=%f\n", sum, min, max);

  return 0;
}

Compilation and execution

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
enter value #0 : 1
enter value #1 : 4
enter value #2 : 2
enter value #3 : 5
enter value #4 : 88
enter value #5 : 78
enter value #6 : 12
enter value #7 : 11
enter value #8 : 3
enter value #9 : -5
enter value #10 : 12 
enter value #11 : 1
enter value #12 : 1
enter value #13 : 1
enter value #14 : 1
enter value #15 : 1
enter value #16 : 1
enter value #17 : 1
enter value #18 : 1
enter value #19 : 1
enter value #20 : 1
enter value #21 : 1
enter value #22 : 1
enter value #23 : 1
enter value #24 : aze
invalid value, reenter it : 1
enter value #25 : 1
enter value #26 : 1
enter value #27 : 1
enter value #28 : 1
enter value #29 : 1
sum=230.000000
min=-5.000000
max=88.000000
pi@raspberrypi:/tmp $ 

As you can see

  • I name the variable from their behavior making the code more clear, contrarily to a, b and c
  • the right type for an index is size_t
  • I do not use a literal integer but I use sizeof in the for to follow the size of the array even if it is changed
  • I use INFINITY to initialize min because any valid double is lower that it, and -INFINITY to initialize max because any valid double is greater that it
  • in case of an invalid input on scanf I flush all the line before to redo, you can also just read a word if you prefer
  • in the printf you do not need the 'l' because printf cannot receive a float whose are transformed to double (but the 'l' is necessary in scanf to read a double)

Upvotes: 4

Related Questions