Advertisement
Guest User

Untitled

a guest
May 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.71 KB | None | 0 0
  1. //#include <stdafx.h>
  2. #include "stdlib.h"
  3. #include <iostream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7.  
  8. void arr_swap_columns  (int     **A,   int N1,  int N2) {
  9.     int Temp;
  10.     for (int k = 0; k < N2; k += 4)
  11.         for (int i = 0; i < N1; i++) {
  12.             if ((k + 2) < N2) {
  13.                 Temp = A[i][k];
  14.                 A[i][k] = A[i][k + 2];
  15.                 A[i][k + 2] = Temp;
  16.             }
  17.             if ((k + 3) < N2) {
  18.                 Temp = A[i][k + 1];
  19.                 A[i][k + 1] = A[i][k + 3];
  20.                 A[i][k + 3] = Temp;
  21.             }
  22.         }
  23. }
  24. void arr_swap_columns  (float   **A,   int N1,  int N2) {
  25.     float Temp;
  26.     for (int k = 0; k < N2; k += 4)
  27.         for (int i = 0; i < N1; i++) {
  28.             if ((k + 2) < N2) {
  29.                 Temp = A[i][k];
  30.                 A[i][k] = A[i][k + 2];
  31.                 A[i][k + 2] = Temp;
  32.             }
  33.             if ((k + 3) < N2) {
  34.                 Temp = A[i][k + 1];
  35.                 A[i][k + 1] = A[i][k + 3];
  36.                 A[i][k + 3] = Temp;
  37.             }
  38.         }
  39. }
  40. void arr_swap_columns  (char    **A,   int N1,  int N2) {
  41.     char Temp;
  42.     for (int k = 0; k < N2; k += 4)
  43.         for (int i = 0; i < N1; i++) {
  44.             if ((k + 2) < N2) {
  45.                 Temp = A[i][k];
  46.                 A[i][k] = A[i][k + 2];
  47.                 A[i][k + 2] = Temp;
  48.             }
  49.             if ((k + 3) < N2) {
  50.                 Temp = A[i][k + 1];
  51.                 A[i][k + 1] = A[i][k + 3];
  52.                 A[i][k + 3] = Temp;
  53.             }
  54.         }
  55. }
  56.  
  57. void declare_2d_arr     (int     **&A,  int &N1, int *N2, char ch = 'A', bool re_enter = false) {
  58.  
  59.     if ((N1 < 1) || re_enter) {
  60.     do {
  61.         system("cls");
  62.         cout << endl << "  int " << ch << "[";
  63.         cin.clear();
  64.         cin.sync();
  65.     }
  66.     while(!(cin >> N1) || (N1 < 1));
  67.     }
  68.     if ((*N2 < 1) || re_enter) {
  69.     do {
  70.         system("cls");
  71.         cout << endl << "  int " << ch << "[" << N1 << "][";
  72.         cin.clear();
  73.         cin.sync();
  74.     }
  75.     while(!(cin >> *N2) || (*N2 < 1));
  76.     }
  77.  
  78.     A = new int *[N1];
  79.     for (int i = 0; i < N1; i++)
  80.        A[i] = new int [*N2];
  81.  
  82.     system("cls");
  83.     cout << endl << "  int " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
  84.  
  85.         for (int i = 0; i < N1; i++)
  86.             for (int j = 0; j < *N2; j++)
  87.                 A[i][j] = 0;
  88. }
  89. void declare_2d_arr     (float   **&A,  int &N1, int *N2, char ch = 'A', bool re_enter = false) {
  90.  
  91.     if ((N1 < 1) || re_enter) {
  92.     do {
  93.     system("cls");
  94.         cout << endl << "  float " << ch << "[";
  95.         cin.clear();
  96.         cin.sync();
  97.     }
  98.     while(!(cin >> N1) || (N1 < 1));
  99.     }
  100.     if ((*N2 < 1) || re_enter) {
  101.     do {
  102.     system("cls");
  103.         cout << endl << "  float " << ch << "[" << N1 << "][";
  104.         cin.clear();
  105.         cin.sync();
  106.     }
  107.     while(!(cin >> *N2) || (*N2 < 1));
  108.     }
  109.  
  110.     A = new float *[N1];
  111.     for (int i = 0; i < N1; i++)
  112.         A[i] = new float [*N2];
  113.  
  114.     system("cls");
  115.     cout << endl << "  float " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
  116.  
  117.         for (int i = 0; i < N1; i++)
  118.             for (int j = 0; j < *N2; j++)
  119.                 A[i][j] = 0.0;
  120. }
  121. void declare_2d_arr     (char    **&A,  int &N1, int *N2, char ch = 'A', bool re_enter = false) {
  122.  
  123.     if ((N1 < 1) || re_enter) {
  124.     do {
  125.     system("cls");
  126.         cout << endl << "  char " << ch << "[";
  127.         cin.clear();
  128.         cin.sync();
  129.     }
  130.     while(!(cin >> N1) || (N1 < 1));
  131.     }
  132.     if ((*N2 < 1) || re_enter) {
  133.     do {
  134.     system("cls");
  135.         cout << endl << "  char " << ch << "[" << N1 << "][";
  136.         cin.clear();
  137.         cin.sync();
  138.     }
  139.     while(!(cin >> *N2) || (*N2 < 1));
  140.     }
  141.  
  142.     A = new char *[N1];
  143.     for (int i = 0; i < N1; i++)
  144.         A[i] = new char [*N2];
  145.  
  146.     system("cls");
  147.     cout << endl << "  char " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
  148.  
  149.         for (int i = 0; i < N1; i++)
  150.             for (int j = 0; j < *N2; j++)
  151.                 A[i][j] = '.';
  152. }
  153.  
  154. void cout_2d_arr       (int     **A,   int N1,  int N2) {
  155.     int max = A[0][0]; for (int i = 0; i < N1; i++) {for (int j = 0; j < N2; j++) if (A[i][j] > max) max = A[i][j];}
  156.  
  157.     int k = 1;          while (max > 10) {max = max / 10; k++;}
  158.  
  159.     for (int i = 0; i < N1; i++) {
  160.         cout << "  ";
  161.         for (int j = 0; j < N2; j++) {
  162.             cout.width(k+1);
  163.             cout << A[i][j] << " ";}
  164.         cout << endl;}
  165. }
  166. void cout_2d_arr       (float   **A,   int N1,  int N2) {
  167.     int max = (int)A[0][0]; for (int i = 0; i < N1; i++) {for (int j = 0; j < N2; j++) if ((int)A[i][j] > max) max = (int)A[i][j];}
  168.     int k = 1;          while (max > 10) {max = max / 10; k++;}
  169.  
  170.     for (int i = 0; i < N1; i++) {
  171.         cout << "  ";
  172.         for (int j = 0; j < N2; j++) {
  173.             cout << fixed << right;
  174.             cout.precision(3);
  175.             cout.width(k+5);
  176.             cout << A[i][j] << " ";}
  177.         cout << endl;}
  178. }
  179. void cout_2d_arr       (char    **A,   int N1,  int N2) {
  180.     for (int i = 0; i < N1; i++) {
  181.         cout << "  ";
  182.         for (int j = 0; j < N2; j++) {
  183.             cout.width(2);
  184.             cout << A[i][j] << " ";}
  185.         cout << endl;}
  186. }
  187.  
  188. void cin_2d_arr        (int     **A,   int N1,  int N2) {
  189.     for (int i = 0; i < N1; i++)
  190.         for (int j = 0; j < N2; j++)
  191.             do {
  192.                 system("cls");
  193.                 cout << endl << "  Input:" << endl << endl;
  194.                 cout_2d_arr(A, N1, N2);
  195.                 cout << endl << "   ";
  196.                 cin.clear();
  197.                 cin.sync();
  198.             }
  199.             while(!(cin >> A[i][j]));
  200.             system("cls");
  201.             cout << endl << "  Input:" << endl << endl;
  202.             cout_2d_arr(A, N1, N2);
  203. }
  204. void cin_2d_arr        (float   **A,   int N1,  int N2) {
  205.     for (int i = 0; i < N1; i++)
  206.         for (int j = 0; j < N2; j++)
  207.             do {
  208.                 system("cls");
  209.                 cout << endl << "  Input:" << endl << endl;
  210.                 cout_2d_arr(A, N1, N2);
  211.                 cout << endl << "   ";
  212.                 cin.clear();
  213.                 cin.sync();
  214.             }
  215.             while(!(cin >> A[i][j]));
  216.             system("cls");
  217.             cout << endl << "  Input:" << endl << endl;
  218.             cout_2d_arr(A, N1, N2);
  219. }
  220. void cin_2d_arr        (char    **A,   int N1,  int N2) {
  221.     for (int i = 0; i < N1; i++)
  222.         for (int j = 0; j < N2; j++)
  223.             do {
  224.                 system("cls");
  225.                 cout << endl << "  Input:" << endl << endl;
  226.                 cout_2d_arr(A, N1, N2);
  227.                 cout << endl << "   ";
  228.                 cin.clear();
  229.                 cin.sync();
  230.             }
  231.             while(!(cin >> A[i][j]));
  232.             system("cls");
  233.             cout << endl << "  Input:" << endl << endl;
  234.             cout_2d_arr(A, N1, N2);
  235. }
  236.  
  237.  
  238.  
  239. int main() {
  240.  
  241.     int N1 = 0, N2 = 0, Temp;
  242.  
  243.     int     **A;
  244.     declare_2d_arr(A, N1, &N2, 'A', false);
  245.     cout << endl << "  Enter the elements (one at time):" << endl << endl;
  246.     system("pause");
  247.     cin_2d_arr(A, N1, N2);
  248.     arr_swap_columns(A, N1, N2);
  249.     cout << endl << "  Result:" << endl << endl;
  250.     cout_2d_arr(A, N1, N2);
  251.  
  252.     for (int i = 0; i < N1; i++)
  253.        delete [] A[i];
  254.     delete [] A;
  255.  
  256.     cout << endl;
  257.     system("pause");
  258.  
  259.         float   **B;
  260.     declare_2d_arr(B, N1, &N2, 'B', false);
  261.     cout << endl << "  Enter the elements (one at time):" << endl << endl;
  262.     system("pause");
  263.     cin_2d_arr(B, N1, N2);
  264.     arr_swap_columns(B, N1, N2);
  265.     cout << endl << "  Result:" << endl << endl;
  266.     cout_2d_arr(B, N1, N2);
  267.  
  268.     for (int i = 0; i < N1; i++)
  269.        delete [] B[i];
  270.     delete [] B;
  271.  
  272.     cout << endl;
  273.     system("pause");
  274.  
  275.             char    **C;
  276.     declare_2d_arr(C, N1, &N2, 'C', true);
  277.     cout << endl << "  Enter the elements (one at time):" << endl << endl;
  278.     system("pause");
  279.     cin_2d_arr(C, N1, N2);
  280.     arr_swap_columns(C, N1, N2);
  281.     cout << endl << "  Result:" << endl << endl;
  282.     cout_2d_arr(C, N1, N2);
  283.  
  284.     for (int i = 0; i < N1; i++)
  285.        delete [] C[i];
  286.     delete [] C;
  287.  
  288.  
  289.     cout << endl << endl;
  290.     system("pause");
  291.     return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement