开发者

Function that searches for difference between members of an array

I need to write a function that will return true if it has found a difference between members of an array.

My code is:

int func1(int  *str)
{
    int i;
    for(i=0;i<*(str+i);i++) {
        if(*(str+i) == *(str+i+1))
        {
            return 1;
        }
    }
    return 0;
}

I have to implement it with pointers.

The code above does not work(logically).

Can anybody help?

UPDATE:

I have changed my code to the following:

int func1(int  *str)
{
    int i,temp=0;
    for(i=0;i<10-1;i++) {
        if(*(str+i) == *(str+i+1))
        {
            temp++;
            if( temp == 10 )
            {
                return 1;
            }
        }
    }开发者_JAVA百科
    return 0;
}

What is the problem with the new code?


This looks like homework to me, so I don't want to spoil the fun but one thing about C I'd like to mention: having a pointer to some array doesn't tell you anything about the size of the array. So your function will need to take a pointer and a second size_t argument (or maybe a pointer to the last element of the array).


Your function only takes in a single array pointer, that seems like one too few for a comparison.

You must add an argument that specifies the lengths of the arrays, or implement some kind of "policy" that e.g. terminates the arrays using a specific value.

You should also look into using the standard memcmp() function.


I don't understand the question (It's unclear what you're trying to achieve)...

As others have already said, there's no boundary checking on your array, which is wrong...

Here's some other feedback on your code:

// func1 - consider giving functions a meaningful name, it helps people to 
// understand what the function is supposed to be doing....
// In this instance, it might have been helpful to identify what the expected
// return values / inputs of the function are...
int func1(int  *str)
{
    int i;

    // Start a counter at 0, loop (adding 1) while 
    // the current value of the counter is less than, the value held in the 
    // array so, {1,2,3,4,0,7} Would terminate on the 0
    // This: {1,20,7,14,0,7} Would also terminate on the 0
    // This seems wrong, but again, it's unclear what you're trying to do here.
    for(i=0;i<*(str+i);i++) {

        // If the current element of the array
        // is the same as the next element of the array
        if(*(str+i) == *(str+i+1))
        {
            // return 1  - two numbers next to each other in the  
            //             array are the same?
            return 1;
        }
    }

    // Either:  The array contained a digit less than the counter,
    // Or: It didn't contain two numbers that were the same next to each other.
    // This seems a bit wrong?!?
    return 0;
}

Your question could be improved (to get a more useful answer), if you showed what inputs you were expecting to return what return values.

Based on this 'I will need to write a function that will return true if its found diffrence between members of array.'

In pseudo code, it seems like you would want:

// Loop, checking we don't overflow.  No point checking the last element as
// there's nothing after it to check...
for (count = 0 to arraysize -1) {
    // If the current element != the next element, we've found a difference?!?
    if(arrayElement[count] != arrayElement[count+1) {
        return true
    }
}
return false

UPDATE:

In your new code...

// You're still assuming the size of 'str'
int func1(int  *str) 
{     
    int i,temp=0;
    // Loop while i < 9, i.e. 9 times.
    for(i=0;i<10-1;i++) {         
        if(*(str+i) == *(str+i+1))         
        {
            temp++;             
            // Temp can never == 10, you're only going round the loop 9 times...
            // Maybe it should be (temp == 10-1), but I don't know where the 
            // 10 comes from...
            if( temp == 10 )
            {
                return 1;             
            }
        }     
    }     
    return 0; 
} 

This:

if(*(str+i) == *(str+i+1))         
{
    temp++;             
    // Temp can never == 10, you're only going round the loop 9 times...
    if( temp == 10 )
    {
        return 1;             
    }
}     

Could be:

// return 0 (FALSE) on first difference
if(*(str+i) != *(str+i+1))         
{
    return 0;             
}     

If you changed the return 0 at the end of your function to return 1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜