Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #define FILE_NAME "numbers.txt"
- #define FILE_NUMBERS_ARRAY_SIZE 16
- void printArray(const int a[], int n) {
- printf("\n");
- printf("\n");
- printf("[");
- for(int i = 0; i < n; i++) {
- // printf with no dividers
- if(i + 1 == n) {
- printf("%d", a[i]);
- } else {
- printf("%d, ", a[i]);
- }
- }
- printf("]");
- printf("\n");
- printf("\n");
- }
- void printTwoDimensionalArray(int n, const int a[n][n]) {
- printf("\n");
- printf("\n");
- for(int i = 0; i < n; i++) {
- printf("row %d - ", i);
- for(int j = 0; j < n; j ++) {
- printf(" %d ", a[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- printf("\n");
- }
- void perform_task(int size, const int array[size][size]) {
- int countOfNegativeElements[size];
- for(int i = 0; i < size; i++) {
- countOfNegativeElements[i] = 0;
- }
- int maxCountOfNegativeElementsRow = -1;
- int maxCountOfNegativeElements = INT32_MIN;
- int maxElement = INT32_MIN;
- int indexRowMaxElement = INT32_MIN;
- int minElement = INT32_MAX;
- int indexRowMinElement = INT32_MIN;
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- int currentElement = array[i][j];
- if(currentElement < 0) {
- countOfNegativeElements[i] = countOfNegativeElements[i] + 1;
- }
- if(currentElement > maxElement) {
- maxElement = currentElement;
- indexRowMaxElement = i;
- }
- if(currentElement < minElement) {
- minElement = currentElement;
- indexRowMinElement = i;
- }
- }
- }
- for(int row = 0; row < size; row++) {
- int countOfNegativeElementsOfCurrentRow = countOfNegativeElements[row];
- if(countOfNegativeElementsOfCurrentRow > maxCountOfNegativeElements) {
- maxCountOfNegativeElements = countOfNegativeElementsOfCurrentRow;
- maxCountOfNegativeElementsRow = row;
- }
- printf("\n\nCount of negative elements at %d row is %d", row, countOfNegativeElementsOfCurrentRow);
- }
- printf("\n\nMax element %d", maxElement);
- printf("\nIndex Row with max element %d", indexRowMaxElement);
- printf("\n\nMin element %d", minElement);
- printf("\nIndex Row with min element %d", indexRowMinElement);
- printf("\n\nRow with max count negative ones %d", maxCountOfNegativeElementsRow);
- printf("\nIndex Row with max count of negative elements %d", indexRowMinElement);
- printf("\n\nMax count of negative elements %d at %d row", maxCountOfNegativeElements, maxCountOfNegativeElementsRow);
- int newArray[size][size];
- // make a copy of array
- for (int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- newArray[i][j] = array[i][j];
- }
- }
- // set zero to the row with max negative elements
- for(int column = 0; column < size; column++) {
- newArray[maxCountOfNegativeElementsRow][column] = 0;
- }
- printf("\nArray after zeroing the row with max count of negative elements");
- printTwoDimensionalArray(size, newArray);
- // replace the row with min element with the row with max element
- for(int column = 0; column < size; column++) {
- int tempElementOfRowWithMaxElement = newArray[indexRowMaxElement][column];
- newArray[indexRowMaxElement][column] = newArray[indexRowMinElement][column];
- newArray[indexRowMinElement][column] = tempElementOfRowWithMaxElement;
- }
- printf("\n Array after replacing the row with max element with the row with min element");
- printTwoDimensionalArray(size, newArray);
- printf("\n\n");
- printf("The side effect's diagonal elements");
- printf("\n");
- int sum = 0;
- for(int i = 1; i <= size; i++) {
- int row = i - 1;
- int column = size - i;
- int sideEffectElement = newArray[row][column];
- printf(" %d ", sideEffectElement);
- if(sideEffectElement % 2 == 0) {
- sum += sideEffectElement;
- }
- }
- printf("\n\nSum of odds and side effect diagonal's elements is %d", abs(sum));
- }
- void fill_array_via_key_board(int size) {
- int array[size][size];
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- array[i][j] = j;
- }
- }
- printf("Print the 2d keyboard's array");
- printTwoDimensionalArray(size, array);
- perform_task(size, array);
- }
- void fill_array_via_file(int size) {
- int numbers[FILE_NUMBERS_ARRAY_SIZE];
- int array[size][size];
- FILE *f = fopen(FILE_NAME, "r");
- if(f != NULL) { // file exists
- int n = 0;
- while(!feof(f)) {
- fscanf(f, "%d", &numbers[n++]);
- }
- } else {
- printf("file does not exist yet");
- }
- fclose(f);
- printf("\nPrint the file's array");
- printArray(numbers, FILE_NUMBERS_ARRAY_SIZE);
- //fill the 2d array by default values
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j ++) {
- array[i][j] = 0;
- }
- }
- // fill the 2d array by the file's numbers
- int currentIndexRow2dArray = -1;
- int currentIndexColumn2dArray = -1;
- for(int i = 0; i < FILE_NUMBERS_ARRAY_SIZE; i++) {
- if(i % size == 0) {
- currentIndexRow2dArray++;
- currentIndexColumn2dArray = -1;
- }
- currentIndexColumn2dArray++;
- int fileNumber = numbers[i];
- array[currentIndexRow2dArray][currentIndexColumn2dArray] = fileNumber;
- }
- printf("\nPrint the 2d file's array");
- printTwoDimensionalArray(size, array);
- perform_task(size, array);
- }
- void fill_array_via_range(int size) {
- int array[size][size];
- int A, B;
- printf("Введите range.");
- printf("\nstart: ");
- scanf("%d", &A);
- printf("end: ");
- scanf("%d", &B);
- if(A > B) {
- int temp = A;
- A = B;
- B = temp;
- }
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- int randomNumber = rand() % (B - A + 1) + A;
- array[i][j] = randomNumber;
- }
- }
- printf("\nPrint the 2d random range's array");
- printTwoDimensionalArray(size, array);
- perform_task(size, array);
- }
- int calculatedElementByFormula(int i, int j) {
- int element = i + j;
- if(i < j) {
- element = i - j;
- } else {
- if (i == j)
- {
- if(j != 0) {
- element = i/j;
- }
- }
- }
- return element;
- }
- void fill_array_via_formula(int size) {
- int array[size][size];
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- int element = calculatedElementByFormula(i, j);
- array[i][j] = element;
- }
- }
- printf("\nPrint the 2d formulas' array");
- printTwoDimensionalArray(size, array);
- perform_task(size, array);
- }
- void task_1() {
- int n;
- printf("\nEnter n to define the size of the array(N x N): ");
- scanf("%d", &n);
- if(n <= 0) {
- printf("\nn must be more than zero");
- } else {
- int array[n][n];
- int wayOfFilling;
- printf("\nEnter the way of filling the array: ");
- printf("\n1 - Via KeyBoard");
- printf("\n2 - Via File");
- printf("\n3 - Via Random numbers in range");
- printf("\n4 - Via Formula: ");
- printf("\n");
- scanf("%d", &wayOfFilling);
- switch(wayOfFilling) {
- case 1: fill_array_via_key_board(n); break;
- case 2: fill_array_via_file(n); break;
- case 3: fill_array_via_range(n); break;
- case 4: fill_array_via_formula(n); break;
- default: printf("Way by %d is not defined", wayOfFilling);
- }
- }
- }
- int main() {
- int numOfTask = 0;
- do
- {
- printf("\nEnter a number of the task: ");
- scanf("%d", &numOfTask);
- switch (numOfTask) {
- case 1:
- task_1();
- break;
- default:
- printf("\nThe task by num %d has not found", numOfTask);
- break;
- }
- } while (numOfTask != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement