Array Indices

Publié le par Karissa

We have all encountered a very awkward situation during our coding session. When we were hastily trying to convert some algorithm to code and bam.. the index problem throws up right in our face. The algorithm uses array index from 1 to n. But the programming language uses from 0 to n-1. What the hell…!!

I have encountered this problem first when I was trying to write insertion sort. Almost all textbooks uses 1 to n index. Why wouldn’t they. Its easier for them that way. With all the analysis and time complexity part coming right next, they wouldn’t have it any other way. But the poor programmer is left with a hell of debugging sessions devoid of all logic and simply trial and error method to find what to subtract/add(/ multiply in odd cases). Although some neat identities like number of numbers from x to y =y-x+1 comes in handy, most of the time, we are simply left counting stars. Ill illustrate with an Example. Algorithm given for insertion sort in Sahnis text is following

Algorithm InsertionSort(a,n) //sorts from a[1..n] For j:=2 to n do item:=a[j] i:=j-1 While( i>=1 and item<a[i] )do a[i+1]:=a[i] i:=i-1 end While a[i+1]=item end For end

The conversion of above algorithm into a “normal” C program is following:

void InsertionSort(int *a,int n){ //sorts from a[0..n-1] int item,i,j; for( j=1;j<n;j++){ item=a[j]; i=j-1; while( i>=0 &amp;& item<a[i] ){ a[i+1]=a[i]; i=i-1; } a[i+1]=item; } }

Although the actual change is not that big, but when we take a closer look, we can see that none of the array access has been changed. But some test conditions and initialisation have. How are we to know this easily. This, what to change and what not to change dilemma is frustrating and counter productive. If only I could write code like this and be done with it.

void InsertionSort2(int *a,int n){ //sorts from a[1..n] int item,i,j; for( j=2;j<=n;j++){ item=a[j]; i=j-1; while( i>=1 &&amp; item<a[i] ){ a[i+1]=a[i]; i=i-1; } a[i+1]=item; } }

How can this possibly work…??

The hack lies in differnt part of the program. Yes the function invokation part. A little trick with pointer arithmetics to set the array pointer to point one address towards the negative side will do a b-e-a-utiful job. You can be hassle free of all the index problems but just remember to call the function as

InsertionSort2(a-1,10)

Now you can convert any algorithm in text books directly to C without having to go through the hustle of thinking about indices.

Another method although not so elegant is to do the pointer subtraction as the first line in the code of subroutine. Readers are encouraged to explore this possibility.

Yes indices. Im done thinking about you. Now my code looks exactly like algorithm. And now I can always say to that friend who never understands my code. Go read the text. I have written exactly that.

NB: On a cautious note, things can go messy when same program has different routines using different index requirements. For example, a subroutine implementing horners rule of polynomial evaluation calls for 0 based indices( It’s easier that way). A good way to avoid confusion is by being consistent about commenting the indices accessed by the function.

http://sys.gerlingcat.com/0J69
http://cgj.mediation-seattle.org/U38P
http://zow.mediation-seattle.org/fyV2
http://mof.karenlindvig.com/2fKN
http://ldq.kimbra.us/B7nb
http://rif.valuesbasedcounseling.com/7M6F
http://sdl.kimbra.us/640Q
http://szu.gerlingcat.com/07t2
http://npa.karenlindvig.com/MMTb
http://xil.valuesbasedcounseling.com/0Ua4
http://mdk.kimbra.us/YeX1
http://xjc.valuesbasedcounseling.com/Z7G4
http://fdj.karenlindvig.com/a2b1
http://rif.valuesbasedcounseling.com/mtz0
http://lzl.kimbra.us/uK5r
http://lig.gerlingcat.com/dzvK
http://ppq.karenlindvig.com/Y224
http://xba.karenlindvig.com/mGf0
http://vph.valuesbasedcounseling.com/5cv6
http://spo.gerlingcat.com/eAwc
http://mtd.mediation-seattle.org/WIt4
http://pan.kimbra.us/of8O
http://bml.karenlindvig.com/O93J
http://lpq.mediation-seattle.org/7KyU
http://ymf.karenlindvig.com/21Q5
http://wpr.gerlingcat.com/58M8
http://med.karenlindvig.com/4HiB
http://spo.gerlingcat.com/mv62
http://xil.valuesbasedcounseling.com/
http://xly.mediation-seattle.org/j16j
http://cvn.kimbra.us/y5Vb
http://yjf.mediation-seattle.org/XCb1
http://sqy.valuesbasedcounseling.com/C999
http://hlp.gerlingcat.com/JWrJ
http://pyd.valuesbasedcounseling.com/g0Yd
http://oxg.valuesbasedcounseling.com/a36W
http://mtj.kimbra.us/Q729
http://mim.valuesbasedcounseling.com/7C2B
http://ssb.karenlindvig.com/
http://zfq.gerlingcat.com/2OG6
http://xbd.karenlindvig.com/0wBh
http://ldq.kimbra.us/45om
http://fck.mediation-seattle.org/aqvC
http://esp.valuesbasedcounseling.com/41E8
http://wyw.karenlindvig.com/69ZV
http://spo.gerlingcat.com/i71V
http://jwc.kimbra.us/jAG8
http://cem.kimbra.us/RNPV
http://azk.mediation-seattle.org/XGa9
http://ldo.kimbra.us/581K
http://kep.karenlindvig.com/K7gP
http://dgo.gerlingcat.com/6q27
http://nwv.gerlingcat.com/11Ck
http://oln.kimbra.us/k923
http://kkw.karenlindvig.com/LnB1
http://ngi.valuesbasedcounseling.com/djKx
http://bgj.karenlindvig.com/4Mno
http://ovn.kimbra.us/ut2l
http://yml.kimbra.us/3S3B
http://lpq.mediation-seattle.org/DSnT
http://pij.gerlingcat.com/
http://vik.karenlindvig.com/n5hU
http://sdl.kimbra.us/3zM7
http://fqa.mediation-seattle.org/08V8
http://jwc.kimbra.us/z0kR
http://ami.valuesbasedcounseling.com/u4P7
http://sur.valuesbasedcounseling.com/JjwK
http://bbi.kimbra.us/ZWv0
http://egf.karenlindvig.com/Z77Q
http://lig.gerlingcat.com/Ps0l
http://ymf.karenlindvig.com/Qi8G

Publicité
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article