Reverse Print Vector Array help

This is a discussion on Reverse Print Vector Array help within the C++ Programming forums, part of the General Programming Boards category; Hello... I am trying to read the contents of a file into a Vector Array, which works, however I cannot ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    Reverse Print Vector Array help

    Hello... I am trying to read the contents of a file into a Vector Array, which works, however I cannot get the array to print the Array in a reverse order. This is the data that I am reading from:

    Code:
    S 10 10 10 20 22 34
    C -3 44 5 9 -6 11 22 19 44 7 354 -12 231 15
    C 98 124 999 25 288 16 29 43 58 12 11
    S 22 31 16 7 88 1297 41
    This is the actual program:

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <numeric>
    
    using namespace std;
    void CFunc( istream & inf, ofstream & outf )
    {
            int i, mean = 0, count = 0, sum = 0;
            vector<int> V(1,0);
            for( i = 0; i < V.size(); i++ )
            {
                    cout << " ";
                    while(inf.peek() != '\n')
                    {
                            inf >> V[i];
                            V.push_back(i);
                            //cout << V[i] << " "; //Reading from here works with cout.
                            sum = sum + V[i];
                            count++;
                    }
            }
    
            //cout << "vsize =" << V.size() - 1 << endl;
            //i = V.size() - 1; cout << "i = " << i << endl;
            for( i = V.size() - 1; i >= 0; i-- ) // This is the loop that I am using to try to read the V vector array in reverse order.  
            {
                  cout << V[i] << " "; 
            }
    // The result of this produces Zeros for some reason.
            mean = sum / count;
    
            cout << endl << " Mean = " <<  mean
                    << " " << " Count = " << count << endl;
            outf << endl << " Mean = " << mean
                    << " " << " Count = " << count << endl;
            cout << endl;
    }
    
    void Scramble( istream & inf, ofstream & outf )
    {
            int i;
            char aLine[60];
            inf.getline(aLine, 60);
            cout << " Original Line: " << aLine << endl;
            outf << " Original Line: " << aLine << endl;
            for( i = 0; i <= 59; i++ )
            {
                    if( aLine[i] == '\n')
                    {
                            outf << " Scrambled Line: " << aLine << endl;
                            cout << " Scrambled Line: " << aLine << endl;
                            cout << endl;
                            break;
                    }
                    if( aLine[i] == '0' || aLine[i] == '1')
                            aLine[i] = 'A';
                    if( aLine[i] == '2' || aLine[i] == '3' || aLine[i] == '4')
                            aLine[i] = 'B';
                    if( aLine[i] == '5' || aLine[i] == '6' || aLine[i] == '7')
                            aLine[i] = 'C';
                    if( aLine[i] == '8' || aLine[i] == '9')
                            aLine[i] = 'D';
            }
    }
    
    int main ()
    {
            ifstream infile("lines.txt");
            ofstream data("data.txt");
            while(!infile.eof())
            {
    char x;
                    infile >> x;
                    if( x == 'S' )
                            Scramble( infile, data);
                    if( x == 'C' )
                            CFunc( infile, data );
            }
            data.close();
            infile.close();
            return (0);
    }
    Any help would be greatly appreciated.

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    reverse (STL Sample)

    The sample code below illustrates how to use the reverse STL function in Visual C++.

    Required Header:
    <algorithm>

    Prototype:


    template<class BidirectionalIterator> inline
    void reverse(BidirectionalIterator first, BidirectionalIterator last)


    Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

    Description:
    The reverse algorithm reverses the order of the elements in the range [first, last).

    Sample Code:

    //////////////////////////////////////////////////////////////////////
    //
    //
    // reverse.cpp : Illustrates how to use the reverse function.
    //
    // Functions:
    //
    // reverse - Reverse the items in a sequence.
    //
    //////////////////////////////////////////////////////////////////////

    // disable warning C4786: symbol greater than 255 character,
    // okay to ignore
    #pragma warning(disable: 4786)

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <functional>

    using namespace std;


    void main()
    {
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of strings
    typedef vector<string > StrVector ;

    //Define an iterator for template class vector of strings
    typedef StrVector::iterator StrVectorIt ;

    StrVector Tongue_Twister(VECTOR_SIZE) ;

    StrVectorIt start, end, it ;

    start = Tongue_Twister.begin() ; // location of first
    // element of Tongue_Twister

    end = Tongue_Twister.end() ; // one past the location last
    // element of Tongue_Twister

    //Initialize vector Tongue_Twister
    Tongue_Twister[0] = "she" ;
    Tongue_Twister[1] = "sells" ;
    Tongue_Twister[2] = "sea" ;
    Tongue_Twister[3] = "shells" ;
    Tongue_Twister[4] = "by";
    Tongue_Twister[5] = "the";
    Tongue_Twister[6] = "sea" ;
    Tongue_Twister[7] = "shore" ;

    cout << "Before calling reverse \n" << endl ;

    // print content of Tongue_Twister
    cout << "Try this Tongue Twister: " ;
    for(it = start; it != end; it++)
    cout << *it << " " ;
    cout << "\n\n" ;

    // reverse the items in the vector Tongue_Twister
    reverse(start, end) ;

    cout << "After calling reverse \n" << endl ;

    // print content of Tongue_Twister
    cout << "Now try the reversed Tongue Twister: " ;
    for(it = start; it != end; it++)
    cout << *it << " " ;
    cout << "\n\n" ;

    }


    Program Output is:

    Before calling reverse

    Try this Tongue Twister: she sells sea shells by the sea shore

    After calling reverse

    Now try the reversed Tongue Twister: shore sea the by shells sea sells she

    I believe the swap() could also be a good help to you.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    Well that is nice of course however is there a simple way to do it with a for loop while decrementing the value of V[i]? Also why is my array empty after it leaves the initial loop? are vector arrays scope dependant?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 12:41 PM
  2. i want to print my linked list in the reverse order please help
    By raghu_equinox in forum C Programming
    Replies: 9
    Last Post: 10-14-2006, 12:45 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM

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