Home > Software engineering >  Reverse the contents of array of n elements
Reverse the contents of array of n elements

Time:07-27

#include <stdio.h>
int main()
{
    int a[100],n,i;
    printf("Enter the number of elements you want to enter in any array:");
    scanf("%d",&n);

    printf("Enter the elements you want to enter in the array:\n");

    for(i=0;i<n;i  )
    {
        scanf("%d",&a[i]);
    }
    
    printf("Array:\n");

    for(i=0;i<n;i  )
    {
        printf("%d\t",a[i]);
    }
    
    printf("\nReversed order:\n");

    
for(i=n;i>=0;i--)
    {
        printf("\n %d",a[i]);
    }
}

What is the mistake in my code? This code is not completely correct. Please tell me where I am making mistake. I am not getting reverse of the array properly so there must be mistake in this part:

for(i=n;i>=0;i--)
        {
            printf("\n %d",a[i]);
        }

CodePudding user response:

In this loop

for(i=n;i>=0;i--)
{
    printf("\n %d",a[i]);
}

when i is initially equal to n then you are trying to output an uninitialized elements of the array because the range of indices of initialized elements is [0, n).

You could rewrite the loop like

for ( i = n; i != 0; )
{
    printf("\n %d",a[--i]);
}

or

for ( i = n; i != 0; i-- )
{
    printf("\n %d",a[i - 1]);
}

But in any case the program does not reverse an array (or a sub-array). It outputs an array in the reverse order.

To reverse an array you need to write

for ( i = 0; i < n / 2; i   )
{
    int tmp = a[i];
    a[i] = a[n - i - 1];
    a[n - i - 1] = tmp;
}

and then to output the reversed array like

for ( i = 0; i < n; i   )
{
    printf("\n %d",a[i]);
}

CodePudding user response:

You only read values to a[i] where i<n.

a[n] is not initialized, so you shouldn't use the value.

The final loop should start from i=n-1, not i=n.

CodePudding user response:

You do not reverse the array only try to display the array in reverse order.

To reverse the array you need to swap elements.

int *reverse(int *arr, size_t size)
{
    int *head = arr, *end = arr   size - 1;

    if(arr && size > 1)
    {
        while(end > head)
        {
            int temp = *head;
            *head   = *end;
            *end-- = temp;
        }
    }
    return arr;
}

CodePudding user response:

The stated objective: to Reverse the contents of array of n elements

"What is the mistake in my code?"

Two items:

  • overwriting array bounds in loop
  • failing to populate the contents of an array in reverse order
  1. C by convention uses zero based array indexing, so for an array of n elements, the only time the value n is used within the array brackets [.]is during declaration.
     int n= 10;
     int array[n] = {0};//n used to size the array to exactly n elements

All other times the value n-1 is the maximum value owned by the array. Due to zero based array indexing, the range of values are 0 to n-1. For this statement then, the very first iteration, with i == n, the array index attempts to access memory it does not own:

    for(i=n;i>=0;i--)
    {
        printf("\n %d",a[i]);
        ...              ^  i == n writes to memory location not owned by process.  UB  

Change the for loop: for(i=n-1;i>=0;i--)

  1. A simple algorithm to reverse the contents of an array follows: (in pseudo code).

    temp = arr[i]
    arr[i] = arr[n - i - 1]  
    arr[n - i - 1] = temp
    
  • Related