## Posted By

AlexanderRavikovich on 01/10/12

# Work 4 in C

/ Published in: C

Work 4 in C

Pointers Recursion

`#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <ctype.h> #define ARR_SIZE 10#define STR_SIZE 10#define H 4#define N 8#define M 6 /* Prototype */void menu(); /* Menu function */int check_sort(int * arr, int size); /* Check if array is sorted DESC */void reducing(char * string); /* Takes string and print out only digits, in reverse order */int slope_rec(int data[][H], int index); /* Recursive function for checking matrix for slope */int isPositive(int data[][H], int index); /*check for positive numbers */int isNegative(int data[][H], int index); /* check for negative numbers */int check_zone(int nums[M][N], int x, int y, int num);int checking(int nums[M][N], int x, int y);int countZeros(int nums[M][N]); /* Help function to count zeros */ void main(){	int i=0, j=0, user_num=0, arr[ARR_SIZE], option, data[H][H]={0};	char str[STR_SIZE];	int res = 0;	int x, y; 	/* Copy temp to nums, to run item 4 many times, because we are setting zeros in array */ 	int temp[M][N] = {		{4, 3 ,2 ,3 ,1 ,3 ,2 ,1},		{5, 3, 3, 2, 2, 2, 1, 2},		{1 ,1 ,3 ,3, 2, 1, 2, 1}, 		{1 ,3 ,3 ,2 ,4 ,3 ,3 ,2},		{5 ,5 ,3 ,4 ,4 ,4 ,3 ,2},		{5 ,3 ,4 ,2 ,2 ,2 ,3 ,2}	}; 	int nums[M][N] = {0}; 	while(1)	{		/* Copy values to nums array */		for (i=0; i<M; i++)		{			for (j=0; j<N; j++)			{				nums[i][j] = temp[i][j];			}		} 		menu(); 		/* Get value of option choosed by user */		scanf("%d", &option);		getchar(); /* Hack for eating last \n */ 		switch (option)		{			case 1:				printf("Enter array size %d for check sorting:\n", ARR_SIZE); 				for (i = 0; i < ARR_SIZE; i++)				{					scanf("%d", &arr[i]);				} 				printf("\nThe array is:\n"); 				for (i = 0; i < ARR_SIZE; i++)				{					printf("%d  ", arr[i]);				} 				res = check_sort(arr, ARR_SIZE); /* call function and set res the value it return */ 				if (res)				{					printf("\nThe array is sorted!\n");				}				else				{					printf("\nThe array is NO sorted!\n");				}			break; 			case 2:				printf("Enter string for reducing:\n"); 				gets(str); /* Insert string to char variable (array) */				printf("\nThe reduced string is:\n");				reducing(str); /* Call for function */				printf("\n\n");			break; 			case 3:				printf("Enter array size %d X  %d for check slope:\n", H, H); 				/* Fill array with numbers */				for (i=0; i < H; i++)				{					printf("Enter row %d:\n", i); 					for (j=0; j<H; j++)					{						scanf("%d", &data[i][j]);					}				} 				printf("\nThe array is:\n"); 				/* Print matrix */				for (i=0; i < H; i++)				{					printf("\n"); 					for (j=0; j<H; j++)					{						printf("%3d\t", data[i][j]);					}				} 				printf("\n"); 				/* Call for function that checks if matrix is slope */				res = slope_rec(data, 0); 				if (res)				{					printf("The array is slope!\n");				}				else				{					printf("The array is NOT slope!\n");				}			break; 			case 4:				/* Get coordinates */				do				{					printf("Enter the coordinates for number:\n"); 					/* Get values from user */					scanf("%d", &x);					scanf("%d", &y); 					if (x < 0 || x > M-1 || y < 0 || y > N-1)					{						printf("Wrong number!\n");					}				} while(x < 0 || x > M-1 || y < 0 || y > N-1); 				/* Print value of place (x,y) in matrix */				printf("The size of the zone is : %d\n", nums[x][y]); 				/* Reset variable for saving result */				res = 0; 				/* Call for recursive function */				res = checking(nums , x , y); 				printf("The size of the zone is : %d\n", res);			break; 			case 5:				exit(1);			break;		}	}} int countZeros(int nums[M][N]){	int count = 0, i, j; 	for (i=0; i<M; i++)	{		for (j=0; j<N; j++)		{			if (!nums[i][j])			{				count++;			}		}	} 	return count;} int checking(int nums[][N], int x, int y){	return check_zone(nums, x, y, nums[x][y]);} /* Function that check values of recursively by x,y set 0 if value is checked [x-1,y-1] [x+1,y] [x+1,y+1][  x,y-1] [ x,y ] [  x,y+1][x-1,y-1] [x+1,y] [x+1,y+1] */int check_zone(int nums[M][N], int x, int y, int num){	if (x < M-1 && nums[x+1][y] == num)	{		nums[x+1][y] = 0;		check_zone(nums, x+1, y, num);	} 	if (x > 0 && nums[x-1][y] == num)	{		nums[x-1][y] = 0;		check_zone(nums, x-1, y, num);	} 	if (y > 0 && nums[x][y-1] == num)	{		nums[x][y-1] = 0;		check_zone(nums, x, y-1, num);	} 	if (y < N-1 && nums[x][y+1] == num)	{		nums[x][y+1] = 0;		check_zone(nums, x, y+1, num);	} 	/* Diagonal */ 	if (x < M-1 && y < N-1 && nums[x+1][y+1] == num)	{		nums[x+1][y+1] = 0;		check_zone(nums, x+1, y+1, num);	} 	if (x > 0 && y > 0 && nums[x-1][y-1] == num)	{		nums[x-1][y-1] = 0;		check_zone(nums, x-1, y-1, num);	} 	if (x > 0 && y < N-1 && nums[x-1][y+1] == num)	{		nums[x-1][y+1] = 0;		check_zone(nums, x-1, y+1, num);	} 	if (x < M-1 && y > 0 && nums[x+1][y-1] == num)	{		nums[x+1][y-1] = 0;		check_zone(nums, x+1, y-1, num);	} 	/* Now count zeros & return value */	return countZeros(nums);}/* Checking if matrix is slope */ int slope_rec(int data[][H], int index){	if (index < H)	{		/* If value in diagonal not 0, return 0 - no reason to continue */		if (data[index][index] != 0)		{			return 0;		} 		/* If not stopped before, checking numbers from left to right for num >= 0 */		if (!isPositive(data, index))		{			return 0;		} 		/* If not stopped before, checking numbers from left to right for num < 0 */		if (!isNegative(data, index))		{			return 0;		} 		/* Call recursively for funcion, to check next diagonal value */		return slope_rec(data, index+1);	}	else	{		return 1;	}} /* If value is < 0 return 0, if not - 1 */int isPositive(int data[][H], int index){	int i = index; 	for (i = index; i < H-index; i++)	{		if (data[index][i] < 0)		{			return 0;		}	} 	return 1;} /* If value is > -1 return 0, if not - return 1 */int isNegative(int data[][H], int index){	int i; 	for (i = 0; i < index; i++)	{		if (data[index][i] > -1)		{			return 0;		}	} 	return 1;} /* Function that checks if value is a number, and prints it.access value of array by pointer, and incrising adress of array's keys  first we increase adress, and after all loops are ended,we print value from the end to begin*/ void reducing(char *string){	if (*string != '\0')	{		reducing(string+1); 		if (isdigit(*string))		{			printf("%c", *string);		}	}} /* Function that checks if array is sorted DESC */int check_sort(int * arr, int size){	if (size > 0)	{		if (arr[size-1] > arr[size-2])		{			return check_sort(arr, size-1);		}		else		{			return 0;		}	}	else	{		return 1;	}} /* Function that prints options */void menu(){	printf("Choose function:\n"	"1-Check sorting\n"	"2-Reducing\n"	"3-Sloping \n"	"4-Check zone\n"	"5-Exit\n");}`