For starters, you should:
- Change void main to int main.
- Indent your code properly.
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.