Write a program to find the maximum difference between two adjacent numbers in an array of positive integers
#include <stdio.h>
int max_difference(int a[], int n) {
int max_dif = 0;
int i;
for (i=1; i<n; i++) {
if (abs(a[i] - a[i-1]) > max_dif) {
max_dif = abs(a[i] - a[i-1]);
}
}
return max_dif;
}
int main() {
int a[] = { 1, 5, 6, 1, 3, 44, 33, 98};
int m = max_difference(a, sizeof(a) / sizeof(a[0]));
printf("Maximum difference berween two adjacent numbers is %d\n", m);
return 0;
}
Comments
Leave a comment