Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I write a array of complex number?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
            }
        }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading