Advertisement
Kostiggig

LAB_17./

Dec 22nd, 2022
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6.  
  7. #define FILE_NAME "numbers.txt"
  8. #define FILE_NUMBERS_ARRAY_SIZE 16
  9.  
  10. void printArray(const int a[], int n) {
  11.     printf("\n");
  12.     printf("\n");
  13.     printf("[");
  14.     for(int i = 0; i < n; i++) {
  15.  
  16.         // printf with no dividers
  17.         if(i + 1 == n) {
  18.             printf("%d", a[i]);
  19.         } else {
  20.             printf("%d, ", a[i]);
  21.         }
  22.     }
  23.     printf("]");
  24.     printf("\n");
  25.     printf("\n");
  26. }
  27.  
  28. void printTwoDimensionalArray(int n, const int a[n][n]) {
  29.     printf("\n");
  30.     printf("\n");
  31.     for(int i = 0; i < n; i++) {
  32.         printf("row %d -  ", i);
  33.         for(int j = 0; j < n; j ++) {
  34.             printf(" %d ", a[i][j]);
  35.         }
  36.         printf("\n");
  37.     }
  38.     printf("\n");
  39.     printf("\n");
  40. }
  41.  
  42. void perform_task(int size, const int array[size][size]) {
  43.  
  44.     int countOfNegativeElements[size];
  45.  
  46.     for(int i = 0; i < size; i++) {
  47.         countOfNegativeElements[i] = 0;
  48.     }
  49.  
  50.     int maxCountOfNegativeElementsRow = -1;
  51.     int maxCountOfNegativeElements = INT32_MIN;
  52.  
  53.     int maxElement = INT32_MIN;
  54.     int indexRowMaxElement = INT32_MIN;
  55.     int minElement = INT32_MAX;
  56.     int indexRowMinElement = INT32_MIN;
  57.  
  58.     for(int i = 0; i < size; i++) {
  59.         for(int j = 0; j < size; j++) {
  60.             int currentElement = array[i][j];
  61.  
  62.             if(currentElement < 0) {
  63.                 countOfNegativeElements[i] = countOfNegativeElements[i] + 1;
  64.             }
  65.  
  66.             if(currentElement > maxElement) {
  67.                 maxElement = currentElement;
  68.                 indexRowMaxElement = i;
  69.             }
  70.  
  71.             if(currentElement < minElement) {
  72.                 minElement = currentElement;
  73.                 indexRowMinElement = i;
  74.             }
  75.         }
  76.     }
  77.  
  78.     for(int row = 0; row < size; row++) {
  79.         int countOfNegativeElementsOfCurrentRow = countOfNegativeElements[row];
  80.  
  81.         if(countOfNegativeElementsOfCurrentRow > maxCountOfNegativeElements) {
  82.             maxCountOfNegativeElements = countOfNegativeElementsOfCurrentRow;
  83.             maxCountOfNegativeElementsRow = row;
  84.         }
  85.  
  86.         printf("\n\nCount of negative elements at %d row is %d", row, countOfNegativeElementsOfCurrentRow);
  87.     }
  88.  
  89.     printf("\n\nMax element %d", maxElement);
  90.     printf("\nIndex Row with max element %d", indexRowMaxElement);
  91.  
  92.     printf("\n\nMin element %d", minElement);
  93.     printf("\nIndex Row with min element %d", indexRowMinElement);
  94.  
  95.     printf("\n\nRow with max count negative ones %d", maxCountOfNegativeElementsRow);
  96.     printf("\nIndex Row with max count of negative elements %d", indexRowMinElement);
  97.  
  98.  
  99.     printf("\n\nMax count of negative elements %d at %d row", maxCountOfNegativeElements, maxCountOfNegativeElementsRow);
  100.  
  101.     int newArray[size][size];
  102.  
  103.     // make a copy of array
  104.     for (int i = 0; i < size; i++) {
  105.         for(int j = 0; j < size; j++) {
  106.             newArray[i][j] = array[i][j];
  107.         }
  108.     }
  109.     // set zero to the row with max negative elements
  110.     for(int column = 0; column < size; column++) {
  111.         newArray[maxCountOfNegativeElementsRow][column] = 0;
  112.     }
  113.  
  114.     printf("\nArray after zeroing the row with max count of negative elements");
  115.     printTwoDimensionalArray(size, newArray);
  116.  
  117.     // replace the row with min element with the row with max element
  118.     for(int column = 0; column < size; column++) {
  119.         int tempElementOfRowWithMaxElement = newArray[indexRowMaxElement][column];
  120.         newArray[indexRowMaxElement][column] = newArray[indexRowMinElement][column];
  121.         newArray[indexRowMinElement][column] = tempElementOfRowWithMaxElement;
  122.     }
  123.  
  124.     printf("\n Array after replacing the row with max element with the row with min element");
  125.     printTwoDimensionalArray(size, newArray);
  126.  
  127.     printf("\n\n");
  128.     printf("The side effect's diagonal elements");
  129.     printf("\n");
  130.  
  131.     int sum = 0;
  132.  
  133.     for(int i = 1; i <= size; i++) {
  134.         int row = i - 1;
  135.         int column = size - i;
  136.         int sideEffectElement = newArray[row][column];
  137.        
  138.         printf(" %d ", sideEffectElement);
  139.         if(sideEffectElement % 2 == 0) {
  140.             sum += sideEffectElement;
  141.         }
  142.     }
  143.  
  144.     printf("\n\nSum of odds and side effect diagonal's elements is %d", abs(sum));
  145. }
  146.  
  147. void fill_array_via_key_board(int size) {
  148.     int array[size][size];
  149.     for(int i = 0; i < size; i++) {
  150.         for(int j = 0; j < size; j++) {
  151.             array[i][j] = j;
  152.         }
  153.     }
  154.  
  155.     printf("Print the 2d keyboard's array");
  156.     printTwoDimensionalArray(size, array);
  157.  
  158.     perform_task(size, array);
  159. }
  160.  
  161. void fill_array_via_file(int size) {
  162.     int numbers[FILE_NUMBERS_ARRAY_SIZE];
  163.     int array[size][size];
  164.  
  165.     FILE *f = fopen(FILE_NAME, "r");
  166.    
  167.     if(f != NULL) { // file exists
  168.         int n = 0;
  169.         while(!feof(f)) {
  170.             fscanf(f, "%d", &numbers[n++]);
  171.         }
  172.     } else {
  173.         printf("file does not exist yet");
  174.     }
  175.     fclose(f);
  176.  
  177.     printf("\nPrint the file's array");
  178.     printArray(numbers, FILE_NUMBERS_ARRAY_SIZE);
  179.  
  180.     //fill the 2d array by default values
  181.     for(int i = 0; i < size; i++) {
  182.         for(int j = 0; j < size; j ++) {
  183.              array[i][j] = 0;
  184.         }
  185.     }
  186.  
  187.     // fill the 2d array by the file's numbers
  188.     int currentIndexRow2dArray = -1;
  189.     int currentIndexColumn2dArray = -1;
  190.     for(int i = 0; i < FILE_NUMBERS_ARRAY_SIZE; i++) {
  191.         if(i % size == 0) {
  192.             currentIndexRow2dArray++;
  193.             currentIndexColumn2dArray = -1;
  194.         }
  195.         currentIndexColumn2dArray++;
  196.         int fileNumber = numbers[i];
  197.        
  198.         array[currentIndexRow2dArray][currentIndexColumn2dArray] = fileNumber;
  199.     }
  200.  
  201.     printf("\nPrint the 2d file's array");
  202.     printTwoDimensionalArray(size, array);
  203.  
  204.     perform_task(size, array);
  205. }
  206.  
  207. void fill_array_via_range(int size) {
  208.     int array[size][size];
  209.  
  210.     int A, B;
  211.     printf("Введите range.");
  212.    
  213.     printf("\nstart: ");
  214.     scanf("%d", &A);
  215.  
  216.     printf("end: ");
  217.     scanf("%d", &B);
  218.  
  219.     if(A > B) {
  220.         int temp = A;
  221.         A = B;
  222.         B = temp;
  223.     }
  224.  
  225.     for(int i = 0; i < size; i++) {
  226.         for(int j = 0; j < size; j++) {
  227.             int randomNumber = rand() % (B - A + 1) + A;
  228.             array[i][j] = randomNumber;
  229.         }
  230.     }
  231.  
  232.     printf("\nPrint the 2d random range's array");
  233.     printTwoDimensionalArray(size, array);
  234.     perform_task(size, array);
  235. }
  236.  
  237. int calculatedElementByFormula(int i, int j) {
  238.     int element = i + j;
  239.     if(i < j) {
  240.         element = i - j;
  241.     } else {
  242.         if (i == j)
  243.         {
  244.             if(j != 0) {
  245.                 element = i/j;
  246.             }
  247.         }
  248.     }
  249.     return element;
  250. }
  251.  
  252. void fill_array_via_formula(int size) {
  253.     int array[size][size];
  254.  
  255.      for(int i = 0; i < size; i++) {
  256.         for(int j = 0; j < size; j++) {
  257.             int element = calculatedElementByFormula(i, j);
  258.             array[i][j] = element;
  259.         }
  260.     }
  261.  
  262.     printf("\nPrint the 2d formulas' array");
  263.     printTwoDimensionalArray(size, array);
  264.  
  265.     perform_task(size, array);
  266. }
  267.  
  268.  
  269. void task_1() {
  270.     int n;
  271.    
  272.     printf("\nEnter n to define the size of the array(N x N): ");
  273.     scanf("%d", &n);
  274.     if(n <= 0) {
  275.         printf("\nn must be more than zero");
  276.     } else {
  277.         int array[n][n];
  278.         int wayOfFilling;
  279.  
  280.         printf("\nEnter the way of filling the array: ");
  281.  
  282.         printf("\n1 - Via KeyBoard");
  283.         printf("\n2 - Via File");
  284.         printf("\n3 - Via Random numbers in range");
  285.         printf("\n4 - Via Formula: ");
  286.  
  287.         printf("\n");
  288.  
  289.         scanf("%d", &wayOfFilling);
  290.  
  291.         switch(wayOfFilling) {
  292.             case 1: fill_array_via_key_board(n); break;
  293.             case 2: fill_array_via_file(n); break;
  294.             case 3: fill_array_via_range(n); break;
  295.             case 4: fill_array_via_formula(n); break;
  296.             default: printf("Way by %d is not defined", wayOfFilling);
  297.         }
  298.  
  299.     }
  300. }
  301.  
  302. int main() {
  303.     int numOfTask = 0;
  304.     do
  305.     {
  306.         printf("\nEnter a number of the task: ");
  307.         scanf("%d", &numOfTask);
  308.  
  309.         switch (numOfTask) {
  310.         case 1:
  311.             task_1();
  312.             break;
  313.         default:
  314.             printf("\nThe task by num %d has not found", numOfTask);
  315.             break;
  316.         }
  317.  
  318.     } while (numOfTask != 0);
  319.  
  320.     return 0;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement