Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
bool equivalent (int s[], int n[], int c){

for (int p=0 ; p<c; p++){
for (int i=0; i<c; i++){
if (s[i]==n[i+p%c])
return true;

}
}
return false;
}




#include <iostream>
using namespace std;
int main (){

int A [5];
int B[5];
for (int i=0; i<5; i++){
cout << "FOR ARRAY A enter 5 numbers ";
cin>> A[i];
}
for (int i=0; i<5; i++){
cout << "For ARRAY B enter 5 numbers ";
cin>> B[i];
}

if (equivalent( A, B, 5))
cout<< "This is correct";
else{
cout<< "This is not right";
}

return 0;
}

What I have tried:

ok the thing is every time i put different set of arrays it runs through the if statement one time. what i mean is that once if statement is true one time it returns true. however, i want the if statement to run n times then return me true or false.
Posted
Updated 31-Aug-17 9:45am

1 solution

Quote:
i want the if statement to run n times then return me true or false.

Use a 'for' loop just like you did for user input.

I don't know what you try to do but in this line of code,
C++
if (s[i]==n[i+p%c])

every time i+p%c is more than 4, you are outside of array n.

Use the debugger to see what your code is doing.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900