Home > front end >  what is wrong with my if condition? it doesn't return the right value. in this case it should r
what is wrong with my if condition? it doesn't return the right value. in this case it should r

Time:02-20

The function should return 'T' if each element in the first array is equal to it corresponding element in the second array, and return 'F' if there is any difference between two array.

#include <stdio.h>

char match_array(int a[10], int b[10]);

int main()
{    
    int a1[10] = {1,2,3,4,5,6,7,8,9,10};
    int a2[10] = {0,0,0,4,5,6,7,8,9,10};
    char match;

    match = match_array(a1, a2);    
    printf("%c", match);
    
    return 0;
}

char match_array(int a[10], int b[10])
{
    int i;

    for(i=0; i<10; i  ){
        if(a[i] == b[i]){
            return 'T';
        }
    }
    return 'F';
}

CodePudding user response:

Check your code for match_array once again clearly.

char match_array(int a[10], int b[10])
{
    int i;

    for(i=0; i<10; i  ){
        if(a[i] == b[i]){
            return 'T';
        }
    }
    return 'F';
}

Here whenever two corresponding elements of array are equal, you are returning T. Which means, return true when 'any' two corresponding elements are equal. I guess, this is not you intended. To return true when 'all' corresponding elements are equal, you should modify your code to this:

char match_array(int a[10], int b[10])
{
    int i;

    for(i=0; i<10; i  ){
        if(a[i] != b[i]){
            return 'F';
        }
    }
    return 'T';
}

Here whenever, two corresponding elements are unequal, you would return false. You should return true only when you have iterated over entire array, and found that no elements are unequal.

CodePudding user response:

Your run your loop

for(i=0; i<10; i  ){
        if(a[i] == b[i]){
            return 'T';
        }
    }
    return 'F';

Which goes through the arrays and check each pair. It will return T when (and if) it'll reach a pair that is equal, which means in our case: a[3] & b[3] == 4

It will return true and that's it. it won't reach the end of the loop because return statement stops the function and returns the return value.

The only way it will return F in this case is there is no equal pair in the arrays, which isn't the case here.

To simplify it: Your algorithm here is:

check if a[i] == b[i]:
if yes - stop function return true
if no - continue loop and check a[i 1] == b[i i]

if reached here -  return F.

CodePudding user response:

For starters the function should be declared like

char match_array( const int a[], const int b[], size_t n );

That is the function should be able to deal with arrays of various sizes.

And its parameters that denote arrays shall have the qualifier const because passed arrays are not changed within the function.

Your function returns 'T' as soon as there are found two elements of the arrays equal each other though all other elements can be unequal each other.

for(i=0; i<10; i  ){
    if(a[i] == b[i]){
        return 'T';
    }
}

The function can be defined the following way

char match_array( const int a[], const int b[], size_t n )
{
    size_t i = 0;

    while ( i < n && a[i] == b[i] )   i;

    return i == n ? 'T' : 'F';
}

And the function is called like

match = match_array( a1, a2, 10 );
  • Related