Returns the largest value stored in an array of int. - C Pointer

C examples for Pointer:Array Pointer

Description

Returns the largest value stored in an array of int.

Demo Code

#include <stdio.h>  
#define LEN 10   /* w  w w  .j  av  a 2 s  .  c om*/
  
int max_arr(const int ar[], int n);  
void show_arr(const int ar[], int n);  
  
int main(void)  
{  
    int orig[LEN] = {1,2,13,4,5,6,7,8,9,10};  
    int max;  

    show_arr(orig, LEN);  
    max = max_arr(orig, LEN);  
    printf("%d = largest value\n", max);  
      
    return 0;  
}  
  
int max_arr(const int ar[], int n)  
{  
    int i;  
    int max = ar[0];  
    for (i = 1; i < n; i++)  
        if (max < ar[i])  
            max = ar[i];  
    return max;  
}  
  
void show_arr(const int ar[], int n)  
{  
    int i;  
      
    for (i = 0; i < n; i++)  
        printf("%d ", ar[i]);  
    putchar('\n');  
}

Result


Related Tutorials