uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

arrays_ex2.c (1090B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <time.h>
      4 
      5 #define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
      6 
      7 /*
      8  * Modify arrays_ex1.c to find max, maxloc, min, minloc,
      9  * and the average value
     10  */
     11 
     12 int
     13 main(int argc, char *argv[])
     14 {
     15         float sum, avg;
     16         int arr[10], i;
     17         int max, min, maxloc, minloc;
     18 
     19         srand(time(NULL));
     20         sum = 0;
     21         for (i = 0; i < ARRLEN(arr); i++) {
     22                 arr[i] = rand() % 101;
     23                 sum += arr[i];
     24                 printf("arr[%d]: %d\n", i, arr[i]);
     25         }
     26 
     27         max = min = arr[0];
     28         maxloc = minloc = 0;
     29         for (i = 0; i < ARRLEN(arr); i++) {
     30                 if (arr[i] < min) {
     31                         min = arr[i];
     32                         minloc = i;
     33                 }
     34                 if (arr[i] > max) {
     35                         max = arr[i];
     36                         maxloc = i;
     37                 }
     38         }
     39 
     40         avg = sum / (float)ARRLEN(arr);
     41         printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n",
     42                 max, maxloc, min, minloc, avg);
     43 
     44         return 0;
     45 }