获得分段错误。我已经尝试了很多,但我找不到错误。请帮我。 [英] Getting segmentation fault. I have tried so much but I could not find error. Please help me.

查看:68
本文介绍了获得分段错误。我已经尝试了很多,但我找不到错误。请帮我。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户将输入分数和坐标。答案应该是最小距离,它是最近一对点之间的距离。我还使用了时钟函数来获得总的经过时间..它的语法是否有问题?



我尝试过:



User will enter the number of points and the co ordinates . Answer should be the minimum distance which will be the distance between the closest pair of points.I have also used the clock function to obtain total elapsed time.. is there any problem in its syntax?

What I have tried:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<float.h>

// A structure to represent a Point in 2D plane
struct Point
{
    int x, y;
};
typedef struct Point Point;


// Needed to sort array of points according to X coordinate
int compareX(const void* a, const void* b)
{
    Point *p1 = (Point *)a,  *p2 = (Point *)b;
    return (p1->x - p2->x);
}
// Needed to sort array of points according to Y coordinate
int compareY(const void* a, const void* b)
{
    Point *p1 = (Point *)a,   *p2 = (Point *)b;
    return (p1->y - p2->y);
}


float dist(Point p1, Point p2)
{
    return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +
                 (p1.y - p2.y)*(p1.y - p2.y)
               );
}

float minm(float x, float y)
{
    return (x < y)? x : y;
}



float stripClosest(Point strip[], int n, float d)
{
    float minm = d;  // Initialize the minimum distance as d

  
    int i,j;
    for ( i = 0; i < n; ++i)
        for (j = i+1; j < n && (strip[j].y - strip[i].y) < minm; ++j)
            if (dist(strip[i],strip[j]) < minm)
                minm = dist(strip[i], strip[j]);

    return minm;
}


float closestUtil(Point Px[], Point Py[], int n)
{

    // Find the middle point
    int mid = n/2;
    Point midPoint = Px[mid];


    
    Point Pyl[mid+1];   // y sorted points on left of vertical line
    Point Pyr[n-mid-1];  // y sorted points on right of vertical line
    int li = 0, ri = 0,i;  // indexes of left and right subarrays
    for (i = 0; i < n; i++)
    {
      if (Py[i].x <= midPoint.x)
         Pyl[li++] = Py[i];
      else
         Pyr[ri++] = Py[i];
    }

   
    float dl = closestUtil(Px, Pyl, mid);
    float dr = closestUtil(Px + mid, Pyr, n-mid);

    
    float d = minm(dl, dr);

 
    Point strip[n];
    int j = 0;
    for ( i = 0; i < n; i++)
        if (abs(Py[i].x - midPoint.x) < d)
            strip[j] = Py[i], j++;

   
    return minm(d, stripClosest(strip, j, d) );
}


float closest(Point P[], int n)
{
    Point Px[n];
    Point Py[n];
    int i;
    for ( i = 0; i < n; i++)
    {
        Px[i] = P[i];
        Py[i] = P[i];
    }
    qsort(Px, n, sizeof(Point), compareX);
    qsort(Py, n, sizeof(Point), compareY);
    return closestUtil(Px, Py, n);
}
int main()
{
    clock_t start,last;
    double total;
    FILE *fp;
    int n,i;
    printf("Enter number of co ordinates:\n");
    scanf("%d",&n);
    Point P[n];
    printf("Enter the co ordinates:\n");
    for(i=0;i<n;i++)
        scanf("%d %d",&P[i].x,&P[i].y);
    start=clock();
    printf("Minimum distance is %f", closest(P, n));
    last=clock();
    total=(double)(last-start);
    printf("%f",total);
  
    return 0;
}

推荐答案

您是否尝试使用调试器运行此功能?这应该能够轻松缩小您在代码中出错的位置。



如何使用gdb在6个简单步骤中调试C程序 [ ^ ]
Have you tried running this with a debugger? That should be able to easily narrow where in the code you're going wrong.

How to Debug C Program using gdb in 6 Simple Steps[^]


Point P[n];



这不是动态数组的正确声明。

你需要使用指针和 malloc

你需要正确学习这门语言。

-----

以下是参考书的链接该语言的作者在C上。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]

-----

调试器是一个很好的工具来理解程序正在做什么



当你不'了解您的代码正在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看代码正在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

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


This is not a proper declaration of a dynamic array.
You need to use a pointer and malloc.
You need to learn properly the language.
-----
Here is links to references books on C by the authors of the language.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
-----
The debugger is a nice tool to understand what a program is doing

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. It 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[^]

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天全站免登陆