Thread: How can I output an array backwards?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    18

    How can I output an array backwards?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
     int  number[6],n;
    
    
    	for (n=0; n<6; n++)
    	{
    	printf("\nEnter a number ");
    	scanf("%d",&number[n]);
    	printf("%d",number[n]);
    
    
     		if (number[n]==26)
     		{
     		printf("\nFound 26!");
     		break;
     		}
     	}
    
    
     return 0;
    }
    I need to input a list of six numbers into an array and output them backwards and if the number 26 is found, the values should stop printing.


    Well I did everything except I do not know how to output them backwards. Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well I did everything except I do not know how to output them backwards.
    Well, let's look at the code you already have.

    Code:
    for (n=0; n<6; n++)
    So you start at "n = 0".
    Then you increment 'n'.
    And continue the loop until "n < 6".

    So ... to print backwards ... perhaps you should start at the "end" and continue until the beginning.

    And if you break out of your reading loop earlier than expected (i.e. you come across the value 26), you need some way to keep track of where the "end" is.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by Matticus View Post
    Well, let's look at the code you already have.

    Code:
    for (n=0; n<6; n++)
    So you start at "n = 0".
    Then you increment 'n'.
    And continue the loop until "n < 6".

    So ... to print backwards ... perhaps you should start at the "end" and continue until the beginning.

    And if you break out of your reading loop earlier than expected (i.e. you come across the value 26), you need some way to keep track of where the "end" is.
    So from what I am trying to understand, it has to be in this format?
    Code:
    for (n=6; n>6; n--)
    {
    printf("%d",number[n]);
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Recall that arrays start at zero, and go to N-1. So for your declaration of "number[6]", the valid indices are 0 - 5.

    Code:
    for (n=6; n>6; n--)
    So trying to access "number[6]" is out of bounds (hint: start at 5).
    If you start "n" at 6 and decrease it, when is "n>6" ever true?
    You're on the right track here.

    Perhaps you should think about it a bit more, and try it out yourself in code. See what happens, see if you can figure it out, and ask specifically if you get stuck (and be sure to post updated code).

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by Matticus View Post
    Recall that arrays start at zero, and go to N-1. So for your declaration of "number[6]", the valid indices are 0 - 5.

    Code:
    for (n=6; n>6; n--)
    So trying to access "number[6]" is out of bounds (hint: start at 5).
    If you start "n" at 6 and decrease it, when is "n>6" ever true?
    You're on the right track here.

    Perhaps you should think about it a bit more, and try it out yourself in code. See what happens, see if you can figure it out, and ask specifically if you get stuck (and be sure to post updated code).

    THANK YOU! I got it working!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie? printing an array backwards
    By d2rover in forum C Programming
    Replies: 30
    Last Post: 09-21-2010, 11:39 AM
  2. Backwards uint16_t?
    By DavidDobson in forum C Programming
    Replies: 7
    Last Post: 05-11-2009, 09:45 AM
  3. Backwards
    By cyberCLoWn in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2004, 08:11 AM
  4. Backwards Up-Down
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 11-08-2003, 05:56 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM