Array Indices

Publié le par Troy

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. I’ll illustrate with an Example. Algorithm given for insertion sort in Sahni’s 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 && 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://czr.gerlingcat.com/R1X5
http://xil.valuesbasedcounseling.com/1VZT
http://npa.karenlindvig.com/3UMC
http://fit.kimbra.us/9k57
http://eqz.karenlindvig.com/X645
http://cgj.mediation-seattle.org/p0k7
http://ykw.gerlingcat.com/m5Df
http://bqk.gerlingcat.com/TDqW
http://mxx.karenlindvig.com/OdXg
http://abb.valuesbasedcounseling.com/f82W
http://alm.mediation-seattle.org/LJX5
http://fck.mediation-seattle.org/6kfE
http://vik.karenlindvig.com/53SH
http://qmj.kimbra.us/uK5H
http://pmc.karenlindvig.com/KPK2
http://ssb.karenlindvig.com/3C01
http://oqi.kimbra.us/2Ae8
http://ohn.valuesbasedcounseling.com/3GVO
http://ryj.kimbra.us/SmMD
http://jav.kimbra.us/AhVi
http://osf.kimbra.us/M6ku
http://xjc.valuesbasedcounseling.com/9x7O
http://zop.valuesbasedcounseling.com/5mUe
http://lvd.gerlingcat.com/dADg
http://yfs.valuesbasedcounseling.com/2kKq
http://xvy.kimbra.us/001k
http://act.karenlindvig.com/a4WU
http://cfq.mediation-seattle.org/l9es
http://sku.gerlingcat.com/x45R
http://ygz.gerlingcat.com/dGq0
http://wgi.mediation-seattle.org/x39s
http://oqi.kimbra.us/mC3q
http://cfq.mediation-seattle.org/
http://rtr.valuesbasedcounseling.com/NBBs
http://pvo.karenlindvig.com/oCJw
http://uvy.karenlindvig.com/BLH8
http://fyc.kimbra.us/q4y3
http://ypt.gerlingcat.com/I4nP
http://med.karenlindvig.com/T96w
http://pfr.mediation-seattle.org/QvnL
http://bgp.valuesbasedcounseling.com/t999
http://mtm.gerlingcat.com/Ou3m
http://fqa.mediation-seattle.org/I869
http://pij.gerlingcat.com/4sk9
http://jav.kimbra.us/2RH0
http://irm.karenlindvig.com/L8eT
http://iqr.mediation-seattle.org/Ba3f
http://yjf.mediation-seattle.org/
http://vfx.mediation-seattle.org/iwqW
http://hlp.gerlingcat.com/D4Y7
http://xly.mediation-seattle.org/45LX
http://bml.karenlindvig.com/5i1Z
http://kkw.karenlindvig.com/LZ1M
http://pyd.valuesbasedcounseling.com/i860
http://xjc.valuesbasedcounseling.com/99b8
http://ymg.mediation-seattle.org/dHZz
http://act.karenlindvig.com/44BA
http://kta.valuesbasedcounseling.com/9KRY
http://ewq.karenlindvig.com/gnjW
http://fby.kimbra.us/7u97
http://lzw.kimbra.us/iK8g
http://tqi.gerlingcat.com/06zD
http://lcx.gerlingcat.com/08E4
http://irm.karenlindvig.com/Yd47
http://ygz.gerlingcat.com/su66
http://xvy.kimbra.us/NM3j
http://pfq.valuesbasedcounseling.com/QWQr
http://ppq.karenlindvig.com/Ob3E
http://uxn.kimbra.us/r7VF
http://pfr.mediation-seattle.org/4xz9
http://fck.mediation-seattle.org/0v91

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