Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #define MAX 100
- void display_menu();
- int check_option(int option);
- int check_size (int size);
- void initialize_2Darray(int x[][MAX], int size);
- void print_2Darray(int x[][MAX], int size);
- void initialize_1Darray(int y[], int size);
- void print_1Darray(int y[], int size);
- int search_min (int x[][MAX], int r, int c, int size);
- int count_match(int x[][MAX], int y[], int size, int r);
- int closest_row(int x[][MAX], int y[], int size);
- void sort_1Darray(int y[], int size);
- void sort_2Darray(int x[][MAX], int size);
- int
- main()
- {
- int size , r, c, option;
- int exit = 0;
- //exits program when = 1
- int x [MAX][MAX], y[MAX];
- srand(time(NULL));
- printf("Enter the size: ");
- scanf("%d", &size);
- while (check_size(size) == 0) {
- printf("\nInvalid input, enter the size of the array again: ");
- scanf("%d", &size);
- }
- while (exit != 6) {
- initialize_2Darray(x, size);
- initialize_1Darray(y, size);
- display_menu();
- scanf("%d", &option);
- while (check_option(option) == 0) {
- printf("Invalid option, enter again: ");
- scanf("%d", &option);
- }
- //Search Min Operation
- if (option == 1) {
- print_2Darray(x, size);
- printf("Enter the row: ");
- scanf("%d", &r);
- printf("\nEnter the col: ");
- scanf("%d", &c);
- printf("The smallest number present in row %d and col %d is %d", r, c, search_min(x, r, c, size));
- }
- //Count Matches Op.
- else if (option == 2) {
- printf("Count Matches Operation\n\n2D array\n");
- print_2Darray(x, size);
- printf("\n1D array\n");
- print_1Darray(y, size);
- printf("\nEnter the row: ");
- scanf("%d", &r);
- if (count_match(x, y, size, r) > 0)
- printf("There are %d matches from 1D array present in 2D array", count_match(x, y, size, r));
- else
- printf("There are no numbers from 1D array present in 2D array");
- }
- //Closest Row Op.
- else if (option == 3) {
- printf("\nClosest Row Operation\n\n2D array\n");
- print_2Darray(x, size);
- printf("\n1D array\n");
- print_1Darray(y, size);
- printf("Row closest to the 1D array is row %d", closest_row(x, y, size));
- }
- //Sort 1 D Array Op.
- else if (option == 4) {
- printf("Sort 1D Array Operation\n\n1D Array before sorting:\n");
- print_1Darray(y, size);
- sort_1Darray(y, size);
- printf("\n\n1D Array after sorting:\n");
- print_1Darray(y, size);
- }
- //Sort 2 D Array Op.
- else if (option == 5) {
- printf("\nSort 2D Array Option\n\n2D Array before sorting:\n");
- print_2Darray(x, size);
- sort_2Darray(x, size);
- printf("\n\n2D Array after sorting:\n");
- print_2Darray(x, size);
- }
- //Exit
- else if (option == 6)
- exit = 6;
- }
- return 0;
- }
- void
- display_menu()
- {
- printf("\nArray operations, your options are:\n\n1: Search Min\n2: Count Matches\n3: Closest Row\n4: Sort 1D Array\n5: Sort 2D Array\n6: Exit\nEnter the operation you want to perform: ");
- }
- int
- check_option(int option)
- {
- if (option > 0 && option < 7)
- return 1;
- else
- return 0;
- }
- int
- check_size(int size)
- {
- if (size > 0 && size <= 100)
- return 1;
- else
- return 0;
- }
- void
- initialize_2Darray(int x[][MAX], int size)
- {
- int i , s;
- //counters
- for (i = 0; i < size; i++) {
- for (s = 0; s < size; s++) {
- x[i][s] = rand() % 10;
- }
- }
- }
- void
- print_2Darray(int x[][MAX], int size)
- {
- int i , s;
- //counters
- for (i = 0; i < size; i++) {
- printf("\n");
- for (s = 0; s < size; s++) {
- printf("%d ", x[i][s]);
- }
- }
- printf("\n");
- }
- void
- initialize_1Darray(int y[], int size)
- {
- int i , r;
- for (i = 0; i < size; i++) {
- r = rand() % 10;
- y[i] = r;
- }
- }
- void
- print_1Darray(int y[], int size)
- {
- int i;
- //Prints array values until(s) ize is reached
- for (i = 0; i < size; i++) {
- printf("%d ", y[i]);
- }
- }
- int
- search_min(int x[][MAX], int r, int c, int size)
- {
- int i , j;
- //counters
- int min = 9;
- for (i = 0; i < size; i++) {
- if (x[r][i] < min) {
- min = x[r][i];
- }
- }
- for (j = 0; j < size; j++) {
- if (x[j][c] < min) {
- min = x[j][c];
- }
- }
- return min;
- }
- int
- count_match(int x[][MAX], int y[], int size, int r)
- {
- int i , j, count;
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- if (y[i] == x[r][j])
- count++;
- }
- return count;
- }
- int closest_row(int x[][MAX], int y[], int size){
- int l , i, j;
- //counters
- int sum = 0;
- int dif = 0;
- int row , totaldif;
- //best matching row & total dif
- for (l = 0; l < size; l++) {
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- dif = abs(y[j] - x[i][j]);
- sum += dif;
- }
- if (sum < totaldif) {
- totaldif = sum;
- row = i;
- }
- sum = 0;
- }
- }
- return row;
- }
- void sort_1Darray(int y[], int size){
- int a , b, temp;
- //Loops through the array, swapping values until in proper order of ascension
- for (a = 0; a < size; a++) {
- for (b = 0; b < size - 1; b++) {
- if (y[b] > y[b + 1]) {
- temp = y[b + 1];
- y[b + 1] = y[b];
- y[b] = temp;
- }
- }
- }
- }
- void sort_2Darray(int x[][MAX], int size){
- int a , b, c, temp;
- //Loops through the array, swapping values until in proper order of ascension
- for (c = 0; c < size; c++) {
- for (a = 0; a < size; a++) {
- for (b = 0; b < size - 1; b++) {
- if (x[a][b] > x[a][b + 1]) {
- temp = x[a][b + 1];
- x[a][b + 1] = x[a][b];
- x[a][b] = temp;
- }
- }
- }
- }
- /**INDENT** Error@234: Stuff missing from end of file */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement