Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define MAX_SIZE 15
- int add(int A[],int n,int x);
- int main()
- {
- int i,n,arr[MAX_SIZE],x,result;
- printf("How many elements: \n");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- scanf("%d",&arr[i]);
- }
- printf("Enter new elements to insert: \n");
- scanf("%d",&x);
- result=add(arr,n,x);
- if(result==-1)
- {
- printf("\n%d can't be added successfully\n",x);
- }
- else
- {
- printf("\n%d is inserted at position: %d\n",x,result);
- }
- return 0;
- }
- int add(int A[],int n,int x)
- {
- int i,getIPos;
- if(n>=MAX_SIZE)
- {
- return -1; /// because n already occupied max size thus we can't add anymore
- }
- for(i=n-1; i>=0 && A[i]> x ; i--)
- {
- A[i+1]=A[i];
- }
- A[i+1]=x;
- getIPos=i+1;
- printf("After Insertion: \n");
- for(i=0;i<n+1;i++)
- {
- printf("%d ",A[i]); ///this printing is unnecessary as per the question guideline
- }
- return getIPos;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement