1

I want to write 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. This is what I've tried so far:

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);
        }
    }
};
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
sogcheh
  • 21
  • 2
  • 18
    Why not use `std::complex` and `std::vector`? – perivesta Jun 12 '23 at 15:27
  • 1
    What is `a` doing here? It's never initialized, but you go ahead and use it, which is *bad*. In C++ try and avoid pointers whenever practical as they create way more trouble than most are prepared to deal with. – tadman Jun 12 '23 at 15:28
  • 4
    PSA: If you are using pointers, `a[i]` is *way* easier to understand than `*(a+i)`. – tadman Jun 12 '23 at 15:29
  • 4
    See [std::complex](https://en.cppreference.com/w/cpp/numeric/complex) and [std::literals::complex_literals](https://en.cppreference.com/w/cpp/numeric/complex/operator%22%22i) – Richard Critten Jun 12 '23 at 15:29
  • 2
    The code doesn't allocate any memory to store the objects. Do yourself a favor and follow the previous suggestions: use `std::vector`. – Bob__ Jun 12 '23 at 15:32
  • 4
    Your complex type shouldn't be the one creating and storing the array of complex types. That doesn't make any sense. A complex number just has a real and imaginary component. – Kevin Jun 12 '23 at 15:33
  • 3
    You are placing to much stuff in single function. Separate "complex" (or use `std::complex`) from array (use std::vector) and from io code (create separate function for reading single complex number and then write function which reads this ti an array). – Marek R Jun 12 '23 at 15:35

2 Answers2

2

You can use complex numbers from the standard library and make a vector (array) from them. With std::complex_literals you can even postfix floating point numbers with an i to do calculations with them. For your use case you can also use std::complex<int> (I missed that)

#include <complex>
#include <iostream>
#include <vector>

int main()
{
    using namespace std::complex_literals;
    std::vector<std::complex<double>> values{ {1.0,1.0}, {2.0,2.0}, {3.0,3.0} };

    for (const auto value : values)
    {
        std::cout << 3.0i * value << "\n";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • I do not have permission to use the library – sogcheh Jun 12 '23 at 15:56
  • Please [note](https://en.cppreference.com/w/cpp/numeric/complex) though, that *"The behavior [of `std::complex`] is unspecified (and may fail to compile) if T is not a cv-unqualified standard (until C++23) floating-point type"*. – Bob__ Jun 12 '23 at 15:58
  • Why not? Are you learning C++? Or is it a datastructures class. – Pepijn Kramer Jun 12 '23 at 15:58
  • @Bob__ Which is kind of translates to only use `int, float, double,...` and do not use `const` or `volatile` for T right? – Pepijn Kramer Jun 12 '23 at 16:01
  • Well, not really, it kinda say that you *can* [use](https://godbolt.org/z/fYedcGs3Y) `int`, but with *unspecified* results (although [predictable](https://godbolt.org/z/jo7dWaqK1), IMHO). – Bob__ Jun 12 '23 at 16:07
0

Because you arent allowed to use the library (which is stupid), what I would suggest is creating a struct to save the data

struct complexNum {             
  int real;        
  int imaginary;   
};

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 complex{  
    private:
        struct complexNum {             
            int real;        
            int imaginary;   
        };
        int n;
        complexNum* a;
    
    public:        
        void set()
        {
            cout<<"enter size of array: ";
            cin>>n;
            a = new complexNum[n];
            for (int i=0; i<n; i++)
            {
                complexNum num;
                cout<<"enter real part: ";
                cin>>num.real;
                cout<<"enter image part: ";
                cin>>num.imaginary;
                a[i]=num;
            }
        }
};
ajgrinds
  • 186
  • 11
  • 1
    I would recommend using the standard libarary. It already has support for complex numbers, best of all that code has already been tested. – Pepijn Kramer Jun 12 '23 at 15:48
  • @PepijnKramer I did not know that. Thank you. Editing now. – ajgrinds Jun 12 '23 at 15:50
  • Sorry to nag, but don't use useless `new` there is no need to make `std::complex a` a pointer. Try to avoid new/delete in C++ as much as you can and if you must have some sort of dynamic memory allocation the either use a container (like std::vector) or std::make_unique/std::unique_ptr – Pepijn Kramer Jun 12 '23 at 15:54
  • Sorry could you edit the code so I can see how it *should* be? – ajgrinds Jun 12 '23 at 15:58
  • 1
    Online demo here : https://onlinegdb.com/pJNKv3F4q – Pepijn Kramer Jun 12 '23 at 16:11
  • 1
    You're welcome. If you're interested in learning more C++. Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 12 '23 at 16:20