finding min and max in array using pointers

Solutions on MaxInterview for finding min and max in array using pointers by the best coders in the world

showing results for - "finding min and max in array using pointers"
Joseph
21 Feb 2018
1#include <stdio.h>
2
3void minmax(int ara[],int n,int *min,int *max){
4    int i;
5    *max=*min=ara[0];
6    for(i=1;i<n;i++){
7        if(ara[i] < *min){
8            *min=ara[i];
9        }
10        if(ara[i] > *max){
11            *max=ara[i];
12        }
13    }
14}
15
16
17int main()
18{
19   int n,i;
20   printf("How many elements do you want in you array:\n");
21   scanf("%d",&n);
22   int ara[n];
23   for(i=0;i<n;i++){
24       scanf("%d",&ara[i]);
25   }
26   printf("\n");
27   printf("The created array is:\n");
28   
29   for(i=0;i<n;i++){
30       printf("%d ",ara[i]);
31   }
32   printf("\n\n");
33   //
34   int min,max;
35   
36   minmax(ara,n,&min,&max);
37   
38   printf("The maximum value of the array is : %d and the minimum value of the array is %d\n",max,min);
39   
40
41    return 0;
42}
43