C language: details in programming

The following details were discovered when individuals completed several programming questions:

This sharing is still divided into two parts:

  1. Code display
  2. Code analysis

Example 1:

Regarding the 6 courses in a two-dimensional array, find the student ID corresponding to the average grade of the student;
code display:

#include<stdio.h>

#define NOT_FIND -1
#define TOTAL_STU 8

float aver(int (*pStu)[7], int stuNo);

int main(void)
{
    
    
	int student[TOTAL_STU][7];
	float averScore;
	int i, j, stuNumber;

	printf("Input the %d student's number and score: \n", TOTAL_STU);

for (i = 0; i < TOTAL_STU; i++)
{
    
    
	for (j = 0; j < 7; j++)
	{
    
    
		scanf("%d", &student[i][j]);
	}
}


	printf("Input a student's number to compute: ");
	scanf("%d", &stuNumber);

	averScore = aver(student, stuNumber);
	printf("Output:");
	if (NOT_FIND == averScore)
	{
    
    
		printf("\nstudent of No.%d not Find!\n", stuNumber);
	}
	else
	{
    
    
		printf("\nThe No.%d student's average is %.2f\n", stuNumber, averScore);
	}

	return 0;
}

float aver(int pStu[TOTAL_STU][7], int stuNo)
{
    
    
	int i, j;
	double sum = 0;
	for (i = 0; i < TOTAL_STU; i++)
	{
    
    
		if (pStu[i][0] == stuNo)
		{
    
    
			for (j = 1; j < 7; j++)
			{
    
    
				sum += pStu[i][j];
			}
			return (float)(sum/6);
		}
	}
	return NOT_FIND;
}

Code analysis:

#define NOT_FIND -1
#define TOTAL_STU 8
This part is a macro definition.

* float aver(int ( pStu)[7], int stuNo); The function of this part of the sub-function is to check whether the input student ID exists, and if it exists, calculate the average of its scores.

This programming question is not difficult, but it is difficult to complete.
the reason:

  1. The defined array name and other variable names are too cumbersome and easy to make mistakes.
  2. To calculate the value, you should remember the (float)(sum/6) coercion conversion, and pay attention to the data type as double when defining the variable (float is defined every time, it’s not bad, but it’s not good). Use brackets and data type to enforce The conversion will be very easy.

Example 2:

Find the sum of the elements on the primary and secondary diagonals of any m×m matrix (Note: each element is counted only once)

Code display:

#include <stdio.h>

int main(void)
{
    
    
	int i, j, m, sum = 0;
	int array[20][20];

	printf("Please input m:");
	scanf("%d", &m);

	printf("\nPlease input array:\n");
	for (i = 0; i < m; i++)
	{
    
    
		for (j = 0; j < m; j++)
		{
    
    
			scanf("%d", &array[i][j]);
		}
	}

	for (i = 0; i < m; i++)
	{
    
    
		sum = sum + array[i][i] + array[i][m - i - 1];
	}
	if (m % 2 == 1)
	{
    
    
		sum = sum - array[m / 2][m /2];
	}
	printf("Output:\nsum=%d\n", sum);
	return 0;
}

Code analysis

The core of this code is in this part:

for (i = 0; i < m; i++)
	{
    
    
		sum = sum + array[i][i] + array[i][m - i - 1];
	}
	if (m % 2 == 1)
	{
    
    
		sum = sum - array[m / 2][m /2];
	}
	printf("Output:\nsum=%d\n", sum);

This part is the calculation of the sum of main and auxiliary diagonal elements;

among them:

for (i = 0; i < m; i++)
	{
    
    
		sum = sum + array[i][i] + array[i][m - i - 1];
	}

Array[i][i] is the calculation of the sum of the main diagonal elements. This algorithm is not difficult to think of.
Secondly, the calculation of the sum of the sub-diagonal elements was not thought of when I first got it.
In fact, this can be found by linking the sum of the input number m with the number of rows i.
It is not difficult to find that for this kind of algorithm for finding rules, in addition to focusing on the relationship between ranks and columns, it should also be noted that the array is a subscript starting from 0, and secondly, it is thought that the number of inputs specified at the beginning is m.

In addition, don't be too nervous about the exams you are studying at this stage. Fortunately, I passed it with my strength.

Guess you like

Origin blog.csdn.net/yooppa/article/details/112371461
Recommended