programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# array doubt

Ranch Hand
Posts: 77
• Number of slices to send:
Optional 'thank-you' note:
This is a mock exam question. The answer given is: 0 0 3 0
Could some one please explain how we will get 3 in arr[2].

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
Hello Cathymala,

Earlier I had the same doubt in this question but I got this explanation from Vishwanath which cleared my doubt and hope will clear yours also:

for the first value in the array:
arr[1]<-- 0
so arr becomes,
{1,0,3,4}

for the next value in the array: [Remb: The array is changed now]

arr[0]<-- 0;
and arr is now,
{0,0,3,4}

for the next value in the array:

arr[3]<--0
gives
{0,0,3,0}

and for the last value in the array:
arr[0]<--0
so, arr remains {0,0,3,0}

Regards,
Lata

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
I could not understand this explanation, why do you start with arr[1] when its actually goes arr[0], arr[1], arr[2] etc.. and why does it remain 3 only in the 3rd position.

Akshay

Sheriff
Posts: 13411
• Number of slices to send:
Optional 'thank-you' note:
The zeroth element in the array has a value of 1 which is what gets assigned to i in the first iteration.

arr[i] equals array[1].

Akshay Dashrath
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Ben,

Is'nt it like this now, does'nt an array always start from the zeroeth element?

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4

Thanks,

Akshay

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
hi,

the critical thing to notice here is that

'i' is used to hold the value in the array
and also to index through the array.

Hence,

when we go step by step into the for loop, its as follows:
arr = {1,2,3,4}

loop(1)
i = 1 , arr[1] = 0 // i takes first value of the array arr
arr = {1,0,3,4}

loop(2)
i = 0, arr[0] = 0 // i takes second value of the array arr
arr = {0,0,3,4}

loop(3)
i = 3 , arr[3] = 0 // i takes third value of the array arr
arr = {0,0,3,0}

loop(4)
i = 0 , arr[0] = 0 // i takes fourth value of the array arr
arr = {0,0,3,0}

confusing...
but observe things step by step and it will be clear.

cathymala louis
Ranch Hand
Posts: 77
• Number of slices to send:
Optional 'thank-you' note:
Thank you, I got it.

 "To do good, you actually have to do something." -- Yvon Chouinard Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat