开发者

Unreasonable error calling function in C - Program to sort an array

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 onl开发者_运维百科y 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 !!


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;
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜