Reputation: 3
I have two char arrays (both [10][10]) and I want to compare each element in first array with the same element in the other one and return 1 if they are similar. Arrays:
char task [10][10] = {
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', t,' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', t, ' ', ' ',t, ' ', ' ', ' ' },
{ ' ', ' ', t, t, t, t,t, t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, t, t, t,t, t, ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
};
char solution [10][10] = {
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', t, t,' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', t, ' ', ' ',t, ' ', ' ', ' ' },
{ ' ', ' ', t, t, t, t,t, t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, ' ', ' ', ' ',' ', t, ' ', ' ' },
{ ' ', ' ', t, t, t, t,t, t, ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ' },
};
Where t: char t = -2; (filed square) I was trying sth like that:
bool cond (char task[10][10], char solution[10][10])
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (strcmp(task[i][j], solution[i][j]))
}
}
}
and in main:
do
{
XxX(zadanie);
wpisz(kolor,w1,w2,zadanie,t);
}
while (!cond(task,solution));
cout << "Solved";
but it didn't work, there is some problem with conversion from char to *char and I have no more idea how to fix it. Probably it's obvious for people who code a lot, but I'm not one of them..
Upvotes: 0
Views: 47
Reputation: 2138
@skarpet Your answer will overwrite result on every iteration. Try something like this:
bool warunek (char task[10][10], char solution[10][10])
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (task[i][j] != solution [i][j])
{
return false;
}
}
}
return true;
}
Additionally you dont have to iterate to the end, because it stops at the first missmatch.
Upvotes: 1
Reputation: 69882
Use standard algorithms wherever possible (which means always):
#include <algorithm>
#include <iterator>
bool matches_solution(char (&candidate)[10][10], char (&solution)[10][10])
{
auto row_same = [](char (&l)[10], char (&r)[10])
{
return std::equal(std::begin(l), std::end(l),
std::begin(r));
};
return std::equal(std::begin(candidate), std::end(candidate),
std::begin(solution), row_same);
}
Upvotes: 0
Reputation: 3
@GoodDeeds @GoodDeeds Right, I pasted wrong code:
bool warunek (char task[10][10], char solution[10][10])
{
int result = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (task[i][j] == solution [i][j])
{
result= 0;
} else
{
result= 1;
}
}
}
return result;
}
Is that correct (I mean the code, not the logic, is it working as intended)?
Upvotes: 0