C number sorting array help

This is a discussion on C number sorting array help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void insertion_sort(int a[], int n); int main() { int n; int i; printf("Enter the array size\n"); scanf("%d", ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    C number sorting array help

    Code:
    #include <stdio.h>
    
    void insertion_sort(int a[], int n);
    
    int main()
    {
        int n;
        int i;
    
        printf("Enter the array size\n");
        scanf("%d", &n);
    
        printf("Enter %d numbers\n", n);
        insertion_sort(a,n);
    
        int a[n];
    		
        for(i = 0; i < n; i++)
        {
            printf("%d ",a[i]);
        }
    
        return 0;
    }
    
    void insertion_sort(int a[], int n)
    {
    int j, p;
    int tmp;
    for(p = 1; p < n; p++)
    {
    tmp = a[p];
    for(j = p; j > 0 && a[j-1] > tmp; j--)
    a[j] = a[j-1];
    a[j] = tmp;
    }
    }
    is there any problem ? i got some errors

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you got some errors, then yes there is a problem. Even better, the errors tell you what the problem is! Things like "a is not a variable" because you haven't defined a variable called a, that sort of thing.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    a is an array , int a[n]; <-- isn't i am difined?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by manfun
    a is an array
    No, it is not. In the scope of the insertion_sort() function, a is a pointer to an int that is expected to the be first int in an array of ints. In the scope of the main() function, a does not exist but is used, and this is an error.

    Incidentally, are you aware that you are using a variable length array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  5. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21