Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

What I have tried:


#include <iostream>
using namespace std;
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);
}
}
void output()
{
for (int i=0;i<n;i++)
{
="" if="" (image<0)
="" cout<<real<<"="" -="" "<<-1*image<<"i"<<endl;
="" }
="" else
="" +="" "<<image<<"i"<<endl;
=""
};
<="" pre="">
Posted
Updated 12-Jun-23 4:59am
v3
Comments
Richard Deeming 12-Jun-23 5:22am    
Your code has been truncated because you didn't wrap it in <pre>...</pre> blocks.

Click the green "Improve question" link and paste your code again, making sure to put a <pre lang="C++"> before it, and a </pre> after it.
sogia 12-Jun-23 10:59am    
thank you
Richard Deeming 12-Jun-23 11:03am    
Try that again: You need <pre>...</pre> tags around your code block.

If you don't add those tags, your code will be truncated and/or corrupted, and nobody will be able to help you.
sogia 12-Jun-23 11:55am    
I had only written input and output
OriginalGriff 12-Jun-23 6:12am    
As Richard says, you need pre tags around your code.
The simplest way to add this is to look at the popup which appears when you post the new copy of your code into your question, and select the "code block" option.
That will add the tags for you.

1 solution

Why are you creating your own class for complex numbers while the C++ standard library already provides one?
In the following code, a sequence of complex numbers is created and then an iteration over the sequence items is performed.
C++
#include <iostream>
#include <complex>
#include <vector>

using namespace std;

using Complex = complex < double >;

void dump( const vector < Complex > & v);

int main()
{

  size_t v_size;
  cout << "please enter the size of the array: ";
  cin >> v_size;

  vector< Complex > v; // the sequence of complex numbers


  for (size_t n=0; n<v_size; ++n) // how to fill the sequence
  {
    double re, im;
    cout << "complex no. " << (n+1) << ", real part: ";
    cin >> re;
    cout << "complex no. " << (n+1) << ", imaginary part: ";
    cin >> im;
    v.emplace_back(Complex(re, im));
  }

  dump(v);
}


void dump( const vector < Complex > & v)
{
  // how to iterate over the sequence
  for (const auto & c : v)
    cout << c << " ";
  cout << "\n";
}
 
Share this answer
 
Comments
jeron1 12-Jun-23 10:02am    
Nice!
CPallini 12-Jun-23 11:02am    
Thank you!
sogia 12-Jun-23 11:03am    
thank you but i need write it for University assignment .
jeron1 12-Jun-23 11:18am    
How does that change anything?
Patrice T 22-Jun-23 7:23am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900