C 中调用函数的不合理错误 - 程序对数组进行排序

发布于 2024-11-01 02:51:40 字数 2381 浏览 1 评论 0原文

我编写了一个程序,从用户接收一系列数字(<=20),而最后一个“0”表示系列结束(不包含在系列存储中)。 2 个数组 (x,y) 大小为 20(0-19 + 1 表示“0”)必须为零,m 表示 Y 数组中的器官数量。

用户必须输入升序的数字(可以是4ex.1,2,2,3,7,8,...,0)并以“0”结尾,当然,如果没有,则会出现相应的错误消息,并且程序将关闭。

我们可以确定用户将保留 <=20 个输入数字。

Y 数组将是(如果 X 数组一切顺利的话)X 的排序数组,但没有重复项。 “m”将是 Y 中的器官数量,当然不包括“0”。

函数 SIFT 必须仅组织 Y 数组以便从 main() 打印。

示例:

如果用户将存储在 X:1,1,2,3,5,5,5,6

屏幕上将是:m = 5 Y = 1,2,3,5,6

我的试用:< /strong>

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

void SIFT(int x_arr[ ], int y_arr[]);

int main ()
{
    int x[20] = {0} , y[20] = {0};
    int m=0,temp=0,curr=0,i=0,j=0;

    printf("Please enter your numbers now:\n\n");

    /*enter numbers one by one. if x[i+1] value < x[i] value, err msg.
      when user want to end the series he must enter '0' which means end of string (it wont       included in x[]) */
    while ( ( temp = getchar() ) != '0' )
    {
        if (temp >= curr)
        {
            x[i] = temp;
            curr = temp;
            i++;
        }
        else
        {
            printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n");
        }
    }

    SIFT(x,y);

    for (i=0 ; y[i]=='0' ; i++) /*strlen(y) without ('0')'s includes*/
        m++;

    /*Prints  m , y's organs*/
    printf("\n\nm = %d",m);
    printf("Y = ");
    while (y[j]!='0')
    {
        printf ("%d ,",y[j]);
        j++;
    }

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;

    while (x_arr[i] != '0')
    {
        if (x_arr[i] == x_arr[i+1]) /*if current val. equals next val. -> jump dbl at x_arr*/
        {
            y_arr[j] = x_arr[i];
            i+=2;
            j++;
        }
        else
        {
            y_arr[j]=x_arr[i];
            i++;
            j++;
        }
    }    

}

return 0;
}

我从 gcc 得到的错误是:

gcc -g -ansi -pedantic -Wall 2.c -o 2   
2.c: In function ‘main’:
2.c:43: warning: ISO C forbids nested functions
2.c:43: warning: ISO C90 forbids mixed declarations and code
/tmp/ccyZNfkF.o: In function `main':
/home/student/Desktop/2/2.c:29: undefined reference to `SIFT'
collect2: ld returned 1 exit status
make: *** [2] Error 1

另一个问题:

我想将此代码转换为 MIPS 汇编代码,是否有一个简短的代码快速的方法是这样做吗?

谢谢大家!!

I wrote a program that receive from user a series of numbers (<=20) while the last '0' means end of series (not included in series storing).
2 arrays (x,y) size 20 (0-19 + 1 for the '0') must be zeros, and m means number of organs in Y array.

The user must enter numbers ascending (it is ok 4ex. 1,2,2,3,7,8,...,0) and end with a '0' of course, if not, appropriate error message will appear, and program will shut off.

We can be sure the user will keep the <=20 numbers of input.

Y array will be (if everything went ok with X array) a sorted array of X but without duplicates.
'm' will be number of organs in Y exclude '0' of course.

Function SIFT must only organize the Y array for being printed from main().

Example:

If user will store in X: 1,1,2,3,5,5,5,6

On screen will be: m = 5 Y = 1,2,3,5,6

My tryout:

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

void SIFT(int x_arr[ ], int y_arr[]);

int main ()
{
    int x[20] = {0} , y[20] = {0};
    int m=0,temp=0,curr=0,i=0,j=0;

    printf("Please enter your numbers now:\n\n");

    /*enter numbers one by one. if x[i+1] value < x[i] value, err msg.
      when user want to end the series he must enter '0' which means end of string (it wont       included in x[]) */
    while ( ( temp = getchar() ) != '0' )
    {
        if (temp >= curr)
        {
            x[i] = temp;
            curr = temp;
            i++;
        }
        else
        {
            printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n");
        }
    }

    SIFT(x,y);

    for (i=0 ; y[i]=='0' ; i++) /*strlen(y) without ('0')'s includes*/
        m++;

    /*Prints  m , y's organs*/
    printf("\n\nm = %d",m);
    printf("Y = ");
    while (y[j]!='0')
    {
        printf ("%d ,",y[j]);
        j++;
    }

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;

    while (x_arr[i] != '0')
    {
        if (x_arr[i] == x_arr[i+1]) /*if current val. equals next val. -> jump dbl at x_arr*/
        {
            y_arr[j] = x_arr[i];
            i+=2;
            j++;
        }
        else
        {
            y_arr[j]=x_arr[i];
            i++;
            j++;
        }
    }    

}

return 0;
}

The error I'm getting from the gcc is:

gcc -g -ansi -pedantic -Wall 2.c -o 2   
2.c: In function ‘main’:
2.c:43: warning: ISO C forbids nested functions
2.c:43: warning: ISO C90 forbids mixed declarations and code
/tmp/ccyZNfkF.o: In function `main':
/home/student/Desktop/2/2.c:29: undefined reference to `SIFT'
collect2: ld returned 1 exit status
make: *** [2] Error 1

And another question:

I would like to convert this code to the MIPS Assembly code, is there a short and fast way to do so ?

Thanks all you guys !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2024-11-08 02:51:40

在声明 SIFT 之前,您没有关闭 main 函数,因此 SIFT 是在 main 内部声明的,这是被禁止的。

通过在 SIFT() 定义之前从 main 返回来修复此问题:

...
return 0;
}

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;
...

You didn't close main function before declaration of SIFT, so SIFT is declared inside of main which is forbidden.

Fix it by returning from main before the definition of SIFT():

...
return 0;
}

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文