C language implements calculation of the difference between the maximum value and the minimum value of each column of a two-dimensional array (5 basic examples of C language - two-dimensional array)

Questions 1-3

C Language Basic Example Questions 1-3 - Pointers

Preface

The title of this article is basic exercises of C, two-dimensional array part. Before doing these exercises, you need to make sure you have learned about two-dimensional arrays.

topic

Question 4

Write a program to define a two-dimensional integer array with a size of 3 rows and 4 columns. Enter 12 integers from the keyboard and store them into an array.

Write a function that accepts a two-dimensional array and the number of rows in the array as parameters, calculates and returns the sum of all elements in the array.

Call this function in the main function to calculate and print the sum of all elements in the array.
Insert image description here

test case

When running this program, you can enter the following test data for verification:

Input:
1 2 3 4 5 6 7 8 9 10 11 12
Output:
2D The sum of all elements of the array is: 78

Question 5

Write a program to define a two-dimensional integer array with a size of 4 rows and 5 columns. Enter 20 integers from the keyboard and store them into an array.

Write a function that accepts a two-dimensional array and the number of rows in the array as parameters, calculates and returns the difference between the maximum and minimum values ​​of each column element in the array.

Call this function in the main function to calculate and print the difference between the maximum and minimum values ​​of the elements in each column.

Insert image description here

test case

When running this program, you can enter the following test data for verification:

Input:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15< /span> The difference between the maximum value and the minimum value of the element in column 1 is: 15< /span> The difference between the maximum value and the minimum value of the element in column 3 is: 15< /span> The difference between the maximum value and the minimum value of the element in column 5 is: 15< /span> The difference between the maximum value and the minimum value of the element in column 4 is: 15 The difference between the maximum value and the minimum value of the element in column 2 is: 15Output:
16 17 18 19 20





Input:
-1 -2 -3 -4 -5
-6 -7 -8 -9 -10< a i=3> -11 -12 -13 -14 -15 -16 -17 -18 -19 -20Output: The difference between the maximum value and the minimum value of the element in column 1 is: 15 The difference between the maximum value and the minimum value of the element in column 2 is: 15 The difference between the maximum value and the minimum value of the element in the 3rd column is: 15 The difference between the maximum value and the minimum value of the element in the 4th column is: 15 The difference between the maximum value and the minimum value of the element in column 5 is: 15







Input:
9 5 7 2 1
6 8 3 4 10
11 15 12 13 14< /span> The difference between the maximum value and the minimum value of the element in column 1 is: 10< /span> The difference between the maximum value and the minimum value of the element in column 3 is: 14< /span> The difference between the maximum value and the minimum value of the element in column 5 is: 18< /span> The difference between the maximum value and the minimum value of the element in column 4 is: 18 The difference between the maximum value and the minimum value of the element in column 2 is: 13Output:
16 18 17 20 19





Reference answer

Question 4

#include<stdio.h>
#define ROW 3
#define COLUMN 4
int calculateSum(int arr[][COLUMN],int row);
int main(void)
{
    
    
    int arr[ROW][COLUMN];
    for(int i=0;i<ROW;i++)
    {
    
    
        for(int j=0;j<COLUMN;j++)
        {
    
    
            scanf("%d",&arr[i][j]);
        }
    }
    printf("二维数组所有元素的和为: %d\n",calculateSum(arr,ROW));

    getchar();
    getchar();
    return 0;
}
int calculateSum(int arr[][COLUMN],int row)
{
    
    
    int sum=0;
    for(int i=0;i<row;i++)
    {
    
    
        for(int j=0;j<COLUMN;j++)
        {
    
    
            sum+=arr[i][j];
        }
    }
    return sum;
}

parse

First, a constant ROW is defined to represent the number of rows of the two-dimensional array, and a constant COLUMN is defined to represent the number of columns of the two-dimensional array. Next, in the main function, the elements of the two-dimensional array input by the user are read one by one through the nested for loop and stored in the arr array. Then, call the calculateSum function to calculate the sum of all elements of the two-dimensional array and print the result.

In the calculateSum function, first define a local variable sum to save the calculation results. Then, each element of the 2D array is iterated through a nested for loop and accumulated into sum. Finally, the calculated result sum is returned.

Question 5

Answer 1

#include<stdio.h>
#include<stdlib.h>
#define ROW 4
#define COLUMN 5
int* calculateColumnDiff(int arr[][COLUMN],int row);
int main(void)
{
    
    
    int arr[ROW][COLUMN];
    int *difference;
    for(int i=0;i<ROW;i++)
    {
    
    
        for(int j=0;j<COLUMN;j++)
        {
    
    
            scanf("%d",&arr[i][j]);
        }
    }
    difference=calculateColumnDiff(arr,ROW);
    for(int i=0;i<COLUMN;i++)
    {
    
    
        printf("第%d列元素的最大值和最小值的差值为: %d\n",i+1,difference[i]);
    }

    getchar();
    getchar();
    return 0;
}
int* calculateColumnDiff(int arr[][COLUMN],int row)
{
    
    
    int max,min;
    int *difference=(int*)malloc(COLUMN*sizeof(int));
    for(int c=0;c<COLUMN;c++)
    {
    
    
        max=min=arr[0][c];
        for(int r=1;r<row;r++)
        {
    
    
            if(arr[r][c]>max)
                max=arr[r][c];
            else if(arr[r][c]<min)
                min=arr[r][c];
        }
        difference[c]=max-min;
    }
    return difference;
}
parse

First, a constant ROW is defined to represent the number of rows of the two-dimensional array, and a constant COLUMN is defined to represent the number of columns of the two-dimensional array.

In the main function, a pointer to an integer, difference, is declared to save the result of the difference between the maximum value and the minimum value of each column. Then, the elements of the two-dimensional array input by the user are read one by one through the nested for loop and stored in the arr array. Next, call the calculateColumnDiff function to calculate the difference of each column and assign the returned result to difference.

In the calculateColumnDiff function, local variables max and min are first defined to represent the maximum and minimum values ​​of the current column respectively. Then, use the malloc function to allocate a one-dimensional array with a size of COLUMN*sizeof(int) through dynamic memory allocation, and assign its address to the pointer difference. Then, use a two-level for loop to traverse the elements of each column and update the values ​​of max and min. Finally, calculate the difference max-min and save the result in the corresponding position of the difference array.

Finally, in the main function, use a for loop to print out the difference results for each column.

Answer 2

#include <stdio.h>

int calculateColumnDiff(int arr[][5], int rows)
{
    
    
    int diffArr[5];

    for (int j = 0; j < 5; j++)
    {
    
    
        int max = arr[0][j];
        int min = arr[0][j];

        for (int i = 1; i < rows; i++)
        {
    
    
            if (arr[i][j] > max)
            {
    
    
                max = arr[i][j];
            }

            if (arr[i][j] < min)
            {
    
    
                min = arr[i][j];
            }
        }

        diffArr[j] = max - min;
    }

    for (int i = 0; i < 5; i++)
    {
    
    
        printf("第%d列元素的最大值和最小值的差值为: %d\n", i + 1, diffArr[i]);
    }
}

int main()
{
    
    
    int arr[4][5];

    printf("请输入20个整数:\n");
    for (int i = 0; i < 4; i++)
    {
    
    
        for (int j = 0; j < 5; j++)
        {
    
    
            scanf("%d", &arr[i][j]);
        }
    }

    calculateColumnDiff(arr, 4);
    getchar();
    getchar();
    return 0;
}
parse

The main idea is to use two nested loops to iterate through the array and find the maximum and minimum values ​​in each column, then calculate the difference and store the result in a one-dimensional array diffArr.

In the main function, a two-dimensional array arr is first defined to store the integer entered by the user, and the integer entered by the user is read through two nested for loops. Then call the calculateColumnDiff function to calculate the difference for each column.

In the calculateColumnDiff function, a one-dimensional array diffArr is first defined to store the difference value of each column. Then, in each column, initialize the max and min values ​​to the first element of the current column. Iterate through each element of the two-dimensional array through two nested loops, updating the values ​​of max and min. Finally, the difference max-min of each column is stored in the corresponding position of the diffArr array, and the difference result of each column is printed out through a loop.

Comparing the two answers, the first answer is more in line with the meaning of the question, but considering that some readers have not learned dynamic memory allocation at this time, the second answer is also acceptable.

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/134006351