0% found this document useful (0 votes)
9 views2 pages

A2 Ques3

Data structures practicals

Uploaded by

mussu0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

A2 Ques3

Data structures practicals

Uploaded by

mussu0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*Q3.

Describe a recursive algorithm for finding the maximum


element in an array A of n elements. What is your running
time and space usage? */
#include <iostream>
using namespace std;
class Myarray
{
int *array;
int size;
public:
Myarray(int x=0) // Default and Parameterized Constructor
{
size=x;
if (size==0)
{
cout<<"Enter Size of the Array:";
cin>>size;
}
array=new int [size];
for (int i=0;i<size;i++)
{
array[i]=0; // Initilizing Array
}
}
~Myarray()
{
delete []array;
}
void getarray() // Getting values for array
{
for (int i=0;i<size;i++)
{
cout<<"Enter the value for Array["<<i<<"]:";
cin>>array[i];
}
}
void display()
{
cout<<"[";
for (int i=0;i<size;i++)
{
cout<<array[i];
if (i!=size-1)
{
cout<<",";
}
}
cout<<"]"<<endl;
}
int maximum(int index=0) // Time Complexity = O(n)
{ // Space Complexity = use of more
if (index==size-1) //space than iterative version
{
return array[index];
}
else
{
int max=maximum(index+1);
if (max<array[index])
{
max=array[index];
}
return max;
}
}
};
int main()
{
Myarray obj;
obj.getarray();
cout<<"Maximum Element is : "<<obj.maximum();
return 0;
}

You might also like