## Posted By

AlexanderRavikovich on 12/16/11

# Work 2 in C

/ Published in: C

Home work 2

`#include <stdio.h>#include <string.h> #define ARR_SIZE 101#define M 3#define N 3 void main(){	/*	i, j, a, b, z, c - counters in loops	*/ 	int numbers_arr[ARR_SIZE];	int user_input, i=0, j=0, a=0, b=0, z=0, p=0, c=0, count=0, average=0, sum=0, arr_size=0, t, again = 1;	char str[81];	int math[M][N];	int max_sum=0, math_start_i=0, math_start_j=0, row_id=0, col_id=0;	int avr[M], avr_sorted[M]; 	/******************************** ITEM 1 *******************************/ 	printf("Item 1\n"); 	do	{		count = 0; // reset for use in loop		sum = 0;		i = 0; 		printf("Enter maximum 100 positive integers and negative one to end input:\n"); 		do		{			/*			Get positive numbers from user, maximum 100 to array. To end input, write an negative number.			*/			scanf("%d", &user_input); 			if (user_input >= 0)			{				numbers_arr[i] = user_input;				i++; 				sum += user_input;			}			else			{				printf("Array finish!\n");			}		}		while(!(user_input < 0)); 		average = sum/(i); 		arr_size = i; 		printf("\nAverage is:%d\n", average); 		for (i=0; i < arr_size; i++)		{			if (numbers_arr[i] > average)			{				c = 0;				for (j = 2; j < numbers_arr[i]; j++)				{					p = numbers_arr[i] % j;					if (p == 0)					{						c = 1;						break;					}				} 				if (c == 0)				{					printf("\nNum is:%d\n", numbers_arr[i]);				}			}		} 		/* Find numbers that n-1 < n < n+1*/		for (i=1; i < arr_size; i++)		{			if (numbers_arr[i+1] > numbers_arr[i] && numbers_arr[i-1] > numbers_arr[i])			{				count++;			}		} 		printf("\nThe number of little numbers is:%d\n", count); 		printf("Item 1 again ? (yes = 1 / no = 0)\n");		scanf("%d", &again);		getchar(); //flush the keyboard buffer of the '\n' key	}	while (again); 	again = 1; // reset for the next use  	/******************************** ITEM 2 *******************************/	/* Remove extra backspaces from string */ 	printf("Item 2\n"); 	do	{		/* Promt user to enter data */		printf("Enter string to 80 characters\n\n");		/* Put user's data to str var */		gets(str);		/* reset i for counting in next while */		i = 0;		/* Go over array untill we get 0 (end of entred string) */		while(str[i] != 0)		{			/* If char is not backspace, print him */			if (str[i] != ' ')			{				printf("%c", str[i]);			}			else if (i != 0 && str[i-1] != ' ')			{				/* If char is backspace, check if previous char in array wasnt backspace too. If not - print it */				printf("%c", str[i]);			}			i++;		} 		printf("\nItem 2 again ? (yes = 1 / no = 0)\n");		scanf("%d", &again);		getchar(); //flush the keyboard buffer of the '\n' key	}	while (again); 	again = 1; // reset for the next use 	/************** ITEM 3 **************/ 	printf("Item 3\n"); 	do	{		sum = 0; 		/* Prom to user enter data */		printf("Enter positive integer smaller than 2000:\n");		/* Loop twice to fill our matrix */		for (i=0; i < M; i++)		{			for (j=0; j < N; j++)			{				/* If user entered wrong number, kepp him in the while until he enter right number */				do				{					/* Get what user entered */					scanf("%d", &user_input);					if (user_input >= 0 && user_input < 2000)					{						/* Put number to the matrix */						math[i][j] = user_input;					}					else					{						printf("Wrong number entered! Try again!\n");					}						}				while(!(user_input >= 0 && user_input < 2000));			}		}		/* Promt user to give number that smaller then N and M*/		printf("Enter positive integer small than %d and %d:\n", N, M);		/* Keep user in loop until he enter right number */		do		{			/* Get user entered data */			scanf("%d", &user_input); 			if (!(user_input < N && user_input < M))			{				printf("Wrong number! Try again!\n");			}		}while(!(user_input < N && user_input < M)); 		/* Loop out matrix to find possible combinations of matrix KxK & max sum of it's numbers */		for (i=0; i <= M-user_input; i++)		{			for (j=0; j <= N-user_input; j++)			{				for (a=i; a<user_input+i; a++)				{					if (z == 0)					{						row_id = i;						col_id = j;					}					for (b=j; b<user_input+j; b++)					{						sum += math[a][b];					}					z++;				} 				if (sum > max_sum)				{					math_start_i = row_id;					math_start_j = col_id;					max_sum = sum;				}				z = 0;				sum = 0;			}		} 		/* Print original math */		for (i=0; i < M; i++)		{			for (j=0; j < N; j++)			{				printf("%d\t", math[i][j]);			}			printf("\n\n");		} 		printf("\n\n"); 		/* Printing sub math with max sum of numbers from top left ij saved in privious steps */		for (a=math_start_i; a<user_input+math_start_i; a++)		{			for (b=math_start_j; b<user_input+math_start_j; b++)			{				printf("%d\t", math[a][b]);			}			printf("\n\n");		} 		printf("\nItem 3 again ? (yes = 1 / no = 0)\n");		scanf("%d", &again);		getchar(); //flush the keyboard buffer of the '\n' key	}	while (again);      	///******************* ITEM 4 *************/	again = 1; // reset for the next use 	/************** ITEM 4 **************/ 	printf("Item 4\n"); 	do	{  		/* Promt to user enter data */		printf("Enter  integers between -800 to 800:\n");		/* Loop for twice to fill our matrix */		for (i=0; i < M; i++)		{			sum = 0;			for (j=0; j < N; j++)			{				/* If user entered wrong number, keep him in the while until he enter right number */				do				{					/* Get what user entered */					scanf("%d", &user_input);					if (user_input >= -800 && user_input < 800)					{						/* Put number to the matrix */						math[i][j] = user_input;					}					else					{						printf("Wrong number entered! Try again!\n");					}						}				while(!(user_input >= -800 && user_input < 800));				sum += user_input;			}			avr[i] = sum/N;		} 		/* Put math to the screen */		printf("\n\n");		/* Loop our matrix */		for (i=0; i < M; i++)		{			/* Row by row printing */			for (j=0; j < N; j++)			{					printf("%d\t", math[i][j]);			}			printf("\n");		} 		printf("\n");		/* Loop for print evrage */		for (i=0; i < M; i++)		{			/* Row by row printing */			printf("%d\t", avr[i]); 		}		printf("\n\n\n"); 		/* Create copy of avr array */		for (i=0; i<M; i++)		{			avr_sorted[i]=avr[i];		} 		/* Bubble sorting "avr_sorted" array math */		for (i=0; i < M; i++)		{			for(j = M-1; j >= i; --j)			{			  /* compare elements */			  if(avr_sorted[j-1] > avr_sorted[j])			  {					/* exchange elements */					t = avr_sorted[j-1];					avr_sorted[j-1] = avr_sorted[j];					avr_sorted[j] = t;				}			}		} 		/* Printing math row by avrage, not very well solution, but works =)) */		/* Loop for "avr_sorted" array */		for (i=0; i < M; i++)		{			/* Loop for oroginal "avr" array */			for (z=0; z < M; z++)			{				/* If we found average from sorted array in original array, so 				print math row, that have index of math array (row) the same as index of avr array,				that have the same average in sorted array */				if (avr_sorted[i] == avr[z])				{					for (j=0; j < N; j++)					{						/* Row by row printing */						printf("%d\t", math[z][j]);					}					printf("\n");					break;				}			}		} 		printf("\nItem 4 again ? (yes = 1 / no = 0)\n");		scanf("%d", &again);		getchar(); //flush the keyboard buffer of the '\n' key	}	while (again);}`