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 10
- 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 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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