से पहले सेगमेंटेशन फॉल्ट इसलिए मैं एक समस्या में भाग रहा हूं जहां किसी भी तरह से मेरा कोड वास्तव में किसी भी मुख्य भाग से पहले सेगमेंटेशन दोष पैदा कर रहा है। मैंने पहले कभी ऐसा नहीं किया था और मेरे पास शायद ही कभी चौथाई का कोडिंग अनुभव है, इसलिए मुझे यकीन नहीं है कि कुछ ऐसा है जो मैं गलत कर रहा हूं। सब कुछ मेरे कंप्यूटर पर कम से कम संकलित करता है, लेकिन इसे चलाने पर मेरा मुख्य कभी नहीं पहुंचता है।मुख्य
संदर्भ: मैं एक आसन्न मैट्रिक्स में वर्टिसेस और एज को जोड़ने की कोशिश कर रहा हूं और फिर एमएसटी बनाने के लिए प्राइम के एल्गोरिदम का उपयोग कर रहा हूं, लेकिन यह बाद में है। मैंने एक हेडर फ़ाइल बनाई, जिसमें मूल रूप से संरचनाओं और कार्यों के लिए केवल typdef कॉल शामिल थे। हालांकि, मैंने संरचना परिभाषाओं को हेडर फ़ाइल में बदल दिया क्योंकि मुझे स्मृति त्रुटियां मिल रही थीं; इसलिए मुझे लगता है कि structs के साथ कोई मुद्दा है।
graph.h:
//Leland Wong 00000897031
//graph header file
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#ifndef GRAPH_H
#define GRAPH_H
typedef struct vertex
{
double longitude;
double latitude;
char city[30];
int index;
int visited; //0: not visited, 1: visited, 2: visited
struct edge* nexte;
struct vertex* nextv;
double projected;
}VERTEX;
typedef struct edge
{
struct vertex* start;
struct vertex* destination;
double distance;
struct edge* nexte;
}EDGE;
typedef struct graph
{
struct vertex* list[756];
struct edge* matrix[756][756];
}GRAPH;
/*
typedef struct vertex VERTEX;
typedef struct edge EDGE;
typedef struct graph GRAPH;
*/
double findDistance(VERTEX* v1, VERTEX* v2); //compute the distance between two locations
EDGE* connect(VERTEX* v1, VERTEX* v2); //connects two vertices and returns the connecting EDGE
GRAPH primMatrix(GRAPH *g); //connects all vertices using Prim's Algorithm in an adjacency matrix
//void lPrimConnect(VERTEX v); //connects all vertices using Prim's Algorithm in an adjacency list
EDGE* findSmallestEdge(VERTEX v, GRAPH *g); //finds the smallest EDGE connected to v
#endif
graph.c: मेरे सभी कार्यों
//functions
//computes the distance between v1 and v2
double findDistance(VERTEX* v1, VERTEX* v2)
{
printf("findDistance");
double long1 = v1->longitude;
double long2 = v2->longitude;
double lat1 = v1->latitude;
double lat2 = v2->latitude;
double distance = 0;
if(long1 < 0)
long1 += 360;
if(long2 < 0)
long2 += 360;
distance = powf((long1-long2), 2) + powf((lat1 - lat2), 2);
distance = sqrt(distance);
return distance;
}
//creates and returns an edge that connects v1 and v2
EDGE* connect(VERTEX* v1, VERTEX* v2)
{
printf("connect");
EDGE *new;
new->start = v1;
new->destination = v2;
new->distance = findDistance(v1, v2);
return new;
}
//finds smallest edge connected to v in GRAPH g
EDGE* findSmallestEdge(VERTEX v, GRAPH *g)
{
printf("findSmallestEdge");
EDGE *tempe;
int i, index;
index = v.index;
//set tempe equal to the first edge connected to v
tempe = g->matrix[index][0];
//find smallest edge connected to v
for(i = 0; i < 756; i++)
{
if(g->matrix[index][i] -> distance < tempe->distance && g->list[index]->visited == 0)
{
tempe = g->matrix[index][i];
}
}
return tempe;
}
//creates an MST out of GRAPH g using Prim's algorithm
GRAPH primMatrix(GRAPH *g)
{
printf("primMatrix");
GRAPH new; // = malloc(sizeof(GRAPH));
EDGE *smallest;
EDGE *tempe;
int i, x;
i = 1;
x = 0;
new.list[0] = g->list[0]; //add root node to MST
g->list[0]->visited = 2;
smallest = findSmallestEdge(*new.list[0], g);
new.matrix[0][smallest->destination->index] = smallest;
//MST will contain all 756 nodes, so run this 755 times to ensure all nodes are reached
while(i < 756)
{
x = 0;
smallest = findSmallestEdge(*new.list[i], g);
//i = number of vertices already reached
while(x < i)
{
tempe = findSmallestEdge(*new.list[x], g);
if(tempe -> distance < smallest -> distance)
{
smallest = tempe;
}
x++;
}
new.list[i] = smallest -> destination;
smallest -> destination -> visited = 2;
new.matrix[smallest->start->index][smallest->destination->index] = smallest;
i++;
}
return new;
}
graphmatrixmain.c के कार्यान्वयन में शामिल हैं: अपने मुख्य समारोह जो रेखांकन बनाता
#include "graph.h"
int main(int argc, char* argv[])
{
FILE *fp;
static GRAPH g;
char buffer[200];
int i, j;
char city[30];
char *long1;
char *lat1;
if(argc == 1)
{
printf("could not open file\n");
return 0;
}
else
fp = fopen(argv[1], "r");
//read in line of data from txt file, build a new vertex, and insert into list
while(fgets(buffer, 200, fp) != NULL)
{
VERTEX *new = malloc(sizeof(VERTEX));
printf("%s", buffer);
sscanf(buffer, "%s %s %s", city, long1, lat1);
//sscanf(buffer, "%[^\t]\t%[^\t]\t%s", city, long1, lat1);
printf("scanned in data\n");
new->longitude = atof(long1);
new->latitude = atof(lat1);
new->index = i;
g.list[i] = new;
printf("%s: (%lf, %lf)", new->city, new->longitude, new->latitude);
i++;
}
//create EDGE and make connects between every VERTEX in list
for(i = 0; i < 756; i++)
{
for(j = 0; j < 756; j++)
{
g.matrix[i][j] = connect(g.list[i], g.list[j]);
if(j == 0)
{
g.list[i]->nexte = g.matrix[i][j];
}
}
}
return 0;
}
यदि आवश्यक हो, तो यह वह फ़ाइल है जिसमें से मैं पढ़ रहा हूं: cities.txt यह con मानना 756 प्रविष्टियों कुल लेकिन जहां तक कोड के रूप में है चिंतित आकार प्रासंगिक नहीं होने चाहिए
Shanghai 121.47 31.23
Bombay 72.82 18.96
Karachi 67.01 24.86
Buenos Aires -58.37 -34.61
Delhi 77.21 28.67
Istanbul 29 41.1
Manila 120.97 14.62
Sao Paulo -46.63 -23.53
Moscow 37.62 55.75
कृपया सभी चेतावनियों और डीबगिंग जानकारी (उदा। 'Gcc -Wall -g') के साथ संकलित करें। जब तक कोई चेतावनी नहीं दी जाती तब तक स्रोत में सुधार करें। फिर, ** एक डीबगर ** का उपयोग करें। और अपने ऑपरेटिंग सिस्टम, आपके कंपाइलर, संकलन कमांड, आदि के बारे में और बताएं ... –
@dasblinkenlight वास्तव में इसे पूरी तरह उत्तर दिया। कोड वर्तमान में किसी भी त्रुटि के बिना संकलित करता है - मैं बस अपने sscanf कथन के साथ एक seg गलती में चल रहा हूँ। मैंने कोड को अपने वर्तमान स्थिति में अपडेट किया है। अब मुझे एहसास है कि यह मेरे delimiters – aperson231