Login Register


Index of array start at 0 problem filter_list
Author
Message
Index of array start at 0 problem #1
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 #2
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 #3
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 #4
(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 #5
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 #6
I think @Deque can help you with this
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG


RE: Index of array start at 0 problem #7
(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 #8
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].
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


RE: Index of array start at 0 problem #9
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.








Users browsing this thread: