I want to write a code that takes a sequence of complex numbers and searches for a number in it. But I don’t know how to write a function to form the sequence. may you help?
class complex{
private:
int real;
int image;
int n;
int *a;
public:
complex (float l,float k) : real(l),image(k){}
float set(float l,float k)
{
cout<<"enter size of array: ";
cin>>n;
for (int i=0;i<n;i++)
{
cout<<"enter real part: ";
cin>>l;
cout<<"enter image part: ";
cin>>k;
*(a+i)=complex(l,k);
}
}
};
>Solution :
We can use std::complex instead of creating our own datatype. The other problem is that a is undefined as is and why not just use a[i]. Also instead why not create a typical array? You also dont need arguments or a return type for your function.
Something like:
class complexArray{
private:
int n;
std::complex* a;
public:
void set()
{
cout << "enter size of array: ";
cin >> n;
a = new std::complex[n];
for (int i = 0; i < n; i++)
{
float real;
float image;
cout << "enter real part: ";
cin >> real;
cout << "enter image part: ";
cin >> image;
a[i] = std::complex(real, image);
}
}
};