Advertisement
heroys6

Lab 5

Sep 6th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.79 KB | None | 0 0
  1. // Compiled on Windows OS using CodeBlocks
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5.  
  6. void max_min_elem(int **matrix, int a);                                     //1
  7. void transpone_B(int **matrix, int b_row, int b_col);                       //2
  8. void multiply(int **matrixA, int **matrixB, int a, int b_row, int b_col);   //3
  9. void row_A(int *matrix, int a);                                             //4
  10. void sum_row_col(int **matrixA, int **matrixB, int a, int b_row, int b_col);//5
  11.  
  12. int main()
  13. {
  14.     int a, b_row, b_col, i, j, ch, ch_m, a_num, main_wh;
  15.  
  16.     printf("\t\t\t\t  Lab 5\n\t\tWork with two-dimensional dynamic arrays");
  17.  
  18.     fflush(stdout); // Ponty
  19.     Sleep(1000);
  20.     printf("\n\nG");
  21.  
  22.     fflush(stdout);
  23.     Sleep(1000);
  24.     printf("o");
  25.  
  26.     fflush(stdout);
  27.     Sleep(1000);
  28.     printf("o");
  29.  
  30.     fflush(stdout);
  31.     Sleep(1000);
  32.     printf("d");
  33.  
  34.     fflush(stdout);
  35.     Sleep(1000);
  36.     printf(" l");
  37.  
  38.     fflush(stdout);
  39.     Sleep(1000);
  40.     printf("u");
  41.  
  42.     fflush(stdout);
  43.     Sleep(1000);
  44.     printf("c");
  45.  
  46.     fflush(stdout);
  47.     Sleep(1000);
  48.     printf("k");
  49.  
  50.     fflush(stdout);
  51.     Sleep(1000);
  52.     printf("!");
  53.  
  54.     do
  55.     {
  56.         printf("\n\nEnter side of square matrix A: ");
  57.         fflush(stdin);
  58.         scanf("%d", &a);
  59.         if (a<1) printf("How do you imagine matrix [%d][%d]? Try again!\n", a, a);
  60.     } while (a<1);
  61.  
  62.     do
  63.     {
  64.         printf("Enter ROWs value of matrix B: ");
  65.         fflush(stdin);
  66.         scanf("%d", &b_row);
  67.  
  68.         printf("Enter COLumns value of matrix B: ");
  69.         fflush(stdin);
  70.         scanf("%d", &b_col);
  71.  
  72.         if (b_row<1 || b_col<1) printf("How do you imagine matrix [%d][%d]? Try again!\n", b_row, b_col);
  73.     } while (b_row<1 || b_col<1);
  74.  
  75.     printf("\nWhat are you prefer?\n1. Enter matrix elements by hand\n2. Automatic assignation\nYour choice: ");
  76.     scanf("%d", &ch);
  77.  
  78.     int **matrix_A = (int **)malloc(a*sizeof(int *));
  79.  
  80.     if (!matrix_A)
  81.     {
  82.         printf("Error of memory allocation");
  83.         exit(1);
  84.     }
  85.     for (i = 0; i<a; i++)
  86.     {
  87.         matrix_A[i] = (int *)malloc(a*sizeof(int)); // Pamyat' dlya stolbcov v kajdom ryadu
  88.         if (!matrix_A[i])
  89.         {
  90.             printf("Error of memory allocation");
  91.             exit(1);
  92.         }
  93.     }
  94.  
  95.     int **matrix_B = (int **)malloc(b_row*sizeof(int *));
  96.     if (!matrix_B)
  97.     {
  98.         printf("Error of memory allocation");
  99.         exit(1);
  100.     }
  101.     for (i = 0; i<b_row; i++)
  102.     {
  103.         matrix_B[i] = (int *)malloc(b_col*sizeof(int *));
  104.         if (!matrix_B[i])
  105.         {
  106.             printf("Error of memory allocation");
  107.             exit(1);
  108.         }
  109.     }
  110.  
  111.     if (ch == 1)
  112.     {
  113.         printf("\n\nYou select [HAND MAKE] input\n\n");
  114.         printf("Assignation of matrix A:\n\n");
  115.         for (i = 0; i<a; i++)
  116.         {
  117.             for (j = 0; j<a; j++)
  118.             {
  119.                 //do
  120.                 //{
  121.                 printf("Enter value in matrix socket [%d][%d] : ", i, j);
  122.                 fflush(stdin);
  123.                 scanf("%d", &matrix_A[i][j]);
  124.                 //if (matrix_A[i][j]<0) printf("Value must be positive(or zero). Try again!\n");
  125.                 //}
  126.                 //while(matrix_A[i][j]<0);
  127.             }
  128.         }
  129.         printf("\nAssignation of matrix B:\n\n");
  130.         for (i = 0; i<b_row; i++)
  131.         {
  132.             for (j = 0; j<b_col; j++)
  133.             {
  134.                 printf("Enter value in matrix socket [%d][%d] : ", i, j);
  135.                 scanf("%d", &matrix_B[i][j]);
  136.             }
  137.         }
  138.     }
  139.     else if (ch == 2)
  140.     {
  141.         printf("\nYou select [INITIALIZATION]\n");
  142.  
  143.         printf("\nAssignation of matrix A:\t[#");
  144.  
  145.         fflush(stdout);
  146.         Sleep(1000);
  147.         printf("#####");
  148.  
  149.         fflush(stdout);
  150.         Sleep(1000);
  151.         printf("#####");
  152.  
  153.         fflush(stdout);
  154.         Sleep(1000);
  155.         printf("###### - 100%%]");
  156.  
  157.         fflush(stdout);
  158.         Sleep(1000);
  159.  
  160.         for (i = 0; i<a; i++)
  161.         {
  162.             for (j = 0; j<a; j++)
  163.             {
  164.                 matrix_A[i][j] = (i + j);
  165.             }
  166.         }
  167.  
  168.         printf("\nAssignation of matrix B:\t[#");
  169.  
  170.         fflush(stdout);
  171.         Sleep(1000);
  172.         printf("#####");
  173.  
  174.         fflush(stdout);
  175.         Sleep(1000);
  176.         printf("#####");
  177.  
  178.         fflush(stdout);
  179.         Sleep(1000);
  180.         printf("###### - 100%%");
  181.  
  182.         fflush(stdout);
  183.         Sleep(1000);
  184.  
  185.         for (i = 0; i<b_row; i++)
  186.         {
  187.             for (j = 0; j<b_col; j++)
  188.             {
  189.                 matrix_B[i][j] = (i + j);
  190.             }
  191.         }
  192.     }
  193.  
  194.     do
  195.     {
  196.         main_wh = 0;
  197.  
  198.         printf("\nOk, select the task for array processing:\n\t1. Max/min element of matrix A\n\t2. Transpose matrix B\n\t3. Find matrix A & B multiplication\n\t4. Sort row elements of massive A\n\t5. Sum of elements of A-rows & B-columns\n\t6. Exit\nYour choice: ");
  199.         scanf("%d", &ch_m);
  200.  
  201.         switch (ch_m)
  202.         {
  203.         case 1:
  204.             max_min_elem(matrix_A, a);
  205.             break;
  206.  
  207.         case 2:
  208.             transpone_B(matrix_B, b_row, b_col);
  209.             break;
  210.  
  211.         case 3:
  212.             multiply(matrix_A, matrix_B, a, b_row, b_col);
  213.             break;
  214.  
  215.         case 4:
  216.             printf("\nSource massive A:\n\n");
  217.  
  218.             for (i = 0; i<a; i++) // put mtrx A
  219.             {
  220.                 for (j = 0; j<a; j++)
  221.                 {
  222.                     printf("%d   ", matrix_A[i][j]);
  223.                 }
  224.                 printf("\n\n");
  225.             }
  226.  
  227.             printf("Select row of matrix A(from 0 to %d) to sort: ", (a - 1));
  228.             scanf("%d", &a_num);
  229.  
  230.             int *matrix_Sngl = (int *)malloc(a*sizeof(int));
  231.  
  232.             if (!matrix_Sngl)
  233.             {
  234.                 printf("Error of memory allocation");
  235.                 exit(1);
  236.             }
  237.  
  238.             for (j = 0; j<a; j++)
  239.             {
  240.                 matrix_Sngl[j] = matrix_A[a_num][j];
  241.             }
  242.  
  243.             row_A(matrix_Sngl, a);
  244.  
  245.             free(matrix_Sngl);
  246.  
  247.             break;
  248.  
  249.         case 5:
  250.             sum_row_col(matrix_A, matrix_B, a, b_row, b_col);
  251.             break;
  252.  
  253.         case 6:
  254.             printf("\nYou selected exit");
  255.             main_wh = 0;
  256.             break;
  257.  
  258.         default:
  259.             exit(1);
  260.         }
  261.  
  262.         if (main_wh == 0)
  263.         {
  264.             printf("\n\nDo you want to select another task?(1-yes/2-no)\nYour answer: ");
  265.             fflush(stdin);
  266.             scanf("%d", &main_wh);
  267.         }
  268.  
  269.         if (main_wh != 1)
  270.         {
  271.             printf("\nGood bye!");
  272.  
  273.             free(matrix_A);
  274.  
  275.             free(matrix_B);
  276.  
  277.         }
  278.  
  279.     } while (main_wh == 1);
  280.  
  281.     return 0;
  282. }
  283.  
  284. void max_min_elem(int **matrix, int a)
  285. {
  286.     int i, j, f, k, max_val = -32768, min_val, reserv;
  287.  
  288.     printf("\n\nMatrix A:\n\n");
  289.  
  290.     for (i = 0; i<a; i++) // Output
  291.     {
  292.         for (j = 0; j<a; j++)
  293.         {
  294.             printf("%d   ", matrix[i][j]);
  295.         }
  296.  
  297.         printf("\n\n");
  298.     }
  299.  
  300.     for (i = 0; i<a; i++) // Biggest
  301.     {
  302.         for (j = 0; j<a; j++)
  303.         {
  304.             if (matrix[i][j]>max_val) max_val = matrix[i][j];
  305.         }
  306.     }
  307.     printf("Biggest matrix value: %d", max_val);
  308.  
  309.     if (a == 1)
  310.     {
  311.         min_val = matrix[0][0];
  312.     }
  313.     else
  314.     {
  315.         for (i = 0; i<a; i++) // All min - to right...
  316.         {
  317.             for (j = i, f = 0; j<a; j++, f++) // j=i above main diagonal
  318.             {
  319.                 for (k = i; k<(a - 1 - f); k++) // (a-1-f) - Skolko raz ostalos slravnivat    f - skolko min values na svoih mestah
  320.                 {
  321.                     if (matrix[i][j]<matrix[i][j + 1])
  322.                     {
  323.                         reserv = matrix[i][j + 1];
  324.                         matrix[i][j + 1] = matrix[i][j];
  325.                         matrix[i][j] = reserv;
  326.                     }
  327.                 }
  328.             }
  329.         }
  330.     }
  331.  
  332.     if (a != 1)
  333.     {
  334.         for (i = 0; i<(a - 1); i++) // Samyu MIN budet vnizu
  335.         {
  336.             for (j = (a - 1); j<a; j++)
  337.             {
  338.                 if (matrix[i][j]<matrix[i + 1][j])
  339.                 {
  340.                     reserv = matrix[i + 1][j];
  341.                     matrix[i + 1][j] = matrix[i][j];
  342.                     matrix[i][j] = reserv;
  343.                 }
  344.                 min_val = matrix[i + 1][j];
  345.             }
  346.         }
  347.     }
  348.  
  349.     printf("\nSmollest matrix value(diagonal + above main diagonal): %d", min_val);
  350. }
  351.  
  352. void transpone_B(int **matrix, int b_row, int b_col)
  353. {
  354.     int B_t[b_col][b_row];
  355.  
  356.     int i, j;
  357.  
  358.     printf("\n\nSource matrix:\n\n");
  359.  
  360.     for (i = 0; i<b_row; i++) // Source matrix
  361.     {
  362.         for (j = 0; j<b_col; j++)
  363.         {
  364.             printf("%d   ", matrix[i][j]);
  365.         }
  366.  
  367.         printf("\n\n");
  368.     }
  369.  
  370.     for (i = 0; i<b_row; i++)
  371.     {
  372.         for (j = 0; j<b_col; j++)
  373.         {
  374.             B_t[j][i] = matrix[i][j];
  375.         }
  376.     }
  377.  
  378.     printf("\nTransponed matrix:\n\n");
  379.  
  380.     for (i = 0; i<b_col; i++) // Transponed matrix
  381.     {
  382.         for (j = 0; j<b_row; j++)
  383.         {
  384.             printf("%d   ", B_t[i][j]);
  385.         }
  386.  
  387.         printf("\n\n");
  388.     }
  389.  
  390. }
  391.  
  392. void multiply(int **matrixA, int **matrixB, int a, int b_row, int b_col)
  393. {
  394.     int i, j, k, x1;
  395.  
  396.     int AB[a][b_col];
  397.  
  398.     printf("\n");
  399.     printf("Matrix A: \n\n");
  400.  
  401.     for (i = 0; i<a; i++) // Source mtrx A
  402.     {
  403.         for (j = 0; j<a; j++)
  404.         {
  405.             printf("%d   ", matrixA[i][j]);
  406.         }
  407.  
  408.         printf("\n\n");
  409.     }
  410.  
  411.     printf("\n");
  412.     printf("Matrix B: \n\n");
  413.  
  414.     for (i = 0; i<b_row; i++) // Source mtrx B
  415.     {
  416.         for (j = 0; j<b_col; j++)
  417.         {
  418.             printf("%d   ", matrixB[i][j]);
  419.         }
  420.  
  421.         printf("\n\n");
  422.     }
  423.  
  424.     if (a != b_row)
  425.     {
  426.         printf("Entered matrices can not be multiplied!");
  427.         return;
  428.     }
  429.     else if (a == b_row)
  430.     {
  431.  
  432.         for (j = 0; j<b_col; j++) // mtrx B columns
  433.         {
  434.             for (i = 0; i<a; i++) // a - rows in mtrx A
  435.             {
  436.                 x1 = 0;
  437.                 for (k = 0; k<a; k++) // a - columns in mtrx A
  438.                 {
  439.                     x1 += matrixA[i][k] * matrixB[k][j];
  440.                 }
  441.                 AB[i][j] = x1;
  442.             }
  443.         }
  444.  
  445.         printf("\n");
  446.         printf("Multiplied matrix: \n\n");
  447.  
  448.         for (i = 0; i<a; i++) // Output matrix(multiplied)
  449.         {
  450.             for (j = 0; j<b_col; j++)
  451.             {
  452.                 printf("%d   ", AB[i][j]);
  453.             }
  454.  
  455.             printf("\n\n");
  456.         }
  457.     }
  458. }
  459.  
  460. void sum_row_col(int **matrixA, int **matrixB, int a, int b_row, int b_col)
  461. {
  462.     int val_A, val_B, i, j;
  463.  
  464.     printf("\n");
  465.     printf("Matrix A: \n\n");
  466.     for (i = 0; i<a; i++) // Output source matrix A
  467.     {
  468.         for (j = 0; j<a; j++)
  469.         {
  470.             printf("%d   ", matrixA[i][j]);
  471.         }
  472.  
  473.         printf("\n\n");
  474.     }
  475.  
  476.     printf("\n");
  477.     printf("Matrix B: \n\n");
  478.     for (i = 0; i<b_row; i++) // Output source matrix B
  479.     {
  480.         for (j = 0; j<b_col; j++)
  481.         {
  482.             printf("%d   ", matrixB[i][j]);
  483.         }
  484.  
  485.         printf("\n\n");
  486.     }
  487.  
  488.     printf("In matrix A:\n");
  489.     for (i = 0; i<a; i++)
  490.     {
  491.         val_A = 0;
  492.         for (j = 0; j<a; j++)
  493.         {
  494.             val_A += matrixA[i][j];
  495.         }
  496.         printf("\nSum of elements of %d row: %d", i, val_A);
  497.     }
  498.  
  499.     printf("\n\nIn matrix B:\n");
  500.     for (i = 0; i<b_col; i++)
  501.     {
  502.         val_B = 0;
  503.         for (j = 0; j<b_row; j++)
  504.         {
  505.             val_B += matrixB[j][i];
  506.         }
  507.         printf("\nSum of elements of %d col: %d", i, val_B);
  508.     }
  509. }
  510.  
  511. void row_A(int *matrix, int a)
  512. {
  513.     int i, j, reserv;
  514.  
  515.     printf("\nSelected row:\n");
  516.  
  517.     for (i = 0; i<a; i++)
  518.     {
  519.         printf("%d  ", matrix[i]);
  520.     }
  521.  
  522.     printf("\n\nSorted row:\n");
  523.  
  524.     for (i = 0; i<a; i++) // Sort
  525.     {
  526.         for (j = 0; j<(a - 1 - i); j++)
  527.         {
  528.             if (matrix[j]>matrix[j + 1])
  529.             {
  530.                 reserv = matrix[j + 1];
  531.                 matrix[j + 1] = matrix[j];
  532.                 matrix[j] = reserv;
  533.             }
  534.         }
  535.     }
  536.  
  537.     for (i = 0; i<a; i++)
  538.     {
  539.         printf("%d  ", matrix[i]);
  540.     }
  541.  
  542.     printf("\n");
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement