2016-08-03 66 views
-1

我目前正在编写该书调试艺术。我已经输入了第一个练习的代码,我应该接收输出到终端。当我运行该程序时,什么都不显示。但是,当我查看GDB中的变量时,我可以正确看到参数的值。没有输出显示到终端,但值显示在GDB

主要问题是print_results()函数。它不会打印任何内容。谁能帮忙?我已经在运行Linux的两台不同机器(Debian和Lubuntu)上尝试了这一点。

下面的代码:

// insertion sort, several errors 
// usage: insert_sort num1 num2 num3 ..., where the num are the numbers to 
// be sorted 
#include <stdio.h> 
#include <stdlib.h> 

int x[10], // input array 
    y[10], // workspace array 
    num_inputs, // length of input array 
    num_y = 0; // current number of elements in y 

void get_args(int ac, char **av) 
{ int i; 

    num_inputs = ac - 1; 
    for (i = 0; i < num_inputs; i++) 
     x[i] = atoi(av[i+1]); 
} 

void scoot_over(int jj) 
{ int k; 

    for (k = num_y; k > jj; k--) 
     y[k] = y[k-1]; 
} 

void insert(int new_y) 
{ int j; 

    if (num_y == 0) { // y empty so far, easy case 
     y[0] = new_y; 
     return; 
    } 
    // need to insert just before the first y 
    // element that new_y is less than 
    for (j = 0; j < num_y; j++) { 
     if (new_y < y[j]) { 
     // shift y[j], y[j+1],... rightward 
     // before inserting new_y 
     scoot_over(j); 
     y[j] = new_y; 
     return; 
     } 
    } 
    // one more case: new_y > all existing y elements 
    y[num_y] = new_y; 
} 

void process_data() 
{ 
    for (num_y = 0; num_y < num_inputs; num_y++) 
     // insert new y in the proper place 
     // among y[0],...,y[num_y-1] 
     insert(x[num_y]); 
} 

void print_results() 
{ 
    int i; 
    for(i=0; i < num_inputs; i++) 
     printf("%d\n", y[i]); 
} 

int main(int argc, char ** argv) 
{ get_args(argc,argv); 
    process_data(); 
    print_results(); 
} 

由于提前, 杰西

+1

你如何运行该程序?它需要参数进行分类和打印,所以用“ 1 2 3 4”打印效果很好。见[demo](http://coliru.stacked-crooked.com/a/0d8885a5cfa02da7) – Mine

回答

1

程序取决于所传递的参数!

它完全有效,但你需要传递它的参数。

由于在代码的顶部注释的使用状态:

//用法:insert_sort NUM1 NUM2 NUM3 ...,其中NUM是数字到

尝试它!

+0

哦,我的天啊。我觉得自己像个白痴。我有一个全脑放屁。我知道我应该走开,稍后再回来看看。我只是在运行./a.out,尽管我知道我应该将整数作为参数传递。 无论如何,感谢您的帮助。我只是需要一个推动才能回到正轨。 – JEM