Hello,
I'm working on a litle program. While working I stumbeled upon some problems. I would like to check if two Arrays are equal to each other.
I tried this one, but it didn't work properly.

int control(int x1,int y1,int x2, int y2)//x1,y1 and x2,y2 -> coordinates of a location in array 
{
     int a,x,y = 0;
     char safer[3][3]; 
     char oldsafer[3][3];
     
     for(y=0;y<=2;y++) //setting all the values of array safer to zero.
     {
     for(x=0;x<=2;x++)
     {
     safer[x][y]=0;
     }
     }

     if(((x1||y1||x2||y2)<=2)&&((x1||y1||x2||y2)>=0)) //the coordinates have to be between 0 and 2,
      { if (safer[x1][y1] == safer[x2][y2])//the coordinates should not be equalto each other
           {   if (oldsafer == safer) //if arrays are equal oldsafer = safer  
              oldsafer = safer;
              else // if any of the statements above is wrong a would become 0;
              a=0;
              }   
        else  
        a=0;  
       }
     else 
     a=0;
     return(a); //returning a, if the function returns 0 an error message is going to apear.
}

I hope you understand my problem. otherwhise ask me.

Recommended Answers

All 5 Replies

if(((x1||y1||x2||y2)<=2)&&((x1||y1||x2||y2)>=0))

If x1, y1, x2 and y2 are all zero, this says:

if (FALSE <= 2 && FALSE >=0)

If any are not zero, it says

if (TRUE <=2 && TRUE >=0),

I doubt that's what you meant.

Read this http://www.cplusplus.com/doc/tutorial/operators/

Ok, thank's. But there's a thing I'm still unsure about. Is it possible to use the equal sign = on arrays like you can use it on variables. If yes how do I write it correctly?

int a,b;
a = b;
//is the same thing possible with arrays?
char c[2][2];
char d[3][3];
 c = d;
// or can you compare two arrays to get a true or false statement?

if ( c == d)
// or if I want to know if two signs in an array are equal:
if ( c[1][2] == d[1][2])
int a,b;
a = b;//bad practice
char c[2][2];
char d[3][3];
c = d;

Well?Why don't you try that in your compiler? the answer is no. you cannot do that with arrays.

// or can you compare two arrays to get a true or false statement?
if ( c == d)

Though the compiler doesn't report any error in this case, you'll not get the intended result from it.Because here, the addresses of arrays c and d are being compared for equality which is never true.

// or if I want to know if two signs in an array are equal:
if ( c[1][2] == d[1][2])

This is the correct form.

Is it possible to use the equal sign = on arrays like you can use it on variables. If yes how do I write it correctly?

Suppose you have an array

int array[5]

then you can assign values to them like this

array[0]=1;//1st address assigned to 1
int i;//will be used as offset address
for(i=1;i<5;i++)
    array[i]=i+1;//2nd address contains 2, 3rd address contains 3 etc.

Ok, thank you. I might find a way to compare two arrays by using a for loop and use this: if ( c[1][2] == d[1][2]) somehow., something like:

int ok = 0;
for (x=0;x<=2;x++)
 {
 y=0;
 y++;
  if(c[x][y] ==d[x][y])
  ok = 1;
}
int ok = 0;
for (x=0;x<=2;x++)
 {
 y=0;
 y++;
  if(c[x][y] ==d[x][y])
  ok = 1;
}

It only compares
c[0][1] with d[0][1]
c[1][1] with d[1][1]
c[2][1] with d[2,1] and so on.
If You want to compare each of the elements then, put it in a nested loop

for(x=0;x<2;x++)
    for(y=0;y<2;y++)
        if(c[x][y]==d[x][y])
        //rest of the code...
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.