1. ## compara array

given array A (1,2,3,4), array B(2,3,4,5)
compare 2 array.find how many % match
when i run only can get 100 % instead of 75%. any wrong of my code?
output should be Only 75 percent are matched.Please try again. but still cannot get

my code
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[4] = {1, 2, 3, 4};
int b[4] = {2, 3, 4, 5};
int r, counter = 0, percentage = 0;

for (r = 0; r < 4; r++)
{
if (a[r] == b[r]);
{
counter = counter + 1;
}
percentage = (((float)counter / 4) * 100);
}

{
if (percentage == 100)
{
printf("all match");
}
else
{
printf("Only %d percent are matched.Please try again.", percentage);
}

}
return 0;
}```

2. percentage is an int
Code:
`percentage=(((float)counter/4)*100);`

3. You should get a 0% match since you compare the same indexes in the both arrays. Your if statement is followed by ';' so it's empty, meaning that what follows will always execute regardless if it's a match or not. Also, your for loop count from 0 to 3.

Edit: don't mind the loop counter, I just noticed that you use a separate counter.

4. For starters, you should:
• Change void main to int main.

Thus:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[4] = {1, 2, 3, 4};
int b[4] = {2, 3, 4, 5};
int r, counter = 0, percentage = 0;

for (r = 0; r < 4; r++)
{
if (a[r] == b[r])
;
counter = counter + 1;
percentage = (((float)counter / 4) * 100);
}

{
if (percentage == 100)
printf("all match");
else
printf("Only %d percent are matched.Please try again.", percentage);
}
return 0;
}```
Notice that you have an if statement whose body does nothing. Then, you increment counter on each iteration, thus counter eventually ends up as 4. Notice also that you only needed to compute the percentage after the loop. Furthermore, you created a code block with those braces, but you did not need that code block anyway.

One thing that can help you here is if you always use braces for if statements, e.g.,
Code:
```if (a[r] == b[r])
{
counter = counter + 1;
}```
and:
Code:
```if (percentage == 100)
{
printf("all match");
}
else
{
printf("Only %d percent are matched.Please try again.", percentage);
}```
EDIT:
Originally Posted by Subsonics
Also, your for loop count from 0 to 3.
That is not a problem. What might be a potential problem is that the magic number 4 is used, but that is okay for now.

some{} problem ..

6. Err... you received three helpful replies. If you don't understand, then you should elaborate on what exactly is it that you don't understand.

7. Originally Posted by laserlight
That is not a problem. What might be a potential problem is that the magic number 4 is used, but that is okay for now.
I somehow assumed that the loop counter was used in the division, but later discovered that was a separate variable.