C Find the largest complex number in an array using recursiv

C++: Find the largest complex number in an array using recursive method. The largest number is the one who has the largest (real)*(real) + (img)*(img)

Solution

#include <iostream>
using namespace std;

class Complex
{
public:
float real;
float img;
Complex()
{
real = img = 0.0;
}
  
Complex(float real,float img)
{
this->real = real;
this->img = img;
}
};

void findLargest(Complex arr[],int size,Complex &max)
{
if(size==0)
return;
else
{
float val = arr[size-1].real*arr[size-1].real+arr[size-1].img*arr[size-1].img;
float maxval = max.real*max.real+max.img*max.img;
  
if(val>maxval)
max = arr[size];
  
return findLargest(arr,size-1,max);
}
}

int main() {
   // your code goes here
   Complex c1(4,4);
   Complex c2(5,5);
   Complex arr[2];
   arr[0] = c1;
   arr[1] = c2;
  
   Complex max;
  
   findLargest(arr,2,max);
  
   cout << \"Max complex number is : \" << max.real << \" + i\"<< max.img;
   return 0;
}

C++: Find the largest complex number in an array using recursive method. The largest number is the one who has the largest (real)*(real) + (img)*(img)Solution#i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site