Sunday 10 January 2016

Inserting An Element into a Linear Array (Algorithm 4.2)


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int N;
    scanf("%d", &N);
    int LA[N+1], K, j, item, i;
    for(i=0; i<N; i++) scanf("%d", &LA[i]);
    scanf("%d%d", &K, &item);
    for(j=N-1; j>=K; j--) LA[j+1] = LA[j];
    LA[K] = item;
    for(i=0; i<=N; i++) printf("%d\t", LA[i]);
    printf("\n");
    return 0;
}
First input N = array element number.  ex. 7
Second input LA[i] = input N element one by one. ex. 1 2 4 5 6 7 8
Third input K = In which location I want to insert.  ex. 2
Forth input item = What I want to insert.  ex. 3
Output ex. 1 2 3 4 5 6 7 8


1 comment:

Note: only a member of this blog may post a comment.