C language classic algorithm example 1: find the maximum and minimum of a two-dimensional array

insert image description here

1. Problem description

Find the maximum and minimum values ​​of a two-dimensional array
The description of the problem is as
follows

  1. From a two-dimensional integer array of n rows and n columns, select two numbers as follows.
  2. First select a large number from each row, and then select a decimal from the selected n large numbers;
  3. Next, select the decimals from each row, and then select the larger number from the n decimals selected.

2. Algorithm instance compilation environment

The compilation environment of the C language classic algorithm example in this article uses the integrated development environment: Visual Studio 2019
insert image description here
insert image description here

Visual Studio 2019 official website link is as follows

Visual Studio 2019 official website link
insert image description here

The features of the Visual Studio 2019 integrated development environment are:

    1. Visual Studio 2019 installs the Live Share code collaboration service by default.
    1. New welcome window to help users write code quickly, improved search functionality, general performance improvements.
    1. Visual Studio IntelliCode AI Help.
    1. Better Python virtual and Conda support.
    1. And support for .NET Core 3.0 projects including WinForms and WPF, etc.

Third, the algorithm example implementation process

3.1, include header files

Include the header file code as follows


#include <stdio.h>
#include <stdlib.h>
  • The C language header files that will be used are included in recent years.

3.2. Defining macros and declaring arrays

The code for defining the macro and declaring the array is as follows

#define MAXN 20
int a[MAXN][MAXN];
  • MAXN is defined, representing that MAXN is a constant of 20.
  • Array a is declared.

3.3, declare related variables

The code for declaring the relevant variables is as follows

	int min, max;
    int row, col, n;
  • Declare related variables min, max, row, col, n;

3.3, the order of the input array (square matrix)

The order code of the input array (square matrix) is shown below

	printf("Please input the order of the matrix:");/* 输入方阵的阶次 */
    scanf("%d", &n);

    printf("\nPlease input the elements of the matrix,\nfrom a[0][0] to a[%d][%d]:\n", n - 1, n - 1);
    for (row = 0; row < n; row++)
    {
    
    
        for (col = 0; col < n; col++)
        {
    
    
            scanf("%d", &a[row][col]);
        }
    }
  • Enter the order of the array (square matrix) according to the text prompt.
  • Enter data into the array according to the text prompts.
    insert image description here

3.4, output "input array"

The output "array of input" code looks like this

 	printf("\nThe original matrix is\n");
    for (int row = 0; row < n; row++)
    {
    
    
        for (int col = 0; col < n; col++)
        {
    
    
            printf("%d ", a[row][col]);
        }
        printf("\n");
    }
    printf("\n");
  • Output the data we put into the array.

Press F5 to compile, and the debugging results are as follows.

insert image description here

  • The data we input into the array can be output correctly.

3.5. Calculate the smallest number in the largest data in each row

Calculate the smallest number in the largest data in each row. The code is as follows

	for (min = a[0][0], row = 0; row < n; row++)
    {
    
    
        for (max = a[row][0], col = 1; col < n; col++) /*从 row 行选出大数 */
        {
    
    
            if (max < a[row][col])
            {
    
    
                max = a[row][col];
            }
        }

        if (row == 0)       /* 保存至 row 行的小数 */
        {
    
    
            min = max;
        }
        else if (min > max)
        {
    
    
            min = max;
        }
    }
    printf("The minimum of maximum number is %d\n", min);
  • First select the largest number from each row
  • Then select the decimal from the selected n large numbers;

Press F5 to compile, and the debugging results are as follows.

3.5.1. Result of second-order array debugging

insert image description here

3.5.2. Result of third-order array debugging

insert image description here

3.6. Calculate the largest number in the smallest data in each row

Calculate the largest number in the smallest data in each row. The code is as follows

 	for (max = a[0][0], row = 0; row < n; row++)
    {
    
    
        for (min = a[row][0], col = 1; col < n; col++) /* 从 row 行选出小数 */
        {
    
    
            if (min > a[row][col])
            {
    
    
                min = a[row][col];
            }
        }

        if (row == 0)       /*保存至 row 行的大数 */
        {
    
    
            max = min;
        }
        else if (max < min)
        {
    
    
            max = min;
        }
    }

    printf("\nThe maximum of minimum numbers is %d\n", max);
  • First select the decimals from each row,
  • Then select the larger number from the selected n decimal numbers.

Press F5 to compile, and the debugging results are as follows.

3.6.1. Result of second-order array debugging

insert image description here

3.6.2. Result of third-order array debugging

insert image description here

Fourth, the complete code of the classic algorithm example program

The complete code of the classic algorithm example program is as follows

4.1, main.h file

#pragma once


#include <stdio.h>
#include <stdlib.h>


4.2, main.c file

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"

#define MAXN 20
int a[MAXN][MAXN];

int main()
{
    
    
	system("color 3E");

    int min, max;
    int row, col, n;

    printf("Please input the order of the matrix:");/* 输入方阵的阶次 */
    scanf("%d", &n);

    printf("\nPlease input the elements of the matrix,\nfrom a[0][0] to a[%d][%d]:\n", n - 1, n - 1);
    for (row = 0; row < n; row++)
    {
    
    
        for (col = 0; col < n; col++)
        {
    
    
            scanf("%d", &a[row][col]);
        }
    }

    printf("\nThe original matrix is\n");
    for (int row = 0; row < n; row++)
    {
    
    
        for (int col = 0; col < n; col++)
        {
    
    
            printf("%d ", a[row][col]);
        }
        printf("\n");
    }
    printf("\n");

    for (min = a[0][0], row = 0; row < n; row++)
    {
    
    
        for (max = a[row][0], col = 1; col < n; col++) /*从 row 行选出大数 */
        {
    
    
            if (max < a[row][col])
            {
    
    
                max = a[row][col];
            }
        }

        if (row == 0)       /* 保存至 row 行的小数 */
        {
    
    
            min = max;
        }
        else if (min > max)
        {
    
    
            min = max;
        }
    }
    printf("The minimum of maximum number is %d\n", min);

    for (max = a[0][0], row = 0; row < n; row++)
    {
    
    
        for (min = a[row][0], col = 1; col < n; col++) /* 从 row 行选出小数 */
        {
    
    
            if (min > a[row][col])
            {
    
    
                min = a[row][col];
            }
        }

        if (row == 0)       /*保存至 row 行的大数 */
        {
    
    
            max = min;
        }
        else if (max < min)
        {
    
    
            max = min;
        }
    }

    printf("\nThe maximum of minimum numbers is %d\n", max);

	system("pause");
	return 0;
}



V. Summary

An example of a classic algorithm in C language in this article: to find the maximum and minimum values ​​of a two-dimensional array, the goals to be achieved are as follows

  1. From a two-dimensional integer array of n rows and n columns, select two numbers as follows.
  2. First select a large number from each row, and then select a decimal from the selected n large numbers;
  3. Next, select the decimals from each row, and then select the larger number from the n decimals selected.

insert image description here

The article ends here.
I hope that the example of a classic algorithm in C language in this article: finding the maximum and minimum values ​​of a two-dimensional array can inspire your love for C language and algorithm learning.

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/126645724