3 Replies - 370 Views - Last Post: 01 November 2011 - 07:55 AM Rate Topic: -----

#1 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

C++ Array Import

Posted 31 October 2011 - 08:04 AM

Hey guys,

Sorry I haven't been around for a while, I've been
very busy.

I'm having some issues with a program for my C++ class.
The assignment is to create a templated array class
( Which I have done ) and then to import the array from
a file of about 40,000 words and sort the words using
shell sort, then using a sub-linear search algorithm
( I'll probably use binary-search ) find a user-input
word in the file.

My issue is that it works fine with my smaller test file.
containing a sequence of about 20 3-character strings. But,
when I switch to the large file, it hangs up. After a massive
amount of debugging, I have found that it's hanging up on the
import for some reason and I have no idea why. I will paste
the template class in a spoiler, but it generally works the
same way as standard subscripted arrays with a constructor.
Array<T> ( size_t pSize, const T& value = T() );
Array<T> ();
Array<T> ( const_iterator first, const_iterator last );
Array<T> ( const Array &a );


Spoiler

MAIN:
/******************************************************************************/
// MAIN
int
main( int argc, char * argv[] )
{
        /*******************************************************
        ****                    VARIABLES                   ****
        *******************************************************/
        Array<string> a( 5 );

        /*******************************************************
        ****                      BODY                   ****
        *******************************************************/
        cout << "Importing Words to array... ";
        importWords( a );
        cout << "done.\n";

        cout << "Sorting Array... ";
        //a = shell( a );
        cout << "done.\n";
        //cout << a;

}

bool
importWords( Array<string> &a )
{
        int i = 0;
        string tmp;

        ifstream fp( "./words.txt" );

        if( fp.is_open() )      // Open the file
        {
                /* Are there remaining Lines? */
                while( fp >> tmp && !fp.eof() )
                {
                        // Is it out of bounds?
                        if( i >= (signed)a.size() )
                        {
                                a.push_back( "0" );
                        }

                        /* Populate the array */
                        a[i++] = tmp;
                }
                fp.close();
                return( true );
        }
        return( false );
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Array Import

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ Array Import

Posted 31 October 2011 - 08:22 AM

first off, lines 14-21 are just asking for trouble and it can be replaced with 1 line.

a.push_back(tmp);



second can you post an example file that creates the issue?

edit:
fp >> tmp && !fp.eof()

i just spotted the likely culprit. let's say fp is 1 line from the end of the file when this condition runs. it reads in temp and eof is still false so it enters the loop. on the next loop you read in temp but there is nothing to read in!!

This post has been edited by ishkabible: 31 October 2011 - 08:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: C++ Array Import

Posted 31 October 2011 - 08:41 AM

wow I'm an idiot :P

Unfortunately it's still hanging up :(

This post has been edited by -shadow-: 31 October 2011 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: C++ Array Import

Posted 01 November 2011 - 07:55 AM

fixed the import. My insert method was really inefficient.
I made it a lot better now, but my shell sort is taking a
while now. Yeah, it's 400,000 strings but the teacher told
us to use shell sort so I feel like it should be faster than
that.

Heres my shellsort code:
/******************************************************************************/
// Shell Sort Array
Array<string> shell( Array<string> a )
{
    /*******************************************************
    ****                    VARIABLES                   ****
    *******************************************************/
    int n = a.size(),           /* Size of Array     */
        i = 0,                  /* Loop Controller   */
        h = 0;                  /* Size of each pass */

    /*******************************************************
    ****                        BODY                    ****
    *******************************************************/
    // Before we waste any time, lets see if its already sorted
    if( isSorted( a ) )
    {
        return( a );
    }

    // Get h initial value
    h = 1;
    while( h * 3 + 1 < n )
    {
        h = h * 3 + 1;
    }

    while( h > 0 )
    {
        // Run through the array, decreasing h by 1 + a factor of 3
        for( i = h; i < n; ++i )
        {
            while( a[i] < a[i - h] )
            {
                std::swap( a[i], a[i - h] );
                i -= ( i - h > 0 ) ? 1 : 0;
            }
        }

        // Update h
        h = h / 3;
    }
    return( a );
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1