reverse an array - help appreciated

This is a discussion on reverse an array - help appreciated within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to figure out to to print an array in reverse, but I'm hiving some troube with the ...

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question reverse an array - help appreciated

    Hello,

    I'm trying to figure out to to print an array in reverse, but I'm hiving some troube with the for loop, I think. I might be somethng else, but I'm not sure. Can someone tell me what I've done wrong?

    Thanks,
    Vireyda

    (Parital code only)
    Code:
    #include <stdio.h>
    void Fill(int B[], int &num);
    void Dump(int C[], int num);
    void Reverse(int D[], int num);
    
    main()
    	{
    	int Array[10], n=0;
    	Fill(Array, n);
    	Dump(Array,n);
    	Reverse(Array, n);
    	}
    
    void Reverse(int D[], int num)
    	{
    	int i;
    	printf("The elements listed in reverse order are:\n");
    	for (i=num; i<0; --i)
    	printf("\t%d\n", D[i]);
    	}

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> for (i=num; i<0; --i)

    "Initialize i to num; While i is less than 0 do loop; decrement i"

    So you want to execute the loop "while i is less than 0"?

    Also remember that when you declare an array of size 10, that means you can access elements 0 through 9, not 10.

    gg

  3. #3
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Thanks for the help. That clarifies so many problems for me.

    Vireyda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Help with Reverse Array
    By Crcullen3916 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2007, 08:47 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21