Thread: Printing an array in reverse order!

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Printing an array in reverse order!

    I'm pretty new to arrays, and I'm having a slight problem printing one of my arrays in reverse:
    Code:
    #include <stdio.h>
    
    int main(void)
    
    {	int i;
    	int firstarray[7] = {4, 9, -3, 0, 2, 7, 51};
    	int secondarray[5] = {34, -6, 5, 15, -21};
    
    	printf("The First Array is: "); 
    	{
    	for(i=0;i<7;i++)
    		printf("%d ",firstarray[i]);}
    		printf("\n");
    
    
    	printf("The Second Array is: ");
    	{
    	for(i=0;i<5;i++)
    		printf("%d ", secondarray[i]);}
    		printf("\n");
    
    		printf("The First Array in reverse order is: ");
    		{
    		for(i=0;i>=7;i--)
    			printf("%d ", firstarray[i]);
    			printf("\n");
    		}
    
    }
    The array in reverse doesnt print anything at all. Any idea what I'm missing?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Because your condition is wrong.

    Code:
    for(i=0;i>=7;i--)
    notice anything wrong with that?

    If 'i' is set to zero and your condition is that it should be GREATER than or equal to '7' how will it ever run?

    should be:

    Code:
    for(i=6;i>=0;i--)
    hth,

    -Feen

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Quote Originally Posted by Feengur View Post
    Because your condition is wrong.

    Code:
    for(i=0;i>=7;i--)
    notice anything wrong with that?

    If 'i' is set to zero and your condition is that it should be GREATER than or equal to '7' how will it ever run?

    should be:

    Code:
    for(i=6;i>=0;i--)
    hth,

    -Feen
    lmao, wow I feel like an idiot. XD Thanks Feen.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    no problem. It happens all the time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-21-2005, 02:11 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. reverse order - arrays
    By Lillian in forum C Programming
    Replies: 12
    Last Post: 11-03-2002, 07:38 PM
  4. Printing String in reverse order
    By ling in forum C Programming
    Replies: 14
    Last Post: 10-18-2001, 09:03 AM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 02:27 PM