Home > Enterprise >  I am trying to reverse an array in C. But, the code throws out random 1-2 to some very large numbers
I am trying to reverse an array in C. But, the code throws out random 1-2 to some very large numbers

Time:09-24

As the title states I am trying to make a program using C which asks user to input the array creates a new array, where the values in the array have been reversed. For ex, Input: 10, 20, 30, 40 Output: 40, 30, 20, 10 I had written the following code for reversing the arrays,

#include <stdio.h>
#define MAX_SIZE 100
int main()
{
    int sizeArray;
    int arr[MAX_SIZE];
    int * ptr = arr;
    printf("Enter Array size: ");
    scanf("%d", &sizeArray);
    printf("Enter Array elements:\n");
    for (int i = 0; i < sizeArray; i  )
    {
        scanf("%d", ptr   i);
    }
    printf("Copying to another array....\n");
    
    int newArr[MAX_SIZE];
    int * ptr2 = newArr;
    for (int i = 0; i < sizeArray; i  )
    {
        *(ptr2   i) = *(ptr   sizeArray - i 1 );
    }
    printf("Printing new array:\n");
    for (int i = 0; i < sizeArray; i  )
    {
        printf("%d\n", *(ptr2   i));
    }
    return 0;

For ex: When I input the values: 1, 2, 3, 4 The output is: 897546457, 1, 4, 3

Please help me with what I am doing wrong here.

CodePudding user response:

In this statement

*(ptr2   i) = *(ptr   sizeArray - i 1 );

the expression ptr sizeArray - i 1 points outside the set of actual data then i is equal to 0 or equal to 1 because in this case you have

ptr   sizeArray - 0  1

that is the same as

ptr   sizeArray  1

and

ptr   sizeArray - 1  1

that is the same as

ptr   sizeArray

Also you did not reset the pointer ptr2 before this loop

printf("Printing new array:\n");
for (int i = 0; i < sizeArray; i  )
{
    printf("%d\n", *(ptr2   i));
}

If you want to use pointers to copy in the reverse order one array in another then the code can look the following way

#include <stdio.h>

#define MAX_SIZE 100

int main( void )
{
    int arr[MAX_SIZE];
    int sizeArray;

    printf("Enter Array size: ");
    if ( scanf("%d", &sizeArray) != 1 )
    {
        sizeArray = 1; 
    }
    else if ( MAX_SIZE < sizeArray )
    {
        sizeArray = MAX_SIZE;
    }

    int *ptr = arr;

    printf("Enter Array elements:\n");
    for (; ptr < arr   sizeArray;   ptr )
    {
        scanf("%d", ptr );
    }
    printf("Copying to another array....\n");
    
    int newArr[MAX_SIZE];
    int * ptr2 = newArr;
    
    while ( ptr != arr )
    {
        *ptr2   = *--ptr;
    }

    printf("Printing new array:\n");
    for ( ptr2 = newArr; ptr2 < newArr   sizeArray;   ptr2 )
    {
        printf("%d\n", *ptr2);
    }

    return 0;
}

CodePudding user response:

You complicate your life with these pointers, they are not useful here. And use the [] operator for array access, it's easier.

#include <stdio.h>
#define MAX_SIZE 100

int main(void)
{
    int sizeArray=6;
    int arr[MAX_SIZE] = {1, 2, 3, 4, 5, 6};

    printf("Copying to another array....\n");
    int newArr[MAX_SIZE];    
    for (int i = 0; i < sizeArray; i  )
    {
        newArr[i] = arr[sizeArray-i-1];
    }

    printf("Printing new array:\n");
    for (int i = 0; i < sizeArray; i  )
    {
        printf("%d\n", newArr[i]);
    }
    return 0;
}
  • Related