Thread: Array Element Insertion in Ascending Order Question

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    5

    Array Element Insertion in Ascending Order Question

    Hi Everyone,

    I'm trying to write a program where I can:

    1.) Select the number of elements to place inside an array of size 10, and
    2.) Select an element and insert that element into the proper location

    resulting in an array where all of the elements are sorted in ascending order,
    from 0 to n - 1.

    So far, I've been able to place all of the elements I desire into that array and select an element to insert. However, when I insert new element into the array, I keep overwriting the next element in the array and then there is some garbage value for the last value in the array. I'm getting a bit confused and was wondering if someone could point out the error in my code and suggest a better alternative so that I end up with a nice sorted array in ascending order after inserting the new element.

    Here is my code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int arr[10];
        int i, j, n, num;
        
        printf("\nEnter the number of elements in the array: ");
        scanf("%d", &n);
        
        for (i = 0; i < n; i++)
        {
            printf("\narr[%d] = ", i);
            scanf("%d", &arr[i]);
        }
        
        printf("\nEnter the element to be inserted: ");
        scanf("%d", &num);
        
        for (i = 0; i < n; i++)
        {
            if (arr[i] > num)
            {
                for (j = n; j <= i; j--)
                {
                    arr[j+1] = arr[j];
                }
            arr[i] = num;
            break;
            }
        }
        
        printf("\nThe array after insertion of %d is: ", num);
        for (i = 0; i < n + 1; i++)
        {
            printf("\narr[%d] = %d", i, arr[i]);
        }
        
        return 0;
    }
    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I see a couple of problems.

    First your scanf() is not putting the numbers in the proper order (unless the user enters the numbers in correct order).

    Second your "insertion" doesn't seem to make room for the new element it just seems to replace the element.

    I suggest you consider using a function to insert the elements in the correct order, you also may want to initialize the array when you declare the array.


    Code:
    Enter the number of elements in the array: 3
    
    arr[0] = 5
    
    arr[1] = 45
    
    arr[2] = 3
    
    Enter the element to be inserted: 6
    
    The array after insertion of 6 is: 
    arr[0] = 5
    arr[1] = 6
    arr[2] = 3
    arr[3] = 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion sort in ascending order isnt working
    By mizzou222 in forum C Programming
    Replies: 5
    Last Post: 06-20-2014, 10:46 AM
  2. Array in ascending order and Average
    By Steveqb14 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2014, 12:48 PM
  3. Sort an Array in Ascending Order ?
    By Coding in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2008, 08:32 PM
  4. array in ascending order
    By galmca in forum C Programming
    Replies: 4
    Last Post: 10-25-2004, 11:44 AM
  5. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM

Tags for this Thread