Пример #1
0
int main()/*begin function main*/
{
   /*vaiables used in the main function*/
   int num;
   int large;
   int row;
   int col;
   int i;
   int x;
   int close;

   printf("\nEnter the size of the 2D  array: ");/*prompt user for the size of the array to be initialized*/
   scanf("%d", &num);

   while (check_error(num) == 0)/*check that a valid size was entered*/
   {
      printf("Invalid input enter the size of the array again: ");
      scanf("%d", &num);
   }

   srand(time(NULL));/*randomize the function reand()*/

   initialize_2Darray(num);/*initialize the 2D array*/

   print_2Darray(num);/*display the 2D array*/

   initialize_1Darray(num);/*initialize the 1D array*/

   print_1Darray(num);/*display the 1D array*/

   printf("\n\nEnter the row: ");/*prompt the user to enter a row number*/
   scanf("%d", &row);

   printf("Enter the col: ");/*prompt the user to enter a column number*/
   scanf("%d", &col);   

   large = search_max(row, col, num);/*find the largest number in the row and column entered by the user*/

   printf("\nThe largest number present in the row %d and col %d is %d\n", row, col, large);/*display the largest value found in the coulumn*/

   for (i = 0; i < num; i++)/*display the diagonal value and how many times it is present in that row and column*/
   {
      x = count_diagonal(i, num);   

      printf("\nDiagonal element %d is present %d more times in the row %d and col %d", array2D[i][i], x, i, i);
   }
  
   close = closest_row(num);/*assign the number of the closest row to the variable closest*/
   
   printf("\n\nRow closest to the array is row %d\n\n", close);/*display the row of the 2D array cloasest to the 1D array*/
}
Пример #2
0
//Program Begin
int main(void)
{
	//Seed Random Generator
	srand(time(NULL));

	//Local Variables
	int mainSize, mainOption;
	int userRow, userColumn;
	int countMatchResult;
	int main2Darray[MAX][MAX];
	int main1Darray[MAX];

	//Receive and check user-selected size
	printf("\nEnter the size: ");
	scanf("%d", &mainSize);
	while(check_size(mainSize) == 0)
	{
		printf("Invalid input enter the size of the array again: ");
		scanf("%d", &mainSize);
	}

	do //Perform functions until user selects 6 (exit)
	{
		//Display menu, take and check user option
		display_menu();
		scanf("%d", &mainOption);
		while(check_option(mainOption) == 0)
		{
			printf("Invalid input: Enter the operation you want to perform: ");
			scanf("%d", &mainOption);
		}

		//Perform function based on user choice
		switch(mainOption)
		{
			case 1:
				// Perform Search Min Function
				initialize_2Darray(main2Darray, mainSize);

				//Display Arrays
				printf("\nSearch Min Operation\n\n");
				printf("2D array\n");
				print_2Darray(main2Darray, mainSize);

				//Get user row and column choice
				printf("\nEnter the row: ");
				scanf("%d", &userRow);
				printf("Enter the col: ");
				scanf("%d", &userColumn);

				//Perform function and display result
				printf( "\n\nThe smallest number present in the "
						"row %d and col %d is ", userRow, userColumn);
				printf("%d", search_min(main2Darray, userRow, userColumn, mainSize));

				break;

			case 2:
				// Perform Count Matches Function
				initialize_2Darray(main2Darray, mainSize);
				initialize_1Darray(main1Darray, mainSize);

				//Display initialized arrays
				printf("\nCount Match Operation\n\n");
				printf("2D array\n");
				print_2Darray(main2Darray, mainSize);
				printf("\n1D array\n");
				print_1Darray(main1Darray, mainSize);

				//Get user row choice
				printf("\n\nEnter the row: ");
				scanf("%d", &userRow);

				//Perform function and display result
				countMatchResult = count_match(main2Darray, main1Darray, mainSize, userRow);
				if(countMatchResult == 0)
					printf("\nThere are no numbers from 1D array present in the 2D array");
				else
				{
					printf( "\nThere are %d numbers from the 1D array present in the 2D array",
							countMatchResult);
				}

				break;

			case 3:
				// Perform Closest Row Function
				initialize_2Darray(main2Darray, mainSize);
				initialize_1Darray(main1Darray, mainSize);

				//Display initialized arrays
				printf("\nClosest Row Operation\n\n");
				printf("2D array\n");
				print_2Darray(main2Darray, mainSize);
				printf("\n1D array\n");
				print_1Darray(main1Darray, mainSize);

				//Perform function and display result
				printf( "\n\nRow closest to the 1D array is row %d",
						closest_row(main2Darray, main1Darray, mainSize));
				break;

			case 4:
				// Perform Sort 1D Array Function
				initialize_1Darray(main1Darray, mainSize);

				//Display initialized array
				printf("\nSort 1D Array Operation\n\n");
				printf("1D Array before sorting\n");
				print_1Darray(main1Darray, mainSize);

				//Display sorted array
				printf("\n1D Array after sorting\n");
				sort_1Darray(main1Darray, mainSize);
				print_1Darray(main1Darray, mainSize);

				break;

			case 5:
				// Perform Sort 2D Array Function
				initialize_2Darray(main2Darray, mainSize);

				//Display initialized array
				printf("\nSort 2D Array Operation\n\n");
				printf("2D Array before sorting\n");
				print_2Darray(main2Darray, mainSize);

				//Display sorted array
				printf("\n2D Array after sorting\n");
				sort_2Darray(main2Darray, mainSize);
				print_2Darray(main2Darray, mainSize);

				break;

			case 6:
				// Exit Program

				break;

			default:
				// Invalid input - Error
				printf( "\n"
						"*********************************\n\n"
						" Error: Invalid Switch Condition \n\n"
						"*********************************\n");
		}

	} while(mainOption != 6);

	return 0;
}