当前位置:网站首页>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 :
- To support 10 A foot long fence actually requires two railings , One at each end .
- 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 .
- First, consider the Trident in the simplest case , Then extrapolate the results , This is principle one .
- 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 ”
边栏推荐
- Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
- Convert MySQL query timestamp to date format
- activity生命周期
- Bi SQL constraints
- vb学习什么[通俗易懂]
- 新一代可级联的以太网远程I/O数据采集模块
- Cobalt strike installation tutorial
- Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
- 腾讯完成全面上云 打造国内最大云原生实践
- The latest QQ wechat domain name anti red PHP program source code + forced jump to open
猜你喜欢
Bi-sql top
51 single chip microcomputer multi computer communication
15. several methods of thread synchronization
“一个优秀程序员可抵五个普通程序员!”
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
Text border format and text block of rich text
2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
AUTOCAD——两种延伸方式
丹麦技术大学首创将量子计算应用于能源系统潮流建模
Preliminary understanding of qtoolbutton
随机推荐
15.线程同步的几种方法
腾讯云WeCity丨你好 2022!
指南针炒股软件怎么样?安全吗?
程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?
腾讯完成全面上云 打造国内最大云原生实践
EVM Brief
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
php easywechat 和 小程序 实现 长久订阅消息推送
How to store dataframe data in pandas into MySQL
卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
丹麥技術大學首創將量子計算應用於能源系統潮流建模
Syntax highlighting of rich text
新手看过来,带你一次性了解“软考”
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Bi-sql - join
Preliminary understanding of qtoolbutton
Boutique enterprise class powerbi application pipeline deployment
Bi-sql select into
MySQL multi condition matching fuzzy query