Advertisement
Guest User

task1

a guest
Dec 4th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4.  
  5. using namespace std;
  6.  
  7. /*Создать класс, private-элемент которого двумерный массив.
  8. Класс будет содержать два метода: один заполнит массив значениями,
  9. второй произведет замену значений строк на значения столбцов.
  10. Преобразовать строки двумерного массива в столбцы*/
  11.  
  12. class ReplaceRowOfCol
  13. {
  14. private:
  15.     int **dynMas;
  16.  
  17.     void createMas(int row, int col){
  18.  
  19.         dynMas = new int*[row];
  20.  
  21.         for(int i = 0; i < row; i++){
  22.             dynMas[i] = new int[col];
  23.         }
  24.     }
  25. public:
  26.  
  27.     void enterArray(int row, int col){
  28.  
  29.         createMas(row, col);
  30.         cout << " Enter Array: " << endl;
  31.         for(int i = 0; i < row; i++){
  32.             cout << endl;
  33.             for(int j = 0; j < col; j++){  
  34.                 cout << "[" << i << "][" << j << "]: ";
  35.                 cin >> dynMas[i][j];
  36.             }          
  37.         }
  38.  
  39.  
  40.     }
  41.  
  42.     void print(int row, int col){
  43.  
  44.         for(int i = 0; i < row; i++){
  45.            
  46.             for(int j = 0; j < col; j++){  
  47.                
  48.                 cout << dynMas[i][j] << " ";
  49.             }  
  50.             cout << endl;
  51.         }
  52.     }
  53.  
  54.     void replace(int row, int col){
  55.  
  56.         int** buf = new int*[col];
  57.  
  58.         for(int i = 0; i < col; i++){
  59.             buf[i] = new int[row];
  60.         }
  61.  
  62.         for(int i = 0; i < col; i++){
  63.             for(int j = 0; j < row; j++){
  64.  
  65.                 buf[i][j] = dynMas[j][i];
  66.                
  67.             }
  68.         }
  69.  
  70.         for (int i = 0; i < col; i++)
  71.         {
  72.             delete [] dynMas[i];
  73.         }
  74.         delete[] dynMas;
  75.  
  76.         createMas(col, row);
  77.         memcpy(dynMas, buf, row*col);
  78.         /*
  79.         for (int i = 0; i < row; i++)
  80.         {
  81.             delete [] buf[i];
  82.         }
  83.         delete[] buf;*/
  84.  
  85.     }
  86. };
  87.  
  88. int main(){
  89.  
  90.     setlocale(LC_ALL, "Russian");
  91.  
  92.     ReplaceRowOfCol arr = ReplaceRowOfCol();
  93.  
  94.     arr.enterArray(5,5);
  95.     arr.print(5,5);
  96.     arr.replace(5,5);
  97.     arr.print(5,5);
  98.  
  99.     system("pause");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement