The C program find the MAX and MIN using pointers in an array of integers is written using Dev-C++ version 184.108.40.206 installed on a 64-bit windows 7 operating system. The program receives an array of numbers as input and find the minimum number and a maximum number using pointers.
This program is intended for intermediate level learners familiar with the concept of pointers. If you are not familiar with pointer concepts, visit the link below to learn about pointers with example.
You can also learn about implementing linear search and binary search algorithms, it will strengthen your concepts.
We want to write a program to find the MAX and MIN using pointers in an array of numbers. The program accept 10 numbers from user and store them in an array of integers.
We then use two pointers to locate the maximum and the minimum of the array. The pointer traverse through all the values of the array and compares it with value of max which is 0 at the beginning.
If the number in array greater than max current value, then make the new number as new max value, otherwise keep looking for another max. The same logic applies to minimum of array.
Flowchart – Find Max and Min using Pointers
To understand the logic of the program see the flowchart given below.
Program Source Code – Find MAX and MIN using Pointers
int i,j,temp,tempmin, max,min;
max = temp = tempmin = 0;
min = arr;
printf("Enter number upto 10 numbern");
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = arr;
if(max <= *ptr1)
temp = max;
max = *ptr1;
*ptr1 = temp;
// finding minimum
if(min >= *ptr2)
tempmin = *ptr2;
*ptr2 = min;
min = tempmin;
Output – Find MAX and MIN using Pointers
The output of the program is given below. The array stores the 10 numbers and MAX and MIN is found using pointers. The MAX value in example is 87 and MIN value is 5 after the program completes.