搜索数组成员之间差异的函数

发布于 2024-11-09 07:04:22 字数 640 浏览 0 评论 0原文

我需要编写一个函数,如果发现数组成员之间存在差异,该函数将返回 true。

我的代码是:

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

我必须用指针来实现它。

上面的代码不起作用(逻辑上)。

有人可以帮忙吗?

更新:

我已将代码更改为以下内容:

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;
            }
        }
    }
    return 0;
}

新代码有什么问题?

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;
            }
        }
    }
    return 0;
}

What is the problem with the new code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-11-16 07:04:22

这对我来说就像是家庭作业,所以我不想破坏乐趣,但我想提一下关于 C 的一件事:指向某个数组的指针并不能告诉您有关数组大小的任何信息。因此,您的函数需要采用一个指针和第二个 size_t 参数(或者可能是指向数组最后一个元素的指针)。

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).

隐诗 2024-11-16 07:04:22

您的函数仅接受一个数组指针,这对于比较来说似乎太少了。

您必须添加一个指定数组长度的参数,或者实现某种“策略”,例如使用特定值终止数组。

您还应该考虑使用标准 memcmp() 函数。

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.

脱离于你 2024-11-16 07:04:22

我不明白这个问题(不清楚你想要实现什么)...

正如其他人已经说过的,你的数组没有边界检查,这是错误的...

这是关于你的代码的一些其他反馈:

// 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;
}

你的如果您显示了您期望返回什么返回值,则可以改进问题(以获得更有用的答案)。

基于此“我需要编写一个函数,如果发现数组成员之间存在差异,该函数将返回 true”。

在伪代码中,您似乎想要:

// 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

更新:

在您的新代码中...

// 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; 
} 

这:

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;             
    }
}     

可能是:

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

如果您将函数末尾的 return 0 更改为 return 1

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文