1

Help me out....There is no compile time error but there is some logical error which i am not able to sort out. Input is taken from user without any problem but the elements are not getting inserted. Output is unchanged array that user inputted.

void insert(int*,int);

void main()
{
    int a[10];
    int i,n,pos,x,j,z;
    clrscr();
    printf("Enter Size Of an array: ");
    scanf("%d",&n);
    printf("Enter Elements of an array: ");
    for(i=0;i<n;i++)
    {
        scanf("%d",a+i);
    }
    insert(a,n);
    printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
    for(i=0;i<n;i++)
    {
        printf("\t%d\t",*a+i);
    }
    getch();
}

void insert(int *b, int n)
{
    if(n>=1)
    {
        printf("Insert Element at 2nd Position: ");
        scanf("%d",b+1);
    }
    if(n>=4)
    {
        printf("Insert Element at 5th Position: ");
        scanf("%d",b+4);
    }
}
7
  • In which way is program behaviour unexpected? You may want to call insert two times. Btw what happens if the user supplies an n that is larger than 10? Commented Aug 25, 2013 at 19:31
  • I think you just replace your elements in 2nd or 5th position and not insert a value. Try to make your array dynamic or use a dynamic list instead. See this thread: stackoverflow.com/questions/4063857/dynamic-list-in-ansi-c. Commented Aug 25, 2013 at 19:32
  • One place right off the top that will march straight into undefined behavior is any n greater than 10. And maybe check the results of those scanf() invokes just for, you know, the thrill. Commented Aug 25, 2013 at 19:32
  • WhozCraig, n == 10 is problematic, too. Commented Aug 25, 2013 at 19:35
  • @Fred where does n==10 cause invalid indexing at a[10+] ? Commented Aug 25, 2013 at 19:36

5 Answers 5

3

Your output is wrong, and it's caused by *a+i being interpreted as (*a)+i. This also shows up without any modification when you enter something other than a direct sequence:

Enter Size Of an array: 3
Enter Elements of an array: 1 2 9

gives the output:

1       2       3

which is clearly not right.

The solution, as mentioned in another couple of answers is to wrap your pointer-arithmetic with paranthesis: *(a+i).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 As usual, Mats, a solid answer example, explaining both the problem, why it is the problem, and why the fix works.
2

"Output is unchanged array that user inputted."

Fix :-

for(i=0;i<n;i++)
{
    printf("\t%d\t",*(a+i));  //Notice `()`
}

FYI..this is not insertion, this is just an over-write !

For Insertion you could have something like following :

/*
   b= original array
   n= size of array (must be large enough)
   pos = position of insertion

  After call make sure to scan array till n+1
*/
 void insert(int *b, int n, int pos)
 {
   int val,c;
   printf("Enter the value to insert\n");
   scanf("%d", &val);

   for (c = n - 1; c >= pos - 1; c--)
      b[c+1] = b[c];

   b[pos-1] = val;
 }

6 Comments

Hey, Thanks P0W, now the problem is the program is replacing the old elements with the new ones. Assuming that the user will not enter more than 10 elements. lets say we take 'n=5'. Then it will work. But i need to add elements not replace it. Please Help.
And its not an overwrite, it (the original problem) was an incrementing additive operation on a single value in the array, namely a[0]. Part of the reason we post ChangeThis ToThis, as it is usually applicable along with explaining why.
@WhozCraig so you mean, the OP code is actually inserting at 2nd/5th position ?
Relative to the 0-offset being considered the 1st position. yes. If by insertion-with-shifting as your updated-code is doing, no. Under that guise you're correct it would be more appropriate to say the OPs code "writes" to the 2nd/5th positions, while you're code "inserts" there. Your point is understood, and we can certainly agree the OP wasn't crystal clear on their definition of "insert" .
@WhozCraig got it, thanks, feel free to change the wordings in my post whatever make sense :D
|
1

You should try using parenthesis around the a+i, e.g.

void main()
{
    int a[10];
    int i,n,pos,x,j,z;
    clrscr();
    printf("Enter Size Of an array: ");
    scanf("%d",&n);
    printf("Enter Elements of an array: ");
    for(i=0;i<n;i++)
    {
        scanf("%d",a+i);
    }
    insert(a,n);
    printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
    for(i=0;i<n;i++)
    {
        printf("\t%d\t", /*SEE HERE */ *(a+i));
    }
    getch();
}

1 Comment

Yeah, the other guy answered quicker than me. I wrote a sample program to demonstrate in more context :)
1
for(i=0;i<n;i++)
{
    printf("\t%d\t",*(a+i));  //parenthesis added.
}

The parenthesis around the "(a+i)" are of utmost importance as together with the asterisk it indicates the value within that address. Errors like this are hard to find as they are very small but have a huge impact on the code.

Comments

0

Finally I solved the problem with the help of mistake pointed out by @P0W and @Mats and also applying concept of inserting element at any user defined position. Thanks Guys!! So Here is the Corrected code. (And it is inserting element and not replacing it).

void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
    scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
    printf("\t%d\t",*(a+i));
}
getch();
}
void insert(int *b, int n)
{
int j;
if(n>=1)
{
    printf("Insert Element at 2nd Position: ");
    for(j=n-1;j>=1;j--)
    {
        b[j+1]=b[j];
    }
    scanf("%d",b+1);
}
if(n>=4)
{
    printf("Insert Element at 5th Position: ");
    for(j=n-1;j>=4;j--)
    {
        b[j+1]=b[j];
    }
    scanf("%d",b+4);

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.