C++ array-based lists/ sentence editor

Help with array

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 5119 Views - Last Post: 08 October 2010 - 02:21 PM Rate Topic: -----

#1 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

C++ array-based lists/ sentence editor

Posted 06 October 2010 - 09:16 AM

It's a simple sentence editor that sorts the input and the output will be the sentence in the correct order.

Like, the sentence of the input file is: This is is an an icorrect sntence.

 
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



The output would be: This is a correct sentence.


I'm not sure how to read the info from the text file and populate the array. That's what I have so far...

#include <iostream>
#include <fstream>
using namespace std;

[code]
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}


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;
}



So, it's a flexibile array since I don't know how large the input will be. How do I populate the array here?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ array-based lists/ sentence editor

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 09:47 AM

look into c++ strings, make a vector(or deque) of strings and read in each line with getline(some_string,some_file). then push back the string onto a vector. then you can parse each string to get the desired results.

edit: if you want to make your own data structure, witch seems to be where you where headed, copying a vector or deque and a string class would be ideal here.

This post has been edited by ishkabible: 06 October 2010 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 09:52 AM

It needs to be an array-based list, and I thought the way I'm trying to do it here is ok. I'm just having troubles working the array. So, you mean using strings to populate the array is better?

This post has been edited by StarTech: 06 October 2010 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 10:06 AM

im saying that you should use stl data types as much as possible over you types, if you have to make an array based list look at deque's. the basic idea of a dynamic array is that you dynamically allocate space and copy over the current data to the new space allocated. if you can use std::list<string> it would be best (although deque offers same functional and in my experience is much faster than list).

edit: im not very clear on what you are tying to do. i think your tying to make a program that stores an array of lines and and interpret them to preform some type of operations on a data type. this is know as an interpreter. you will need to use lexical analysis for this to be achieved. input each line into an array of strings, tokenize these strings, then based of the tokens do something. usually the next step is compiling to a lower level target code but that is mostly unnecessary for this.

This post has been edited by ishkabible: 06 October 2010 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 10:09 AM

I can't use STL. Not supposed to.
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 10:21 AM

then make your own using the dynamic memory allocation principles you where thought in class. the new and delete key words come to mind, no? start by making two classes to handle your data, a string class and an array based list class to store these strings. you have started to do this obviously. the things you will want in a list are

*push onto back
*push onto front
*pop of back
*pop of front
*list size
*list is empty

push back is simple enough so i will give you the basic idea of how to do it.

1) make a new ptr to point to current contents
2) allocate new space to handle one more element and make the array point point to it
3) copy over elements from old array to new array for each element in old array
4) add value to be pushed on to list at the back of the array
5) adjust size keeper to correctly represent the current size of the array
Was This Post Helpful? 0
  • +
  • -

#7 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 11:43 AM

So, I just have to modify/add what I have there and that's gonna be fine?

Just saw you asked something else in the other post.

The input is a regular text file, where each line is terminated with an end-of-line character(s). Each line will contain a list of words. There are ”insert”, ”delete”, ”print” and ”neighbor” commands to manipulate the list. ”insert” and delete” have the word and position as input parameters. Word positions may change after a word is inserted or deleted. If the position given is invalid then the sentence should be unchanged. Neighbor” prints the previous and next elements for an input word; if there are multiple occurences of the word it is OK to stop at the first occurerrence, but you can optionally detect and print neighbors for all occurrences.
It just needs to perform those things in order to get the sentence in the right order.


Maybe you can use the fstream class to read in the sentence
in that text file. After reading in the sentence, you could write a function to
split that sentence into an array. Afterwards, you can go through
that array and adding it to the Linked List word by words and then writing
the rest of the program based off of the linked list. The array is just
for storage for the words taken in by the fstream ?

This post has been edited by StarTech: 06 October 2010 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 02:02 PM

(huge)edit: I'm making this too complicated, i am looking at this from a simpler compiler making perspective witch is not needed

*load the string
*read the file, command by command, if print print the list, if insert read in the arguments and preform the action directly, if delete preform the action directly, if neighbor read the arguments and preform the action

much simpler and is what your teacher intended i believe.

you would read in insert like so

file>>some_char>>some_string>>some_char>>some_int>>some_char>>some_char


the some_char is useless and is just used to skip over things you don't need. some_string is the string you need to insert and some_index is the place to insert it.

This post has been edited by ishkabible: 06 October 2010 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 02:52 PM

View Postishkabible, on 06 October 2010 - 01:02 PM, said:

*read the file, command by command, if print print the list, if insert read in the arguments and preform the action directly, if delete preform the action directly, if neighbor read the arguments and preform the action



Thank you. But I'm not getting what you're saying here. So, have it search for print, insert delete and have it perform it accordingly to those arguments? So, if there's a delete, I delete it. If there's a print, I do nothing but print it etc. ?
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 03:24 PM

I think ishkabible has misinterpreted the assignment completely. There are only strings to handle. There is no interpretation and commands. This is not a compiler or interpreter. There is no parsing for various datatypes either...

So the big challenge here is the data structure. You know you have a sequence of words (characters separated by a space). You need a data structure that can store:

- required: a collection, of a size not known until runtime
- required: insertion, desirable: fast insertion anywhere within the sequence
- required: deletion, desirable: fast anywhere within the sequence
- required: random element modification, desirable: fast modification anywhere within the sequence

There's no data structure that has every desirable characteristic possible. But you can evaluate various tradeoffs, and based on your priorities, make good decisions.

Let's consider what won't work. A primitive array will not work. Here's why it won't work. Primitive arrays require fixed sizes. You can dynamically allocate an array, at runtime. But you can't increase the size of the array. Let's say you have an array to hold 10 strings. You need to add an 11th string. There's no way to do it with that array. YOu need to dynamically create a new array large enough to hold 11 strings.

You need a more complex data structure. There is no one right answer. One thing you could do is create a datatype that automatically manages an array dynamically. So let's go back to that fixed array example, with 10 elements. YOu want to add an 11th string. So you would allocate an array of more elements. Maybe 20. Maybe 15. Some factor more. Let's say you wanted to add this new string in the 5th position. You would then copy over the first 4 strings from the old array. Then write in this new string at the 5th position. Then copy over the remaining strings from position 6 onwards...

You could also use a linked list. Research that if you don't know what it is. If you study the properties of a linked list, you should recognize that it allows for very fast insertion and deletion.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 03:31 PM

It looks to me like the OP *has* a data structure to be used:

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;
}



Unless *I* am misinterpreting as well.
Was This Post Helpful? 0
  • +
  • -

#12 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 03:47 PM

Ok, linked list sounds ok. I have to look into it again though. Just covered it this week. So the linked list would take care of all the processing?
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 03:54 PM

"Just covered it this week" Then this is probably what you are expected to use!?

"So the linked list would take care of all the processing?" What do you mean "take care of all the processing"?
Was This Post Helpful? 0
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 04:23 PM

i think we are going to need a more clear description of the assignment if, are you to read the file that has the insert, neighbor, delete, print functions? if not then i completely misunderstood the assignment and apologize for any confusion i may have bought you. :unsure:
Was This Post Helpful? 0
  • +
  • -

#15 StarTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-October 10

Re: C++ array-based lists/ sentence editor

Posted 06 October 2010 - 04:38 PM

Don't worry. any help is appreciated,ishkabible!I posted an example of the input file in my first post. It gets the input from that file. Something needs to sort the input file to get the correct output. A linked list allows me to allocate a new node, insert a new element etc. I just don't see how I would do the processing part of the input file. By that, I mean if you break your code into 3 parts, you have input, processing, output. The processing part would be the part that sorts the input etc.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2