Advertisement
joharido

Untitled

Mar 9th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. /* Passing an array to a function. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX 20
  8.  
  9.  
  10. int largest(int * x);
  11. void display(int *arr, int leng);
  12.  
  13.  int main(int argc, char *argv[])
  14.  {
  15.      int array[MAX], count;
  16.  
  17.      /* Input MAX values from the keyboard. */
  18.      int i;  count=0;
  19.      while ( scanf("%d", &i) != EOF){
  20.         *(array + count) = i; // store in array[count]
  21.         count++;
  22.      }
  23.      //int p = scanf("%d", &i);
  24.  
  25.       /* Call the function and display the return value. */
  26.       printf("Inputs: ");
  27.       display(array, count);
  28.  
  29.      
  30.      
  31.      //printf("\nLargest value: %d\n", largest(array));
  32.      
  33.      return 0;
  34.  }
  35.  
  36.  /* display a int array */
  37.  
  38.  void display(int *arr, int leng)
  39.  {
  40.      int i = 0;
  41.      while (i < leng){
  42.          printf("%d ", *arr);
  43.          arr++;
  44.          i++;
  45.      }
  46.  }
  47.  
  48.  
  49. /* Function largest() returns the largest value */
  50.  /* in an integer array */
  51.  
  52.  int largest(int * arr)
  53.  {
  54.      int *ptr, max;
  55.      ptr = arr;
  56.      max = 0;
  57.      int size = sizeof(arr)/sizeof(int);
  58.      while (size >= 0){
  59.          if (*ptr > max){
  60.              max = *ptr;
  61.          }
  62.          ptr++;
  63.          size--;
  64.      }
  65.      
  66.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement