当前位置:网站首页>C language boundary calculation and asymmetric boundary

C language boundary calculation and asymmetric boundary

2022-06-25 01:17:00 Can't play the guitar

Railing error (“ Almost a mistake ”)

100 Foot long fence , every other 10 Feet requires a supporting rail , How many railings are needed ?

most “ Obvious ” The answer is 100/10, And what you get is 10, That's what we need 10 A railing . But this result is wrong , The answer is 11.
Way of thinking :

  1. To support 10 A foot long fence actually requires two railings , One at each end .
  2. Except for the right most railing , Every other paragraph 10 The foot long fence has only one railing on the left ; The exception is that the rightmost section of fence has not only a railing on the left , There is also a railing on the right .

So I have summed up two avoidance “ Railing error ” The general principles of .

  1. First, consider the Trident in the simplest case , Then extrapolate the results , This is principle one .
  2. Calculate the boundary carefully , Never take it lightly , This is principle two .

Each language sets the upper and lower bounds of the array

If there is an array that has 10 Elements , What is the allowable range of subscripts in this array ?

stay Fortran,PL/I as well as Snobol4 Programming language , The values in the following table of the array are from 1 Start , and Algol and Pascal Other languages , The following table of the array has no default starting value , The programmer must refer to the lower and upper bounds of each array . In standard Basic In language , Make a statement 10 Array of elements , The compiler actually allocates 11 Space of elements , Subscript range from 0-10.
But in C In language , The subscript range of this array is from 0 To 9. In one with 10 In an array of elements , There is a subscript of 0 The elements of , But there is no subscript 10 The elements of . stay C In language , A possession n Array of elements , There is also no subscript n The elements of .

So why C Language should define an array in a different way than ordinary people understand ?
Don't worry. , Let's start with an example of an asymmetric boundary

Asymmetric boundary and “ Railing error ”

Definition

“ Asymmetric boundary ” Yes, it will “ Lower bound upper bound problem ” To “ Entry and exit problems ”, Take the lower bound as “ Entry point ” Included in the value range , and “ Out of bounds point ” After the lower bound , Not included in the value range . Such as X>=16 And X<38, Entry point “16” Included in the value range , and “38” It is out of bounds , Not included in the value range .

Assume an integer x Satisfy the boundary conditions x >= 16 And x <=37, Then within this range x How many values are possible ?
let me put it another way , Sequence of integers 16,17,18,…,36,37 How many elements are there ? Obviously , The answer is to 37-16 Very close to , So the answer is 20,21 still 22?

According to principle 1 , We consider the simplest special case . Let's assume an integer x The range of phi is zero x >= 16 And x <= 17, Obviously, integers x There are two values for (17 - 16 + 1), Then push the special case outward , that x >= 16 And x <=37 Namely ,37 -16 + 1.
Then according to principle 2 , Calculate again , The result is really (37 -16 + 1).
cause “ Railing error ” The root of is “37 -16 + 1” Of “+1”


Is there a programming technique to reduce the occurrence of such errors ?
Yes , also C Language can avoid this problem

A numerical range is represented by the first in bound point and the first out bound point .

For example, the same value range , The just x >= 16 And x < 38, Then the result is 38 -16 了 , The result is the same 22.

Be careful : The lower bound here is “ Entry point ”, I.e. included in the value range ; And the upper bound is “ Out of bounds point ”, Is not included in the value range .

Although this kind of “ Asymmetry ” Maybe it's not very beautiful mathematically , Or not used to it , But its simplification of program design is enough to surprise people .
Such as C Language this setting array from 0 Start setting , The convenience of asymmetric boundary design is very obvious : The upper bound of this array ( first “ Out of bounds point ”) Just the number of array elements ! therefore , If you want to in C Define a 10 Array of elements ,0 Namely “ Entry point ”( Points within the array subscript range , Including boundary points ),10 Namely “ Out of bounds point ”( Points outside the array subscript range , Without boundary points ), The code implementation is as follows

int i = 0;
	int arr[10] = {
     0 };
	for (i = 0; i < 10; i++)
	{
    
		arr[i] = 0;
	}

Not in writing

int i = 0;
	int arr[10] = {
     0 };
	for (i = 0; i <= 9; i++)
	{
    
		arr[i] = 0;
	}

Let's go back to the beginning ,C Language setting array subscript from 0 It wasn't set up casually at first , But to simplify the process , Avoid the appearance of the program “ Railing problems ” Other questions

So ,C Language is called “ One of the most flexible languages is not without reason ”

原网站

版权声明
本文为[Can't play the guitar]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242037378222.html