Advertisement
sheredega

C++ / Complex Numbers

Sep 12th, 2022 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #define n 5
  3. using namespace std;
  4.  
  5. struct Complex {
  6.     char name[20];
  7.     double a;
  8.     double b;
  9. };
  10.  
  11. Complex readinfo() {
  12.     Complex x;
  13.     cout << "Enter name: ";
  14.     cin >> x.name;
  15.     cout << "Enter the first variable: ";
  16.     cin >> x.a;
  17.     cout << "Enter the second variable: ";
  18.     cin >> x.b;
  19.     return x;
  20. };
  21.  
  22. void showinfo(Complex x) {
  23.     cout << x.name << ": " << x.a << "+" << x.b << "i" << endl;
  24. };
  25.  
  26. int main() {
  27.     Complex tab[n];
  28.     Complex temp;
  29.     int poz, supr;
  30.     cout << "Enter 5 complex numbers: " << endl;
  31.     for (int i = 0; i < n; i++) {
  32.         tab[i] = readinfo();
  33.     };
  34.     for (int k = 0; k < n; k++) {
  35.         for (int i = 0; i < (n-1); i++) {
  36.             if (tab[i].b > tab[i+1].b) {
  37.                 temp = tab[i];
  38.                 tab[i] = tab[i+1];
  39.                 tab[i+1] = temp;
  40.             };
  41.         };
  42.     };
  43.     cout << "Sorted complex numbers: " << endl;
  44.     for (int i = 0; i < n; i++) {
  45.         showinfo(tab[i]);
  46.     };
  47.     return 0;
  48. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement