Results 1 to 3 of 3

Thread: Reversing an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    23

    Reversing an array

    I need to find out how to reverse an array.. here is the code

    // REVERSE.CPP
    //
    // This program reverses the order of the elements in an array.
    //
    // Author: Leon Noel

    #include <iostream.h>
    #include "apvector.h"

    void Reverse(apvector<int> &a);

    int main()

    {
    apvector<int> test(6);
    int i;

    // Set the test values in the array:
    test[0] = 1;
    test[1] = 1;
    test[2] = 2;
    test[3] = 3;
    test[4] = 5;
    test[5] = 8;

    // reverse the array:
    Reverse(test);

    // Display the array:
    for (i = 0; i < 6; i++)
    cout << test[i] << ' ';
    cout << endl;

    return 0;
    }

    void Reverse(apvector<int> &a)

    // Reverses the elements of the array

    {
    int i, ...;

    while {...}
    }
    }

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Location
    Left past the postbox
    Posts
    30

    Reply

    Here is some code I quickly wrote in notepad, I HAVENT compiled this code yet.

    Code:
    const arrlimit = 6;
    
    void Reverse(apvector<int> &a,apvector<int> &tmp)
    {
    
    	int x = 0;
    
    	for (int i=arrlimit;i>0;i--)
    	{
    		a[i] = tmp[x];
    		x++;
    	}
    
    
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    for (int i=arrlimit-1;i>=0;i--)


    And in case you're allowed to use proper C++ (many courses don't) here's what to do:
    Code:
    #include <vector>
    #include <algorithm>
    using std::vector;
    
    template<typename T>
    inline void Reverse(vector<T> &v) {
      std::reverse(v.begin(), v.end());
    }
    And to put it parksie's way:
    If your teacher doesn't like it send him/her here and we'll deal with it. The ap classes are evil and any course that thinks it is any good shouldn't use them. STL templates are the way to go.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width