Question & Answer: Suppose f(x):R rightarrow R be a function given by Find maximum and minimum of functional values using an…..

3) Suppose f(x): R R be a function given by F (x) 33 25 71 83 22 47 51 Find maximum and minimum of functional values using an algorithm with programming code. Find complexity of the algorithm

Suppose f(x):R rightarrow R be a function given by Find maximum and minimum of functional values using an algorithm with programming code. Find complexity of the algorithm.

Expert Answer

 Algorithm :

Step1 : start

Step2 : function declaration of max
Step3 : function declaration of min
Step4 : array declaration
Step5 : for(x=0;x<8;x++)
Step6 : print the elements of the F
Step7 : calling function to max
Step8 : calling function to min
Step9 : print the maximum element of the F
Step10 : print the minimum element of the F
Step11 : called function of max
Step12 : maxInt=F[0];
Step13 : for(x=0; x<8; x++)
Step14 : temp=F[x];
Step15 : if(temp>maxInt)
Step16 : maxInt=temp;
Step17 : return maximum value
Step18 : called function of min
Step19 : minInt=F[0];
Step20 : for(x=0; x<8; x++)
Step21 : temp=F[x];
Step22 : if(temp<minInt)
Step23 : minInt=temp;
Step24 : return minimum value

Step25 : stop

Program :

#include <iostream>
using namespace std;
int findMax(int F[], int index); // function declaration
int findMin(int F[], int index); // function declaration
int main()
{
int F[8]={33,25,71,83,22,47,51,36}; // F declaration
int x,max,min;
for(x=0;x<8;x++)
{
cout<<“F[“<<x<<“] = “<<F[x]<<endl; // print the elements of the F
}
max=findMax(F,8); // calling function
min=findMin(F,8); // calling function
cout<<“The maximum element is “<<max<<endl; // print the maximum element of the F
cout<<“The minimum element is “<<min<<endl; // print the minimum element of the F
return 0;
}
int findMax(int F[], int index) // called function
{
int maxInt,temp,x;
maxInt=F[0];
for(x=0; x<8; x++) // loop to find maximum element of an F
{
temp=F[x]; // assign F element in temp
if(temp>maxInt) // if the F element is less than the maximum value
{
maxInt=temp; // change maximum value
}
}
return maxInt; // return maximum value
}
int findMin(int F[], int index) // called function
{
int minInt,temp,x;
minInt=F[0];
for(x=0; x<8; x++) // loop to find minimum element of an F
{
temp=F[x]; // assign F element in temp
if(temp<minInt) // if the F element is less than the minimum value
{
minInt=temp; // change minimum value
}
}
return minInt; // return minimum value
}

Output :

Complexity of algorithm :

  • Here for loop for n times.
  • The time complexity of for loop is O(n).
  • Since the for loop consumes more time which is O(n) and remaining code consumes less than O(n).

So Time complexity of algorithm is O(n).

Still stressed from student homework?
Get quality assistance from academic writers!