Thread: Help with program using arrays.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Help with program using arrays.

    The problem deals with writing a program to geta series of integers from a user and storing those values into an array. Then use a function called selection_sort, when given an array with n elements, the function must sort the array from smallet to largest values.

    I have code, and im trying to get it to compile but im getting these implicit declaration errors and conflicting types, I have no idea why im getting them. Any help is appreciated, Here is my code and the compiler errors.

    Code:
    Compilation started at Sun Feb 10 20:14:48
    
    gcc -Wall -o ex9-1 ex9-1.c
    ex9-1.c: In function ‘main’:
    ex9-1.c:16:5: warning: implicit declaration of function ‘selection_sort’ [-Wimplicit-function-declaration]
    ex9-1.c:20:2: warning: implicit declaration of function ‘prinf’ [-Wimplicit-function-declaration]
    ex9-1.c: At top level:
    ex9-1.c:31:6: warning: conflicting types for ‘selection_sort’ [enabled by default]
    ex9-1.c:16:5: note: previous implicit declaration of ‘selection_sort’ was here
    /tmp/ccKgLSrk.o: In function `main':
    ex9-1.c:(.text+0xe2): undefined reference to `prinf'
    collect2: ld returned 1 exit status
    
    Compilation exited abnormally with code 1 at Sun Feb 10 20:14:49
    Code:
    #include <stdio.h>
    
    
    int main(void) {
    
        int a[100], i, j, N;
    
        printf("How many numbers will you be entering: ");
        scanf("%d", &N);
    
        printf("Enter numbers to be sorted: ");
        for(i = 0; i < N; i++) {
        scanf("%d", &a[i]);
        }
        
        selection_sort(a[i], N);
        
        printf("Your sorted list of integers is : ");
        for(j = 0; j < N; j++) {
        prinf("%4d\n", &a[i]);
        }
    
        return 0;
    
    }
    
    void selection_sort(int a[], int size) {
        int i, j, x;
    
        for(i = 1; i <= size - 1; i++) {
    
        j = i;
        x = a[i];
        
        while(a[j - 1] > x && j > 0) {
            a[j] = a[j-1];
            j = j - 1;
        }
        
        a[j] = x;
        }
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You need a prototype for your selection sort function above main.
    You need the 't' in printf on line 20, and you also want to drop the & unless you want to print out the addresses instead of the actual numbers.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok i fixed the printf, put the signature of the function before the main, and took out the & sign, it compiles, but it gives me this error in output.
    Code:
    connor@connor-VirtualBox:~$ cd hw06
    connor@connor-VirtualBox:~/hw06$ ./ex9-1
    How many numbers will you be entering: 3
    Enter numbers to be sorted: 1 2 3
    Segmentation fault (core dumped)
    connor@connor-VirtualBox:~/hw06$ ./ex9-1
    How many numbers will you be entering: 3
    Enter numbers to be sorted: 1
    2
    3
    Segmentation fault (core dumped)
    connor@connor-VirtualBox:~/hw06$

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You are passing in one instance of the array for your selection sort in main, you need to call it like this-> selection_sort(a,N);

    EDIT: also in your final call to print your array you need to index the array with the variable j, not i.

    Code:
    printf("Your sorted list of integers is : ");
        for(j = 0; j < N; j++) {
        printf("%4d\n", a[i]);//<-You are using i here, but the loop is using the variable j
        }
    Last edited by camel-man; 02-10-2013 at 09:46 PM.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    ok that works, How would i set this up so that i can have the user enter as many numbers as they want, until they hit a certain button on the keyboard, like ctrl + d?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a program on arrays. need some help.
    By ashlee in forum C Programming
    Replies: 11
    Last Post: 10-16-2010, 01:15 PM
  2. Need help with program using arrays
    By altrev in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2009, 01:25 PM
  3. Arrays Program...help Me!
    By TrYpSiN4EvEr in forum C Programming
    Replies: 8
    Last Post: 11-07-2002, 06:08 PM
  4. Need help with 'C' Program (arrays)
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 12:36 AM