Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int getNum(int* );/*This function allows you to correctly get the input values.*/
  5.  
  6. void source(int** , int, int );/*Displays the entered matrix*/
  7.  
  8. int min(int**, int , int );/*searches for the minimum element in the xth row*/
  9.  
  10. int init(int**, int );/*Fills the matrix with input values*/
  11.  
  12. void output(int**, int, int );/*Shifts each row by a certain number of elements and displays the resulting matrix*/
  13.  
  14. int main()
  15. {
  16.     while (true) {
  17.         int n, ** a = NULL, count = 0;
  18.         printf("Hello !!! This is a lab option 25. \n\nEnter the number of lines: ");
  19.         if (!getNum(&n))//Get the number of rows in the matrix
  20.         {
  21.             printf("Error!!!\n");
  22.             return 0;
  23.         }
  24.         while (n <= 0)
  25.         {
  26.             printf("Error!!!\nZero or negative numbers - invalid lengths!\nEnter the correct value: ");
  27.             if (!getNum(&n))
  28.             {
  29.                 printf("Error!!!\n");
  30.                 return 0;
  31.             }
  32.         }
  33.         a = new int* [n];
  34.         count = init(a, n);
  35.         count *= 6;
  36.         count += 1;
  37.         int xz = count;
  38.         source(a, n, xz);
  39.         output(a, n, xz);
  40.         printf("//////////////////////////////////\n");
  41.     }
  42.     return 0;
  43. }
  44.  
  45. /*We take into account the lessons, starting from the first column,
  46. because the lengths of rows are stored in zero*/
  47.  
  48. int getNum(int* a)
  49. {
  50.     int n, b;
  51.     do
  52.     {
  53.         n = scanf_s("%d", a);
  54.         b = (int)*a;
  55.         if (n < 0) return 0;
  56.         if (!n || ((*a - b) != 0))
  57.         {
  58.             n = 0;
  59.             printf("Error!!!\nEnter the correct value: ");
  60.             scanf_s("%*[^\n]");
  61.         }
  62.     } while (!n);
  63.     return n < 0 ? 0 : 1;
  64. }
  65.  
  66. int init(int ** a, int n)
  67. {
  68.     int count = 0, m;
  69.     for (int i = 0; i < n; i++)
  70.     {
  71.         printf("\nEnter ROW SIZE number %d: ", i + 1);
  72.         if (!getNum(&m))
  73.         {
  74.             printf("Error!!!\n");
  75.             return 0;
  76.         }
  77.         while (m <= 0)
  78.         {
  79.             printf("Error!!!\nZero or negative numbers - invalid lengths!\nEnter the correct value: ");
  80.             if (!getNum(&m))
  81.             {
  82.                 printf("Error!!!\n");
  83.                 return 0;
  84.             }
  85.         }
  86.         printf("\n");
  87.         if (i == 0) count = m;
  88.         a[i] = new int [m + 1]; //Allocate memory one column more, since 1 column contains row lengths
  89.         *a[i] = m;//From the first element of the string we get its length
  90.         for (int j = 1; j <= m; j++)
  91.         {
  92.             printf("Enter VALUE number %d: ", j);
  93.             if (!getNum(&a[i][j]))
  94.             {
  95.                 printf("Error!!!\n");
  96.             }
  97.         }
  98.     }
  99.     int buf = count;
  100.     return buf;
  101. }
  102.  
  103. void output(int ** a, int n, int count)
  104. {
  105.     int m;
  106.     for (int i = 0; i < n; i++)
  107.     {
  108.         int M = min(a, n, i);
  109.         m = *a[i];//From the first element of the string we get its length
  110.         //printf("a[1] = %d a[m] = %d",a[i][1], a[i][m]);
  111.         //printf("\nMinimum: %d\n", M);
  112.         while (a[i][1] != M)
  113.         {
  114.             int buf = a[i][1];
  115.             for (int j = 2; j < m + 1; ++j) //Perform a shift one element to the left
  116.             {
  117.                 a[i][j - 1] = a[i][j];
  118.             }
  119.             a[i][m] = buf;
  120.             //printf("!!!!\n");
  121.         }
  122.     }
  123.     int buf = count;
  124.     printf("\nOutput matrix:\n");
  125.     while (--count)
  126.         printf("-");
  127.     printf("\n");
  128.     for (int i = 0; i < n; i++, printf("\n"))
  129.     {
  130.         m = *a[i];//From the first element of the string we get its length
  131.         for (int j = 1; j <= m; j++) printf("|%4d|", a[i][j]);
  132.     }
  133.     while (--buf)
  134.         printf("-");
  135.     printf("\n");
  136.     for (int i = 0; i < n; i++) delete a[i];
  137.     delete[] a;
  138. }
  139.  
  140. void source(int** a, int n, int count)
  141. {
  142.     int  m;
  143.     int buf = count;
  144.     printf("\nSource matrix:\n");
  145.     while (--count)
  146.         printf("-");
  147.     printf("\n");
  148.     for (int i = 0; i < n; i++, printf("\n"))
  149.     {
  150.         m = *a[i];//From the first element of the string we get its length
  151.         for (int j = 1; j <= m; j++) printf("|%4d|", a[i][j]);
  152.     }
  153.     while (--buf)
  154.         printf("-");
  155.     printf("\n");
  156. }
  157.  
  158. int min(int** mas, int n, int x)
  159. {
  160.     int min = 2147483647, m;
  161.     for (int i = 0; i < n; i++)
  162.     {
  163.         m = *mas[i];//From the first element of the string we get its length
  164.         if (i == x)
  165.         for (int j = 1; j <= m; j++)
  166.             if (mas[x][j] < min) min = mas[x][j];
  167.     }
  168.     return min; //Return the minimum
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement