Thread: reverse number of array element

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    reverse number of array element

    How to reverse the element of array in c program

    like array[4]= {2,2,3,4} ---> array[4]= {4,3,2,2};

    Code:
    #include<stdio.h>int main (void)
    {
        int i;
        int array[4]= {2,2,3,4};
        for(i=0; i<4; i++)
        {
          printf("print array element : %d \n", array[i]);
        }
     
        return 0;
    
    
    }
    print array element : 2
    print array element : 2
    print array element : 3
    print array element : 4

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    If you just want to print in the reverse, then the most straightforward way is probably to loop from the last element to the first element. If you want to reverse the elements themselves, then loop over half of the array, swapping each element with the corresponding element in the other half of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    If you just want to print in the reverse, then the most straightforward way is probably to loop from the last element to the first element. .
    Ok

    Code:
    #include<stdio.h>int main (void)
    {
    	int i;
    	int array[4]= {2,2,3,4};
    	for(i=4; i>0; i--)
    	{
    	  printf("print array element : %d \n", array[i]);
    	}
    	
        return 0;
    
    
    }
    but it show different result
    print array element : 4
    print array element : 4
    print array element : 3
    print array element : 2

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    What is the last and first index of an array with 4 elements ?

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by OldGuy2 View Post
    What is the last and first index of an array with 4 elements ?
    I got it.

    Code:
    #include<stdio.h>int main (void)
    {
        int i;
        int array[4]= {2,2,3,4};
        for(i=3; i>=0; i--)
        {
          printf("print array element : %d \n", array[i]);
        }
     
        return 0;
    
    
    }
    print array element : 4
    print array element : 3
    print array element : 2
    print array element : 2

    How can i find specific element of array like If I want to know which number is 2nd element of array ?
    Last edited by vead; 01-21-2018 at 07:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a random number array
    By Mitzi in forum C Programming
    Replies: 3
    Last Post: 03-06-2017, 02:43 PM
  2. Arrays, how to find if some number is the element of an array.
    By InvariantLoop in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2006, 02:43 AM
  3. Count the number of element in 2D char array
    By alice in forum C Programming
    Replies: 2
    Last Post: 04-24-2004, 12:51 AM
  4. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  5. assigning a rand number to each element in an array
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2002, 01:12 PM

Tags for this Thread