Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int getNum(int* );/*This function allows you to correctly get the input values.*/
- void source(int** , int, int );/*Displays the entered matrix*/
- int min(int**, int , int );/*searches for the minimum element in the xth row*/
- int init(int**, int );/*Fills the matrix with input values*/
- void output(int**, int, int );/*Shifts each row by a certain number of elements and displays the resulting matrix*/
- int main()
- {
- while (true) {
- int n, ** a = NULL, count = 0;
- printf("Hello !!! This is a lab option 25. \n\nEnter the number of lines: ");
- if (!getNum(&n))//Get the number of rows in the matrix
- {
- printf("Error!!!\n");
- return 0;
- }
- while (n <= 0)
- {
- printf("Error!!!\nZero or negative numbers - invalid lengths!\nEnter the correct value: ");
- if (!getNum(&n))
- {
- printf("Error!!!\n");
- return 0;
- }
- }
- a = new int* [n];
- count = init(a, n);
- count *= 6;
- count += 1;
- int xz = count;
- source(a, n, xz);
- output(a, n, xz);
- printf("//////////////////////////////////\n");
- }
- return 0;
- }
- /*We take into account the lessons, starting from the first column,
- because the lengths of rows are stored in zero*/
- int getNum(int* a)
- {
- int n, b;
- do
- {
- n = scanf_s("%d", a);
- b = (int)*a;
- if (n < 0) return 0;
- if (!n || ((*a - b) != 0))
- {
- n = 0;
- printf("Error!!!\nEnter the correct value: ");
- scanf_s("%*[^\n]");
- }
- } while (!n);
- return n < 0 ? 0 : 1;
- }
- int init(int ** a, int n)
- {
- int count = 0, m;
- for (int i = 0; i < n; i++)
- {
- printf("\nEnter ROW SIZE number %d: ", i + 1);
- if (!getNum(&m))
- {
- printf("Error!!!\n");
- return 0;
- }
- while (m <= 0)
- {
- printf("Error!!!\nZero or negative numbers - invalid lengths!\nEnter the correct value: ");
- if (!getNum(&m))
- {
- printf("Error!!!\n");
- return 0;
- }
- }
- printf("\n");
- if (i == 0) count = m;
- a[i] = new int [m + 1]; //Allocate memory one column more, since 1 column contains row lengths
- *a[i] = m;//From the first element of the string we get its length
- for (int j = 1; j <= m; j++)
- {
- printf("Enter VALUE number %d: ", j);
- if (!getNum(&a[i][j]))
- {
- printf("Error!!!\n");
- }
- }
- }
- int buf = count;
- return buf;
- }
- void output(int ** a, int n, int count)
- {
- int m;
- for (int i = 0; i < n; i++)
- {
- int M = min(a, n, i);
- m = *a[i];//From the first element of the string we get its length
- //printf("a[1] = %d a[m] = %d",a[i][1], a[i][m]);
- //printf("\nMinimum: %d\n", M);
- while (a[i][1] != M)
- {
- int buf = a[i][1];
- for (int j = 2; j < m + 1; ++j) //Perform a shift one element to the left
- {
- a[i][j - 1] = a[i][j];
- }
- a[i][m] = buf;
- //printf("!!!!\n");
- }
- }
- int buf = count;
- printf("\nOutput matrix:\n");
- while (--count)
- printf("-");
- printf("\n");
- for (int i = 0; i < n; i++, printf("\n"))
- {
- m = *a[i];//From the first element of the string we get its length
- for (int j = 1; j <= m; j++) printf("|%4d|", a[i][j]);
- }
- while (--buf)
- printf("-");
- printf("\n");
- for (int i = 0; i < n; i++) delete a[i];
- delete[] a;
- }
- void source(int** a, int n, int count)
- {
- int m;
- int buf = count;
- printf("\nSource matrix:\n");
- while (--count)
- printf("-");
- printf("\n");
- for (int i = 0; i < n; i++, printf("\n"))
- {
- m = *a[i];//From the first element of the string we get its length
- for (int j = 1; j <= m; j++) printf("|%4d|", a[i][j]);
- }
- while (--buf)
- printf("-");
- printf("\n");
- }
- int min(int** mas, int n, int x)
- {
- int min = 2147483647, m;
- for (int i = 0; i < n; i++)
- {
- m = *mas[i];//From the first element of the string we get its length
- if (i == x)
- for (int j = 1; j <= m; j++)
- if (mas[x][j] < min) min = mas[x][j];
- }
- return min; //Return the minimum
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement