This C Program checks 2 elements in the array such that difference between them is largest. This program finds maximum differnce between the 2 array elements.
Here is source code of the C Program to find 2 elements in the array such that difference between them is largest.. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find 2 Elements in the Array such that Difference between them is Largest
*/
#include <stdio.h>
int maximum_difference(int array[], int arr_size)
{
int max_diff = array[1] - array[0];
int i, j;
for (i = 0; i < arr_size; i++)
{
for (j = i + 1; j < arr_size; j++)
{
if (array[j] - array[i] > max_diff)
max_diff = array[j] - array[i];
}
}
return max_diff;
}
int main()
{
int array[] = {10, 15, 90, 200, 110};
printf("Maximum difference is %d", maximum_difference(array, 5));
getchar();
return 0;
}
$ cc pgm97.c $ a.out Maximum difference is 190
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Related Posts:
- Apply for C Internship
- Check C Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs