- 1
Why the occ[4] not icrement or increase
5 ответов
+ 1
In you code, instead loop write this and see what happening...
for(int i=0; i<n.length; i++)
{
System.out.println("number "+ n[i] + " is in "+(++occ[n[i]])+ "times ");
//occ[n[i]];
}
Edit :
Rankush
int n[]={1,2,5,1,2,3,4,2,4,1};
Initially all values in occ array are 0.
Next by this ++occ[ n[i] ], the values changing..
I =0, n[0] = 1, then ++occ[1] causing occ[1] to 1 from 0.
I =1, n[1] = 2, then ++occ[2] causing occ[2] to 1 from 0.
I =2, n[2] = 5, then ++occ[5] causing occ[5] to 1 from 0.
I =3, n[3] = 1, then ++occ[1] causing occ[1] to 2 from 1.
I =4, n[4] = 2, then ++occ[2] causing occ[2] to 2 from 1.
I =5, n[5] = 3, then ++occ[3] causing occ[3] to 1 from 0.
I =6, n[0] = 4, then ++occ[4] causing occ[4] to 1 from 0.
I =7, n[0] = 2, then ++occ[2] causing occ[2] to 3 from 2.
I =0, n[0] = 4, then ++occ[4] causing occ[4] to 2 from 1.
I =0, n[0] = 1, then ++occ[1] causing occ[1] to 3 from 2.
0
The program finding how many times a numbers is in from array n and that is telling by occ array.
There occ[4] telling that 2 times number is in array n. Not increamenting n[4]...
0
Thanks for answering me
0
I updated above, see again, if you want more detail.
Rankush you're welcome..
- 1
Please understand me briefly