这里有什么问题? [英] What is the problem here?

查看:75
本文介绍了这里有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是编写一个函数来取一个数组并检查它是否已排序



我尝试了什么:



the question is to write a function in to take an array and check if it's sorted

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int check(int n,int a[]);
int main()
{
    int a[100],n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
    printf("%d",a[n]);
    check(int n,int a[]);
    if (check(int n,int a[]) = 1)
        {
            printf("the array isn't sorted");
        }`
    return 0;
}
int check(int n,int a[])
{
  int i,s;
  for(i=0;i<n;i++)
    {
        if(a[i] < a[i+1])
        {
            s = a[i];
            a[i] = a[i+1];
            a[i+1] = s;
            return 0;

    }
        else {
            return 1;
        }
    }
}

推荐答案

存在多个问题。其中一些应该已经由编译器指示。



请参阅注释:

There are multiple problems. Some of them should be already indicated by the compiler.

See the comments:
int main()
{
    int a[100],n,i;
    /* Hopefully the user know what to enter without guiding him. */
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    /* The element a[n] is not initialised: Garbage will be printed out */
    printf("%d",a[n]);
    /* C basics: How to call a function. The compiler should complain about this */
    check(int n,int a[]);
    /* As above.
       Using a value of 1 as not sorted is rather uncommon. 
    */
    if (check(int n,int a[]) = 1)
    {
        printf("the array isn't sorted");
    }
    return 0;
}




int check(int n,int a[])
{
    int i,s;
    for(i=0;i<n;i++)
    {
        /* Whith the last loop iteration (i == n-1) 
           this will access again the unitialised element a[n].
            So the loop should start at 1 when comparing with the previous item 
            or the end condition should be < n-1.
        */
        if(a[i] < a[i+1])
        {
            s = a[i];
            a[i] = a[i+1];
            a[i+1] = s;
            return 0;
        }
        else {
            return 1;
        }
    }
}



要求是检查数组是否已排序,但上面的代码将修改一个元素并返回任何元素小于下一个元素时为零。从我的角度来看,不应该修改数组。我也会想到被排序意味着所有元素都是按升序排列的。所以离开循环并返回未排序的条件是当任何元素大于下一个元素(或小于前一个元素)时。


The requirement is to check if the array is sorted but the above code will modify one element and return zero when any element is less than the next. From my point of view the array should not be modified. Also I would think of being sorted means that all elements are in ascending order. So the condition to leave the loop and return "not sorted" would be when any element is larger than the next (or smaller than the previous).


看看你的代码:我会删除无关紧要的问题。

Look at your code: I'll remove the irrelevent-to-your-problem bits.
int check(int n,int a[])
    {
    for(i=0;i<n;i++)
        {
        if(a[i] < a[i+1])
            {
            return 0;
            }
        else 
            {
            return 1;
            }
        }
    }

因此,如果 n 大于1,<$ c $如何? c>我永远变成2?



当你修复它时,它会失败,可能是由于你正在寻找分段错误比输入数组多一个元素:

So, if n is greater than 1, how does i ever get to be 2?

When you fix that, it will fail, probably with a segmentation error because you are looking at one more element than the input array holds:

if(a[i] < a[i+1])

但是 i 的范围从 0 n - 1 ,所以 i + 1 1 n 而你的阵列索引从 0 运行到 n - 1

But i ranges from 0 to n - 1, so i+1 is from 1 to n while your array indexes run from 0 to n - 1


要求您检查如果数组是否排序,则不会要求您对数组进行排序,那么为什么要交换数组中的项目?

You are asked to check if an array is sorted or not, you are not asked to sort the array, so why are you swapping items in the array ?
s = a[i];
a[i] = a[i+1];
a[i+1] = s;



如果你不理解你的代码在做什么或为什么它做了什么,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于这里有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆