Thread: C number sorting array help

  1. #1
    Registered User
    Join Date
    Mar 2009

    C number sorting array help

    #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);
        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
    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
    a is an array , int a[n]; <-- isn't i am difined?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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