Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following algorithm. algorithm Secret (A [0..n - 1|) Input: An arra

ID: 3861743 • Letter: C

Question

Consider the following algorithm. algorithm Secret (A [0..n - 1|) Input: An array A [0..n - 1] of n real numbers mineral leftarrow A [0]: leftarrow A [0] for i leftarrow 1 to n - 1 do if A [i] return manual - manual What does this algorithm compute? What is its basic operation? How many times is the basic operation executed? What is the efficiency class of this algorithm? Suggest an improvement or a better algorithm altogether and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.

Explanation / Answer

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Answer =>   i wrote a c program based on given algorithm . it always return the differnce the maximum and minimum value element of any given array . you can run it . this program will give desired output .

#include<stdio.h>
int function();
int main()
{
int i= function();
printf("so the function output maxval-minval is =%d ",i);
return 0;
}
int function()
{
int arr[30];
int i,n;
int minval,maxval;
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
minval=arr[0];
maxval = arr[0];
for(i=0; i<= (n-1); i++)
{
if(arr[i] <minval)
{
minval=arr[i];
}
if(arr[i] > maxval)
{
maxval = arr[i];
}
}
return maxval-minval;
}

=============================

OUTPUT -> Enter the number of elements in the array :  6

User Entered array elements :   3 4 8 12 5 7

  so the function output maxval-minval is = 9

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

complete algoritm description =>

* Firstly , we take an array A of n real numbers

* minval = A[0] ; /* means store the first element of array in minval variable means in starting we take A[0] element value as minimum value of this array */

* maxval = A[0] ; /* means store the first element of array in maxval variable means in starting we take A[0] element value as maximum value of yhis array */

* for loop from i=1 to n-1 /* if array has n values */

when i=1 ,

* if A[1] < minval /* compare A[1]<minval and if condition true .. then

* minval = A[1] /* means now minimum value is A[1] because it is less than our assumed minimum value


* if A[1] > maxval /* compare A[1] > maxval and if condition true ... then

* maxval = A[1] /* means now maximum value is A[1] because it is less than our assumed maximum value . -

when i=2 ,

* if A[2] < minval /* compare A[2]<minval and if condition true .. then

* minval = A[1] /* means now minimum value is A[2] because it is less than our assumed minimum value


* if A[2] > maxval /* compare A[2] > maxval and if condition true ... then

* maxval = A[2] /* means now maximum value is A[2] because it is less than our assumed maximum value


when i=3,

* if A[3] < minval /* compare A[3]<minval and if condition true .. then

* minval = A[3] /* means now minimum value is A[3] because it is less than our assumed minimum value


* if A[3] > maxval /* compare A[3] > maxval and if condition true ... then

* maxval = A[3] /* means now maximum value is A[3] because it is less than our assumed maximum value

...
..
.
.
.
.
. <= This process goes on till we reach on the last element of the array .......................
.
..

.
.

.
.
* finally , maxval holds the maximum value of array and minval holds the minimum value of array

* so it returns , maxval-minval => /* means it returns the difference between the maximum value and minimum value of any given array

---------------------------------------------------------------------------------------------------------------------------------------------------------------------