uni

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

ptrs_ex3.c (1169B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
      5 
      6 /* 
      7  * Modify arrays_ex3.c to use pointer arithemtic instead
      8  * of indexing
      9  */
     10 
     11 int
     12 main(int argc, char *argv[])
     13 {
     14         float sum, avg;
     15         int arr[10], i;
     16         int max, min, maxloc, minloc;
     17 
     18         sum = 0;
     19         for (i = 0; i < ARRLEN(arr); i++) {
     20                 do {
     21                         printf("arr + %d: ", i);
     22                         scanf("%d", arr + i);
     23                         getchar();
     24                 } while (*(arr + i) < 0 || *(arr + i) > 100);
     25 
     26                 sum += *(arr + i);
     27         }
     28 
     29         max = min = *arr;
     30         maxloc = minloc = 0;
     31         for (i = 0; i < ARRLEN(arr); i++) {
     32                 if (arr[i] < min) {
     33                         min = *(arr + i);
     34                         minloc = i;
     35                 }
     36                 if (arr[i] > max) {
     37                         max = *(arr + i);
     38                         maxloc = i;
     39                 }
     40         }
     41 
     42         avg = sum / (float)ARRLEN(arr);
     43         printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n",
     44                 max, maxloc, min, minloc, avg);
     45 
     46         return 0;
     47 }