1. ## Reversing an array

hey everyone,

I'm trying to understand a calculation here why a certain function has to be the way it is.

Below is a code where the user enters a number for n times and then reverses it and on the for loop initializing the i, i don't understand why i have to divide the number by 2. Please have a look and explain it to me because i really don't understand

Code:
```#include <stdio.h>

int main()
{
int arr;
int num,i, temp = 0;

printf("Enter Number: ");
scanf("%d", &num);

for (i=0; i<num; i++)
{
printf("Arr[%d]: ", i);
scanf("%d", &arr[i]);
}

for (i=0; i<num/2; i++)         // Here i don't understand num/2
{
temp = arr[i];
arr[i] = arr[num-1-i];
arr[num-1-i] = temp;
}

for (i=0; i<num; i++)
{
printf("Arr[%d] = %d\n",i,arr[i]);
}

}``` 2. If you're starting from both ends, then you would want to stop in the middle. 3. Originally Posted by Matticus If you're starting from both ends, then you would want to stop in the middle.
so if let's say num=5 it means that i start at num=3? 4. if you keep on reversing after you had passed the middle you would wind up with the same array

for example lets take:
1-2-3-4
let's do the first step
4-2-3-1
let's do another step and get to the middle:
4-3-2-1
we need to stop here , if we continue we'll get:
4-2-3-1
1-2-3-4 5. Originally Posted by Dave11 if you keep on reversing after you had passed the middle you would wind up with the same array

for example lets take:
1-2-3-4
let's do the first step
4-2-3-1
let's do another step and get to the middle:
4-3-2-1
we need to stop here , if we continue we'll get:
4-2-3-1
1-2-3-4
amazing thank you i understand it now Popular pages Recent additions 