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.