Thread: Reversing an array

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    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[10];
        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. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    If you're starting from both ends, then you would want to stop in the middle.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    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. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    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. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Dave11 View Post
    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 subscribe to a feed

Similar Threads

  1. Need help in reversing words in an Array
    By Justin Page in forum C Programming
    Replies: 8
    Last Post: 07-22-2012, 12:18 AM
  2. reversing elements in array
    By dantestwin in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2004, 08:24 PM
  3. string array reversing...
    By cit in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 03:09 AM
  4. reversing elements of an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 09:04 PM
  5. Reversing a Multi-D Array
    By Bradley33 in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:37 AM