## Posted By

AlexanderRavikovich on 12/27/11

# Work 3 in C

/ Published in: Objective C

Buld menu & use pinter & dynamic arrays

`#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 optionsvoid my_fill(int arr[][N]); // fill 2d array with user entered datavoid my_print(int arr[][N]); // print 2d array  that we filledint magic_board(int arr[][N], int k); // Processing array that we filledint * build_array(int n); // create dynamic array & return his pointervoid print_dynamic(int * p, int n); // print array by pointerint checking(int * arr, int n, int * h); // cheerking array for number tha sum of elements before and after the sameint * 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(); 		scanf("%d", &option); 		printf("The choice is %d\n\n", option); 		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 */				printf("Enter number that less then %d\n", N);				scanf("%d", &k);				/* Check if function return 1 or 0 */				if (magic_board(math, k))				{					printf("\nThere is a magic sub-array of size %d\n\n", k);				}				else				{					printf("\nThere is NO a magic sub-array of size %d\n\n", k);				}			break; 			case 4:				printf("Enter size of the array:\n");				scanf("%d", &n);				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))				{					printf("The number %d with index %d divides the array in two equal parts.\n\n", p_array[h], h);				}				else				{					printf("There is no number that divides the array in two equal parts. Try another numbers.\n\n");				}			break; 			case 7:				printf("Enter a number for cleanning:\n");				scanf("%d", &k); 				p_array = cleanning(p_array, k, &n); // p_array takes address of new created array			break; 			case 8:				exit(1); // finish our programm			break;		}	}} /* Function my_fill */ void my_fill(int arr[][N]){	/* Setting local variables */	int user_num, i, j; 	printf("Enter numbers between 35-143\n"); 	/* 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 */				scanf("%d", &user_num);				/* 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				{					printf("Wrong number! Try again:\n");				}			} 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 */			printf("%3d\t", arr[i][j]);		}		printf("\n\n");	}} /* 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; 			memset(sum, 0, sizeof(int)*(N*2+2)); 			/* 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(){	printf("Choose function:\n"	"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; 	p_array = (int*) malloc(n*sizeof(int)); 	/* Validating of memory */	if(p_array == NULL)	{	   fprintf(stderr, "Out of memory, exiting\n");	   exit(1);	} 	 printf("Enter %d numbers between -30 to 30:\n", n); 	for (i=0; i < n; i++)	{		do		 {			/* Get entered number */			scanf("%d", &user_num);			if (user_num < 30 && user_num > -30)			{				p_array[i] = user_num;			}			else			{				printf("Wrong number, try again!\n");			}		}while(!(user_num < 30 && user_num > -30));	} 	printf("End of dyamic array.\n"); 	return p_array;} void print_dynamic(int * p, int n){	int i; 	printf("The dynamic array:\n"); 	for (i=0; i<n; i++)	{		printf("%d ", p[i]);	}	printf("\n\n");} 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 */	new_p_array = (int*) malloc(*n*sizeof(int)); 	/* Validating of memory */	if(new_p_array == NULL)	{	   fprintf(stderr, "Out of memory, exiting\n");	   exit(1);	} 	/* 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 */	free(arr); 	/* Return array (pointer?, adress?) something like that, because function defined with star (int * cleanning bla bla bla */	return new_p_array;}`