0

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

7
  • 7
    for(i=0;i<31;i++){ will go outside the range of july[30] Commented Jun 20, 2019 at 15:48
  • 5
    a, b, and c are not good variable names. Use something more descriptive. Commented Jun 20, 2019 at 15:49
  • 5
    Also, the printf is only printing two out of three passed variables. Commented Jun 20, 2019 at 15:49
  • 2
    ALso july[i+1] can access out of the array or in the best case to a non initialized value Commented Jun 20, 2019 at 15:53
  • 3
    keep your solutions simple when learning. do the total and min max searching in different loops. It will be more clear what is happening. Commented Jun 20, 2019 at 16:02

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

4 Comments

From the problem statement by the OP, the array size should be 31. Using an array is not even required, a simple double variable would suffice. The OP is probably expected to write 2 separate loops: one to read the values into an array, and a second one to compute the sum, min and max of the values in the array, where the initial values are easier to determine (use the first element of the array).
Another interesting point to address is whether NaN values should produce NaN results or be ignored...
@chqrlie hello, cela faisait longtemps, tu as diminué ton activité ici, ceci dit moi aussi et plus ça va plus je pense carrément arrêter, j'ai un certain raz le bol de certaines pratiques ...
Bonjour Bruno, l'activité est assez variable, comme l'intérêt des questions :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.