= (pos-1); i--) { a[i+1]=a[i]; // w" /> = (pos-1); i--) { a[i+1]=a[i]; // w" /> = (pos-1); i--) { a[i+1]=a[i]; // w"/>

why loop is not iterating intended number of times?

121 Views Asked by At
printf("Enter position : ");
    scanf("%d", &pos);
    printf("Enter element  : ");
    scanf("%d", &element);
    for ( i = 4; i >= (pos-1); i--)
    {   
        a[i+1]=a[i]; // why is this loop only working one time when pos=3
    }

this is the portion of my code where i am trying to insert an element into an array of size 5 with 4 elements by starting to shift elements to the next indexes but i am shifting from the fifth element itself that is 0(or garbage value) . i know this is not the correct way to achieve insertion but my question is why this line of code is not working

a[i+1]=a[i];

also the loop doesn't seem to work 3 times but instead 1 time .( which is my main question )

my original code :

#include<stdio.h>

int main(){
    int a[5],i, pos , element;
    printf("Enter elements : ");
    for ( i = 0; i < 4; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter position : ");
    scanf("%d", &pos);
    printf("Enter element  : ");
    scanf("%d", &element);
    for ( i = 4; i >= (pos-1); i--)
    {   
        a[i+1]=a[i];
    }
    a[pos-1]=element;
    for ( i = 0; i < 5; i++)
    {
       printf("%d ",a[i]);
    }
    
    return 0;
}
2

There are 2 best solutions below

6
Vlad from Moscow On

This for loop

for ( i = 4; i >= (pos-1); i--)
{   
    a[i+1]=a[i];
}

is accessing memory outside the array when i is equal to 4 due to the expression a[i+1] that in this case is equivalent to a[5] while the valid range of indices is [0, 4].

So the code invokes undefined behavior.

Also if you want to add a new value at the position pos then using this condition in the for loop

i >= (pos-1)

instead of at least this condition

i >= pos

does not make sense.

And you should check that the value of the variable pos is in the range of valid indices. For example as the variable pos has the signed integer type int then nothing prevents the user to enter a negative value.

Instead of writing the for loop that is error prone you could use standard C string function memmove declared in the header <string.h>.

For example

#include <string.h>

//...

enum { N = 5 };
int a[N];

//...

if ( 0 <= pos && pos < N )
{
    memmove( a + pos + 1, a + pos, ( N - pos - 1 ) * sizeof( *a ) );
    a[pos] = element;
}

As for the for loop then it can look for example the following way

if ( 0 <= pos && pos < N )
{
    for (int i = N; --i != pos; )
    {
        a[i] = a[i - 1];
    }
    a[pos] = element;
}

Also pay attention to that the first for loop

for ( i = 0; i < 4; i++)
{
    scanf("%d",&a[i]);
}

leaves the last element of the array uninitialized.

Try to not use magic numbers like 4 or 5. Use named constants as for example

enum { N = 5 };
int a[N];
6
Eric Postpischil On

Since a is defined as int a[5], it has elements from a[0] to a[4]. Consider what happens if the compiler puts i where a[5] would be. Then, when i is 4, a[i+1] = a[i] overwrites i, which can change it to a value that stops the loop.