#include<iostream>
using namespace std;
void comparison(char arr2[], char arr3[],int& i,int j)
{
int abc=1;
for(j=0;j<5;j++)
{
if((arr2[j]==arr3[j])||(arr2[0]!=arr3[0]))
{
j++;
}

 i=i+1;
j=5;


}

}
int main()
{
char arr[5]={'a','b','c','d','e'};
char arr1[5]={'a','b','c','d','e'};
int refr=0, iteratorr=0;
comparison(arr,arr1,refr,iteratorr);
for(iteratorr=0;(iteratorr<5&&refr==1) ;iteratorr++)
{
cout<<arr1[iteratorr]<<endl;

}


return 0;
}

//Can anyone help me figure out what's happening to the values of i and j in the comparison function? Why the values of i and j are not being incremented in the comparison function? Or if they are being incremented, how many times and what values do i and j reach within comparsion function scope?

When in the loop:
I is incremented by 1
J might be incremented by 2, then it's set to 5

#include<iostream>
using namespace std;
void comparison(char arr2[], char arr3[],int& i,int j)
{
int abc=1;
for(j=0;j<5;j++)
{
if((arr2[j]==arr3[j])||(arr2[0]!=arr3[0]))
{
j++;
}

 i=i+1;
j=5;


}

}
int main()
{
char arr[5]={'a','b','c','d','e'};
char arr1[5]={'a','b','c','d','e'};
int refr=0, iteratorr=0;
comparison(arr,arr1,refr,iteratorr);
for(iteratorr=0;(iteratorr<5&&refr==1) ;iteratorr++)
{
cout<<arr1[iteratorr]<<endl;

}


return 0;
}

//Can anyone help me figure out what's happening to the values of i and j in the comparison function? Why the values of i and j are not being incremented in the comparison function? Or if they are being incremented, how many times and what values do i and j reach within comparsion function scope?

void comparison(char arr2[], char arr3[],int& i,int j)
       {
         int abc=1;
         for(j=0;j<5;j++)
         {
            if((arr2[j]==arr3[j])||(arr2[0]!=arr3[0]))
            {
              j++;
            }
     
            i=i+1;
            j=5;
         }
     
       }

Your for loop only runs one time because j is assigned the value of 5 and that ends the loop.

Can you explain in detail what's happening?Whats going on inside the loop in the function comparison please?

#include<iostream>
using namespace std;
void comparison(char arr2[], char arr3[],int& i,int j)
{
int abc=1;
for(j=0;j<5;j++)
{
if((arr2[j]==arr3[j])||(arr2[0]!=arr3[0]))
{
cout<<arr2[j];
j++;
}

 i=i+1;



}

}
int main()
{
char arr[5]={'a','b','c','d','e'};
char arr1[5]={'a','b','c','d','e'};
int refr=0, iteratorr=0;
comparison(arr,arr1,refr,iteratorr);
for(iteratorr=0;(iteratorr<5&&refr==1) ;iteratorr++)
{
cout<<arr1[iteratorr]<<endl;

}


return 0;
}

How come the output is ace in this case?

And how come the value of i doesn't exceed 1?

And how come the value of i doesn't exceed 1?

The value of i is 3 when the for loop finishes. The reason your output reads "a_c_e" is because you are incrementing j twice per loop so you are skipping the "b" and the "d":

for(j=0;j<5;j++)   // here j is incremented
{
       if((arr2[j]==arr3[j])||(arr2[0]!=arr3[0]))
       {
         cout<<arr2[j];
         j++;      // here j is incremented again for the same iteration
       }
     
       i=i+1;
     
}

oh lol that was such a silly mistake, i didn't realize i was incrementing it twice lol, thanks a lot for pointing it out, can i ask u something? I read that function can only return one value, or u can only make a function to calculate one value? Could you please possibly explain that with an example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.