Algorithm and Flowchart to find the Maximum Absolute Difference Among the given Array

[8304 views]




Algorithm for Maximum Absolute Difference

Declare int a[100], i, n, res, j; Read n; Read array a; for (i = 0 to i = n) implement by 1 { for (j = i to j = n) implement by 1 { check if (absolute of a[i] - a[j] > res) { res = abs(a[i] - a[j]); } } } print(res)

In this algorithm, first we declare an array of int a[100] to store array elements and the variable i and j to run the loop, n for getting the no. of elements in the array.

Res is for storing the result. We Read variable n and array a. We run for loop i=0 to i=n and inside that another loop for which will run from j=i to j=n.

We will check abs(a[i]-a[j]>res), it means that when we are subtracting a[0]-a[0] then a[0]-a[1] ,a[0]-a[2] ,a[0]-a[3]....a[0]-a[n].So we can see that when i=0 j will run from 0 to n then i=1 j will run from 1 to n. Likewise it will happen for all the numbers. So we are checking the subtraction of two elements and it's absolute value whether is greater than res or not. If it is greater it will get store in res otherwise it will pass.

Flowchart to find the Maximum Absolute Difference:

Algorithm and Flowchart to find the Maximum absolute difference among the given array
Remove WaterMark from Above Flowchart

Implementation of Program:

#include<stdio.h> #include<stdlib.h> int maxv(int a[], int n); int main() { int a[100], i, n, res; printf("Enter the no. of inputs:"); scanf("%d", & n); printf("Enter the elements of array:"); for (i = 0; i < n; i++) { scanf("%d", & a[i]); } for (i = 0; i < n; i++) { for (j = i; j < n; j++) { if (abs(a[i] - a[j]) > res) { res = abs(a[i] - a[j]); } } } printf(" Maximum absolute difference is %d", res); }

Output:

Algorithm and Flowchart to find the Maximum absolute difference among the given array
                 



Want to Learn How to write own Algorithm and Flowcharts



Want to test your logical skills in Algorithms?



Comments










Search

Hot Deals ends in













Technical Quizzes Specially For You:

Games:
     



Search Tags

    Algorithm to find the Maximum Absolute Difference

    Flowchart for Maximum Absolute Difference

    Pseudocode for Maximum Absolute Difference