+ 2
Can someone explain this code logic please
3 Antworten
+ 1
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if(arr[i]==arr[j])
cnt1++;
}
//this above inner for loop finds a array number how many times encountered for ex : 1 2 1 2
For this i=0 =>arr[0], cnt1= 2
In next iteration of i=1,=>arr[1], cnt1= 2
In next iteration of i=2,=>arr[2], cnt1= 2
In next iteration of i=3,=>arr[3], cnt1= 2
if(cnt1>=cnt){
if(arr[i]<maxval && cnt==cnt1){
cnt=cnt1;
This above if condition finds which cnt1 has >= for each iteration..
Initially cnt=0,maxval=0,
For i =0, arra[0]<maxval (1<0) && cnt1>=cnt, false so in else part cnt=cnt1=2, and maxval=arr[0];
Now cnt=2,maxval=1
For i =1, arra[1]<maxval (2<1) && cnt1>=cnt, false so cnt=cnt1, and maxval=arr[1]=2;
For i =2, arra[2]<maxval (1<2)&& cnt1>=cnt, true so cnt=cnt1=2, maxvalue, don't change..
For i =3, arra[3]>maxval (2>2) && cnt1>=cnt, true cnt=cnt1=2, maxvalue, don't change..
Finally maxval=2.
Hope it clarifies....
0
Is this not your code?
As it name says, it finding maximum repeated value and printing max value from repeated values...
Take a look at line by line...
Which statements or block you don't understand..?
0
No it is someone else code I am trying to understand it but I am not understanding these steps
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if(arr[i]==arr[j])
cnt1++;
}
if(cnt1>=cnt){
if(arr[i]<maxval && cnt==cnt1){
cnt=cnt1;