Advertisement
Kostiggig

lab 17 final version

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