Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct complex{
  5. int real ;
  6. int imag ;
  7. } complex1, complex2, temp ;
  8.  
  9. void swap_complex (complex*, complex* ) ;
  10. int main()
  11. {
  12. cout << "Enter real and imaginary part of a complex number :" ;
  13. cin >> complex1.real >> complex1.imag ;
  14.  
  15. cout << "\nEnter real and imaginary part of another complex number :" ;
  16. cin >> complex2.real >> complex2.imag ;
  17.  
  18. cout << "\n\n--------Before Swapping---------"<<endl;
  19. cout << "\nFirst complex number : "<<complex1.real<<"+"<<(complex1.imag)<<"i" ;
  20. cout << "\nSecond complex number : "<<complex2.real<<"+"<<(complex2.imag)<<"i" ;
  21.  
  22. swap_complex(&complex1, &complex2);
  23.  
  24. cout << "\n\n--------After Swapping---------"<<endl;
  25. cout << "\nFirst complex number : "<<complex1.real<<"+"<<(complex1.imag)<<"i" ;
  26. cout << "\nSecond complex number : "<<complex2.real<<"+"<<(complex2.imag)<<"i\n\n" ;
  27. return 0;
  28. }
  29.  
  30. void swap_complex (complex* a, complex* b )
  31. {
  32. temp.real = (*a).real; //for Pointer : using ' (*a) ' is equivalent to ' -> ' . Either of them can be used for pointing operation
  33. temp.imag = (*a).imag ;
  34.  
  35. (*a).real = (*b).real ;
  36. (*a).imag = (*b).imag ;
  37.  
  38. (*b).real = temp.real;
  39. (*b).imag = temp.imag ;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement