2014-10-10 61 views
0

任務:我的工作應該排序與侏儒排序字符串(設置在rosettacode.com碼爲哪些)陣列字符串和侏儒的排序

問題的陣列的程序:編譯所述代碼之後,報道沒有錯誤,但是當我執行程序,在無序數組版畫,我也得到「分段錯誤(核心轉儲)」錯誤:

____(6:46PM)[[email protected]]:_____ ~> ./a.out

Unsorted: state 0: California state 1: Oregon state 2: Washington state 3: Texas zsh: segmentation fault (core dumped) ./a.out

我想不出什麼問題。絕對的排序功能出了問題,只是無法弄清楚究竟是什麼。

源代碼

#include<stdio.h> 
#include<string.h> 


void 
gnome_sort (char *a[], int n) 
{ 
    int i = 1, j = 2; 
    char temp[20]; 


//#define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; } 
    while (i < n) 
    { 
     if (strcmp (a[n - 1], a[n]) > 0) 
    { 
     //swap(i - 1, i); 
     strcpy (temp, a[i]); 
     strcpy (a[i], a[i - 1]); 
     strcpy (a[i - 1], temp); 
     if (--i) 
     continue; 
    } 

     i = j; 
     j++; 



    } 
//# undef swap 
} 


int 
main() 
{ 

    char *states[] = { "California", "Oregon", "Washington", "Texas" }; 
    int num_states = 4; 

// int n; 

//n=sizeof a/sizeof a[0]; 

//printf("n is %d ",n); 

    int i; 
    printf ("\n Unsorted:\n"); 

    for (i = 0; i < num_states; i++) 
    { 
     printf ("state %d: %s\n", i, states[i]); 
    } 

    gnome_sort (states,num_states); 

    printf ("\n Sorted: \n"); 

    for (i = 0; i < num_states; i++) 
    { 
     printf ("state %d: %s\n", i, states[i]); 
    } 


    return 0; 
} 

更新:所以我想通了一個辦法,使工作方案。正如用戶在這裏提到的那樣,我必須讓所有字符串都具有相同的大小,並使用this post here來實現。但是當程序進入排序功能時我仍然有問題。

更新代碼 1的#include 2的#include

3 void 
4 gnome_sort (char states[][20], int n) 
5 { 

6 int i = 0, j = 2; 
7 char temp[20]; 


8 while (i < n) 
9  { 

10  printf ("\n *** Inside while loop *** \n"); 
11  printf ("\n %d", strcmp (states[i - 1], states[i])); 
12  if (strcmp (states[i - 1], states[i]) < 0) 
13  { 

14  strcpy (temp, states[i]); 
15  printf ("\n %s \n", temp); 
16  strcpy (states[i], states[i - 1]); 
17  strcpy (states[i - 1], temp); 
18  if (--i) 
19   continue; 
20  } 

21  i = j; 
22  j++; 



23  } 


24 printf ("\n Sorted in gnome sort: \n"); 

25 for (i = 0; i < n; i++) 
26  { 
27  printf ("state %d: %s\n", i, states[i]); 
28  } 



29 //end of function 
30 } 


31 int 
32 main() 
33 { 

34 char states[][20] = { "Washington", "Colorado", "Iowa", "Texas" }; 
35 int num_states = 4; 

36 // int n; 

37 //n=sizeof a/sizeof a[0]; 

38 //printf("n is %d ",n); 

39 int i; 
40 printf ("\n Unsorted:\n"); 

41 for (i = 0; i < num_states; i++) 
42  { 
43  printf ("state %d: %s\n", i, states[i]); 
44  } 

45 gnome_sort (states,num_states); 

46 /* printf ("\n Sorted: \n"); 

47 for (i = 0; i < num_states; i++) 
48  { 
49  printf ("state %d: %s\n", i, states[i]); 
50  }*/ 


51 return 0; 
52 } 

更新的輸出

$ ./a.out 

Unsorted: 
state 0: Washington 
state 1: Colorado 
state 2: Iowa 
state 3: Texas 

*** Inside while loop *** 

-87 
Washington 

*** Inside while loop *** 

-87 
Washington 

*** Inside while loop *** 

18 
*** Inside while loop *** 

-6 
Iowa 

*** Inside while loop *** 

-73 
Iowa 

*** Inside while loop *** 

-17 
Texas 

*** Inside while loop *** 

-84 
Texas 

*** Inside while loop *** 

-11 
Texas 

Sorted in gnome sort: 
state 0: Texas 
state 1: Iowa 
state 2: 
state 3: Colorado 
*** stack smashing detected ***: ./a.out terminated 
Aborted (core dumped) 

最終解決方案: 堆棧溢出可能是因爲字符串大小WASN」足夠大,我有種被另一個01暗示。該程序現在就像我想要的,但不知道爲什麼字符串必須是30個字符長,因爲最大的字符串 - 華盛頓 - 是10個字符長,這是遠低於20

最終代碼

1 #include<stdio.h> 
    2 #include<string.h> 

    3 void 
    4 gnome_sort (char states[][30], int n) 
    5 { 

    6 int i = 1, j = 2; 
    7 char temp[30]; 


    8 while (i < n) 
    9  { 

    10  printf ("\n *** Inside while loop *** \n"); 
    11  printf ("\n %d", strcmp (states[i - 1], states[i])); 
    12  if (strcmp (states[i - 1], states[i]) > 0) 
    13  { 

    14  strcpy (temp, states[i]); 
    15  printf ("\n %s \n", temp); 
    16  strcpy (states[i], states[i - 1]); 
    17  strcpy (states[i - 1], temp); 
    18  if (--i) 
    19   continue; 
    20  } 

    21  i = j; 
    22  j++; 



    23  } 


    24 printf ("\n Sorted in gnome sort: \n"); 

    25 for (i = 0; i < n; i++) 
    26  { 
    27  printf ("state %d: %s\n", i, states[i]); 
    28  } 



    29 //end of function 
    30 } 


    31 int 
    32 main() 
    33 { 

    34 char states[][30] = { "Washington", "Colorado", "Iowa", "Texas" }; 
    35 int num_states = 4; 

    36 // int n; 

    37 //n=sizeof a/sizeof a[0]; 

    38 //printf("n is %d ",n); 

    39 int i; 
    40 printf ("\n Unsorted:\n"); 

    41 for (i = 0; i < num_states; i++) 
    42  { 
    43  printf ("state %d: %s\n", i, states[i]); 
    44  } 

    45 gnome_sort (states,num_states); 

    46 /* printf ("\n Sorted: \n"); 

    47 for (i = 0; i < num_states; i++) 
    48  { 
    49  printf ("state %d: %s\n", i, states[i]); 
    50  }*/ 


    51 return 0; 
    52 } 

最終輸出

$ ./a.out 

Unsorted: 
state 0: Washington 
state 1: Colorado 
state 2: Iowa 
state 3: Texas 

*** Inside while loop *** 

20 
Colorado 

*** Inside while loop *** 

14 
Iowa 

*** Inside while loop *** 

-6 
*** Inside while loop *** 

3 
Texas 

*** Inside while loop *** 

-11 
Sorted in gnome sort: 
state 0: Colorado 
state 1: Iowa 
state 2: Texas 
state 3: Washington 

回答

2

你的排序代碼副本串之間的a(原states)的元素,但不是所有這些字符串都是相同的大小。因此,如果您正在複製某些內容到Texas所在的位置,副本將會經過Texas結尾處的位置,從而修改在Texas之後在空間中存儲的任何內容。

+0

換句話說,我需要實際聲明狀態數組的大小?像char * states [20]? – 2014-10-10 01:15:07

+1

問題不在於'states'中的元素數目;問題是分配給該數組中每個字符串的空間。一種解決辦法是將聲明改爲「國家[4] [20]」;這將爲每個字符串留出20個字節,因此每個插槽都有空間存放任何名稱。另一個將交換指針而不是交換字符(這將有更快的額外好處)。 – 2014-10-10 10:55:56