/ Published in: Objective C
Buld menu & use pinter & dynamic arrays
Expand |
Embed | Plain Text
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> /* Setting matrix size NxN */ #define N 5 /* Prototype */ void my_menu(); // Printing menu options void my_fill(int arr[][N]); // fill 2d array with user entered data void my_print(int arr[][N]); // print 2d array that we filled int magic_board(int arr[][N], int k); // Processing array that we filled int * build_array(int n); // create dynamic array & return his pointer void print_dynamic(int * p, int n); // print array by pointer int checking(int * arr, int n, int * h); // cheerking array for number tha sum of elements before and after the same int * cleanning(int * arr, int k, int * n); // return pointer of new created array with values from original array, that not devides by k void main() { int option; // option for menu int n; // number of elements in dynamic array /* Matrix that we will working on */ int math[N][N] = {0}; // static 2d array (matrix) int k; // used for getting one number form user int *p_array; // p_array is a pointer int h; // stores index of element in array, that contains number that devides array to 2 by sum of elements while(1) { /* Print my menu options */ my_menu(); switch(option) { case 1: /* Pass pointer to "math" to our function to fill it */ my_fill(math); break; case 2: /* Pass pointer to filled "math" array to function "my_print" for print it's data */ my_print(math); break; case 3: /* Promt user to enter positive number, for finding magic cube */ /* Check if function return 1 or 0 */ if (magic_board(math, k)) { } else { } break; case 4: p_array = build_array(n); break; case 5: print_dynamic(p_array, n); break; case 6: /* p_array pointer (is auto converted), & we send adress of "h", not his value */ if (checking(p_array, n, &h)) { } else { } break; case 7: p_array = cleanning(p_array, k, &n); // p_array takes address of new created array break; case 8: break; } } } /* Function my_fill */ void my_fill(int arr[][N]) { /* Setting local variables */ int user_num, i, j; /* Fill matrix NxN with numbers from user */ for(i=0; i < N; i++) { for (j=0; j < N; j++) { /* Keep user in loop until he enter right number */ do { /* Get entered number */ /* User input validating */ if (user_num < 143 && user_num > 35) { /* Fill array with entered number (we are really changing "math" array because "arr" is a pointer */ arr[i][j] = user_num; } else { } } while(!(user_num < 143 && user_num > 35)); } } } /* Function my_print for printing 2d array */ void my_print(int arr[][N]) { /* Setting local variables */ int i, j; /* Print out our data */ for(i=0; i < N; i++) { for (j=0; j < N; j++) { /* Print fromatted data */ } } } /* Find magic board in our math */ int magic_board(int arr[][N], int k) { /* Local variables */ int i,j,a,b,z, j_pos=0, row_sum=0, col_sum=0, diag_sum=0, ctr=0, magic_exists=0; int sum[N*2+2]; for (i=0; i <= N-k; i++) { if (magic_exists) { return magic_exists; } for (j=0; j <= N-k; j++) { if (magic_exists) { return magic_exists; } ctr = 0; /* Row sum */ for (a=i; a<k+i; a++) { row_sum = 0; for (b=j; b<k+j; b++) { row_sum += arr[a][b]; } /* Save sum of row */ sum[ctr] = row_sum; ctr++; } /* Cols sum */ for (a=i; a<k+i; a++) { col_sum = 0; for (b=j; b<k+j; b++) { col_sum += arr[b][a]; } /* Save sum of col */ sum[ctr] = col_sum; ctr++; } /* Diagonal sum 1 */ diag_sum = 0; j_pos = j; // <-- remember position of LEFT TOP j (column), and make him ++ for (a=i; a<k+i; a++) { diag_sum += arr[a][j_pos]; j_pos++; } /* Save sum of diag 1 */ sum[ctr] = diag_sum; ctr++; /* Diagonal sum 2 */ diag_sum = 0; j_pos = j+k-1; /* <-- calculate position of RIGHT TOP j (column), and make him -- (I do 2nd diagonal from RIGHT TOP to LEFT BOTTOM) */ for (a=i; a<k+i; a++) { diag_sum += arr[a][j_pos]; j_pos--; } /* Save sum of diag 2 */ sum[ctr] = diag_sum; /* Now we have array like sum = {15,15,15,15,15,15,15,15,15,15,15} Validate that all numbers is the same. If we have something like sum = {15,15,15,15,15,33,15,15,15,15,15} it's not magik cube */ /* Validating found sums */ for(z=0; z < ctr; z++) { if(sum[z] == sum[z+1]) { magic_exists = 1; } else { magic_exists = 0; break; } } } } return magic_exists; } /* Printing menu */ void my_menu() { "1-Filling array\n" "2-Printing array\n" "3-If Magic Board\n" "4-Build Array\n" "5-Printing array\n" "6-Checking Array\n" "7-Cleanning Array\n" "8-Exit\n"); } int * build_array(int n) { int i, user_num; /* Pointer to dynamic array with n elements */ int * p_array; /* Validating of memory */ if(p_array == NULL) { } for (i=0; i < n; i++) { do { /* Get entered number */ if (user_num < 30 && user_num > -30) { p_array[i] = user_num; } else { } }while(!(user_num < 30 && user_num > -30)); } return p_array; } void print_dynamic(int * p, int n) { int i; for (i=0; i<n; i++) { } } int checking(int * arr, int n, int * h) { int i, j; int sum_left = 0, sum_right = 0; for (i = 0; i < n; i++) { sum_left += arr[i]; /* sum of numbers from left to right */ sum_right = 0; for (j = n-1; j > i; j--) { sum_right += arr[j]; /* Sum of numbers from right ot left */ } if (sum_left == sum_right) { *h = i; /* "*h" point to the adress of "h" that in main function, so its changes value of "h" that in main func */ return 1; } } return 0; } /* Function for cleaning array */ int * cleanning (int * arr, int k, int * n) { int i, members=0, t=*n, z=0; int * new_p_array; /* Her i am just count elements for new array */ for (i = 0; i < t; i++) { if (arr[i] % k) { members++; } } /* Give variable n value of members, using pointer (insert instantly to the memory adress) */ *n = members; /* Creating new array */ /* Validating of memory */ if(new_p_array == NULL) { } /* Here I copy elements from old array to new */ for (i = 0; i < t; i++) { if (arr[i] % k != 0) { new_p_array[z] = arr[i]; z++; } } /* Deleting old array */ /* Return array (pointer?, adress?) something like that, because function defined with star (int * cleanning bla bla bla */ return new_p_array; }
You need to login to post a comment.
