+ 2

Need help with Array - description of the problem in the codw

https://code.sololearn.com/cs2fosY83RyC/?ref=app

18th May 2018, 6:57 PM
Daniel (kabura)
Daniel (kabura) - avatar
6 Answers
+ 3
when the third loop is execute third loop is executive five time with each value of i. As you see what you print in second loop System.out.print(buff [ i. ]); is i for first execution i=0 buff[0]=5; buff[1]=5; . . . . . buff[5]=5; note than-->> the last value where the second loop is terminate is 5 that why you see the output is 5,0,0,0,0,0 5,5,0,0,0,0 5,5,5,0,0,0 5,5,5,5,0,0 5,5,5,5,5,5
18th May 2018, 8:09 PM
Arun Tomar
Arun Tomar - avatar
+ 3
first loop of your code is for (int i = 0; i < buff.length; i++) { second loop is for (int j = 0; j < sequence.length; j++) { if ( i==j) { j++; } buff[i] = sequence[j]; } } first point--> first loop run for 5 time second loop run for 5 time for each value of first loop.. second point--> In second loop you increment the value of i if i and j are equal then after you assign the value of array buff[i] and your output like this first execution of loop i=0 j=0 . j=1 your output is-->>1, 2, 3, 4, 5 second execution i=1 j=0 your output 10 then after j=1 your output is 10,2,3,4 ,5 fifth execution i=5 j=0 when j=5 you output is 10,1,2,3,4, 5 ------------------------------------------------------------------- as you see the last digit where the second loop end is always five continue in second post
18th May 2018, 7:52 PM
Arun Tomar
Arun Tomar - avatar
+ 3
i=0 j=0 you print buff[i] after that you second loop terminate. at buff[0]=5; third loop come to play i=0 k=0 buff[k]=5
18th May 2018, 8:20 PM
Arun Tomar
Arun Tomar - avatar
+ 2
thank you for help, I already understand where I made the mistake I will try to correct the error tomorrow
18th May 2018, 8:24 PM
Daniel (kabura)
Daniel (kabura) - avatar
+ 1
works :) and how can you optimize this code? what to do in the future
19th May 2018, 11:18 AM
Daniel (kabura)
Daniel (kabura) - avatar
0
Can anyone please post the question
15th Aug 2021, 12:31 PM
Nana King Obedson
Nana King Obedson - avatar