CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Oct 2010
    Posts
    11

    array-based list question

    Hello,

    I'm trying to do the following. It's a code that corrects a sentence. That's one of the input files:

    Code:
    delete("is",2)
    print 1:This 2:is 3:an 4:an 5:icorrect 6:sntence
    delete("an",3)
    print 1:This 2:is 3:an 4:icorrect 5:sntence
    delete("icorrect",4)
    print 1:This 2:is 3:an 4:sntence
    insert("incorrect",4)
    print 1:This 2:is 3:an 4:incorrect 5:sntence
    delete("sntence",5)
    insert("sentence",5)
    print 1:This 2:is 3:an 4:incorrect 5:sentence
    neighbors("is") 2:is previous:This next:an
    So, first it's "This is is an an icorrect sntence", and after the code has sorted everything, it's gonna be "This is a correct sentence".

    Because I don't know how large the input, I thought I would use this to get the input:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
    }
    Now I don't know how to create the array and populate it. Will this work?

    Code:
    template <class elemType>
    bool arrayListType<elemType>::isempty() const
    {
    return (length == 0);
    }
    template <class elemType>
    bool arrayListType::isFull() const
    {
    return (length == maxSize);
    }
    
    template <class elemType>
    int arrayListType<elemType>::listSize() const
    {
    return lenght;
    }
    template <class elemType>
    int arrayListType<elemType>::maxListSize() const
    {
    return maxSize;
    }
    
    Any help/input is appreciated! Thank you

  2. #2
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    Hm, nobody knows? Maybe my question wasn't clear. If so, just let me know

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: array-based list question

    Not really clear, no. What is arrayListType and why can't you just use a std::vector or std::list?

  4. #4
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    That's just the name, and I'm not sure if I can. I think I have to do it this way. I'm just having troubles populating the array.
    I guess I would have to make dynamic memory allocation. Any idea how?

  5. #5
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    Can't edit it anymore. Anyway, it's supposed to be array-based lists/linked lists. Wanted to add that.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: array-based list question

    "Array-based linked list" is a contradiction in terms. Arrays and linked lists are fundamentally different.

    I suppose you could implement a linked list using an array to "allocate" the memory, but that's overly complicated for a school assignment.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: array-based list question

    I don't see how an array or linked list fits into this at all except that you might have a string which is an array of characters. The STL provides all sorts of containers such as std::list, std:eque, std::string, and so forth. I would be more concerned about your correction algorithm. I don't really understand your basic requirements at all. How does the program know what a correct sentence is?

  8. #8
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    The commands are in the input file, as you can see in my 1st post.

    I guess I could load the string, read each line command by command and perform the actions accordingly?

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: array-based list question

    Quote Originally Posted by TheTrace View Post
    The commands are in the input file, as you can see in my 1st post.

    I guess I could load the string, read each line command by command and perform the actions accordingly?
    Sure, you could. What does that have to do with an array based list? It is a really strange looking input file. The last line doesn't make any sense at all based on what you indicated that the final answer is supposed to be.

  10. #10
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    It needs to sort it. And I thought I could use an array-based list to solve it. But I now think linked lists are the way to go.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: array-based list question

    So just load the elements into a std::vector and call std::sort....

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: array-based list question

    I'm just curious to what an array based list is? Is that some sort of variation of the classic deque implementation? Or a fancy way of saying vector?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  13. #13
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: array-based list question

    Quote Originally Posted by monarch_dodra View Post
    I'm just curious to what an array based list is? Is that some sort of variation of the classic deque implementation? Or a fancy way of saying vector?
    There are benefits for having an array in a linked-list. For starters, it is much easier to manage pointers, implementing reference count such as weak reference that would be difficult without it, and the allocation strategy is more flexible and easier to maintain. I use this approach frequently in a client-policy based design.

  14. #14
    Join Date
    Oct 2010
    Posts
    11

    Re: array-based list question

    Ok, nevermind. Sorry. It's a linked lsit that has to be used. I think fstream would be the best option. I don't know how to load the input into the linked list though. Would be nice if someone could show me how to do this.

    Thank you

  15. #15
    Join Date
    Sep 2010
    Posts
    31

    Re: array-based list question

    Now what do you mean, how to put things into std::list or how to write your own implementation of singly/doubly linked list?

Page 1 of 2 12 LastLast

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

Featured