Sinisterly
Index of array start at 0 problem - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: Index of array start at 0 problem (/Thread-Index-of-array-start-at-0-problem)



Index of array start at 0 problem - Gintoki Ameto - 05-26-2014

Due to my country education system, I had used Pascal for programming all the time. After changing to other language, everything work like a charm but one exception (it's been following me from years):
The array in Python, C, Java,... always start its index at 0.
In Pascal, you can design where the index should start, and in many case, I used to declare it at 1.
For example: the problem require finding the shortest path from City[1->5], the array is prototype as City[1..n] = [1,2,3,4,5]. In this case, it became pretty easy to imply the algorithm because the value i is also the value of City[i] (0<i<6).
In other language, since index of array start at 0, I must declare the array City[0->5] = [0,1,2,3,4,5] and abandon City[0] through all the program, City[0] became garbage and cost system memory.
Another way is prototype City[0->4] = [1,2,3,4,5]. This won't cost any garbage memory, but it is more difficult to imply the algorithm since City[i] = i+1.
The question is, which way is better to use? Which way will you choose? Is there any other way to solve this problem?


RE: Index of array start at 0 problem - The Real Slim Shady - 05-26-2014

i dont understand your problem. Why cant you start an array at 0? why are you not using/ignoring [0]? people coding in C/C++/Java/PHP/Python/Perl/etc/etc/etc dont seem to have this issue?


RE: Index of array start at 0 problem - Gintoki Ameto - 05-26-2014

Because I want the City[1..5] = [1,2,3,4,5]
In C: City[6] = [0,1,2,3,4,5]
And ignore the City[0]


RE: Index of array start at 0 problem - The Real Slim Shady - 05-26-2014

(05-26-2014, 01:26 PM)Gintoki Ameto Wrote: Because I want the City[1..5] = [1,2,3,4,5]
In C: City[6] = [0,1,2,3,4,5]
And ignore the City[0]

But thats my point... why does it matter if it is City[0..4] or City[1..5]? You are storing the exact same values??

Seems to me you are just being picky


RE: Index of array start at 0 problem - Gintoki Ameto - 05-26-2014

Because the problem need it to be like that hn..
It looks like I'm picky? hn..


RE: Index of array start at 0 problem - Ex094 - 05-26-2014

I think @Deque can help you with this


RE: Index of array start at 0 problem - The Real Slim Shady - 05-26-2014

(05-26-2014, 03:10 PM)Ex094 Wrote: I think @Deque can help you with this

Im not sure anyone can help him. Maybe I am missing something... but

Code:
i = 0; while(i<5) { print City[i]; }

Will print

Quote:City[0]
City[1]
City[2]
City[3]
City[4]

Sounds to me like he just wants to start with 1. He's probably declaring i=1 because thats what he's done in pascal for years, and is now getting confused when the arrays are forcing him to start with 0? thats the only reason I can think of for him to suggest that City[i] = i+1??


RE: Index of array start at 0 problem - alok9shm - 05-26-2014

Look at what you said: " since index of array start at 0, I must declare the array City[0->5] = [0,1,2,3,4,5] and abandon City[0] through all the program, City[0] became garbage and cost system memory."

Why should you abandon city[0]?

Keep the first city in City[0], second one in City[1], third one in city[2]...fifth in City[4].
[Image: Untitled.jpg]

Check this C code:
Code:
int a[5]={2,3,6,4,5}; for(i=0;i<=4;i++) // or for(i=0;i<5;i++) printf("%d ",a[i]);
That would print out 2 3 6 4 5
Any problem with that?
Since you are starting from position 0, you can end it at position 4.

[table]
[row]
[cell]a[0][/cell]
[cell]a[1][/cell]
[cell]a[2][/cell]
[cell]a[3][/cell]
[cell]a[4][/cell]
[/row]
[row]
[cell] 2[/cell]
[cell] 3[/cell]
[cell] 6[/cell]
[cell] 4[/cell]
[cell] 5[/cell]
[/row]
[/table]
So, start from 0 and end at 4, you get your 5 cities without wasting any blank cell in the array. Smile

If you start from a[1], you got to end at a[n], but if you start from a[0], you got to end at a[n-1], where n is the number of cities.
You should start from a[0] always instead of a[1].


RE: Index of array start at 0 problem - Gintoki Ameto - 05-27-2014

I really did it for years and... Since my country's education is sticking with it, most of the problem is designed with index = 1 too.
So basically, I only have to do one thing: Changing the problem so that index start with 0.
Thank you all for helping me with this problem, I now shall close this thread.
Best Regard.