part A ) : consider the following : what is the output for the following code an
ID: 3550178 • Letter: P
Question
part A ) : consider the following :
what is the output for the following code and what is the reason . please explain it step by step to better understand.
bool equivalent(int a[],intb[],int n){
for( int i=0; i< n ; i++ ){
if ( a[i] !=b[(i+3)%n] return false;
return true;
}
int main(){
inta[5]={ 1,2,3,4,5} , b[5]={1 ,2,3,4,5,};
cout<< equivalent( a,b,5)
return0;
}
B) :what is the output of the following code and please explain it step by step on how did you get the output:
bool equivalent(inta[], intb[] ,int n){
int shift;
for(int i =0 ; i< n; i++){
if( a[i] ! + b[( i+shift)% n])
return false;
}
return true;
}
int main(){
int a[5]= {1,2 3,4 ,5} , b[5]= { 1 , 4 , 5 , 1 , 2}:;
cout<< equivalent( a ,b ,5 );
return 0;
}
Explanation / Answer
part A ) : consider the following :
what is the output for the following code and what is the reason . please explain it step by step to better understand.
bool equivalent(int a[],int b[],int n){
for( int i=0; i< n ; i++ ){
if ( a[i] !=b[(i+3)%n]) return false; // if a[0] = 1 and b[3%5] = b[3] = 1
return true;
}
}
// at i = 0
// if a[0] = 1 and b[3%5] = b[3] = 1 since both are same it will go to next loop.
// at i = 1
// a[1] = 2 and b[4%5] = b[4] = 2 since both are same it will go to next loop.
// at i = 2
// a[2] = 3 and b[5%5] = b[0] = 1 since 3!=1 it returns false here and returns from function
int main()
{
int a[5]={ 1,2,3,4,5} , b[5]={1 ,2,3,4,5,};
cout<< equivalent( a,b,5);
return 0;
}
B) :what is the output of the following code and please explain it step by step on how did you get the output:
bool equivalent(int a[], int b[] ,int n){
int shift;
for(int i =0 ; i< n; i++)
{
if( a[i] != b[( i+shift)% n])
return false;
}
return true;
}
// at i =0
// a[0] = 1 and b[0%5] = b[0] = 1 since both are same it will go to next loop
// at i = 1
// a[1] = 2 and b[1%5] = b[1] = 4 since 4!=2 it returns false here and returns from function
int main(){
int a[5]= {1,2, 3,4 ,5} , b[5]= { 1 , 4 , 5 , 1 , 2};
cout<< equivalent( a ,b ,5 );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.