I'm trying to create an array of floating point in a class. The constructor should accept an interger argument and dynamically allocate the array to hold that many number. The problem I'm having is I'm not sure how to access the number in the array. I want to be able to show the entire content and print out one number at a time.I'm doing this object orient. could you give me an example of how this will work
but this is outside of my expertise
I am providing you with a rough program of how this should work.A lot of theory goes along with the program. I will try to cover it here.
I have used a dynamically created link list.A link list has pointers pointing from one memory location to another.You keep record of the first and the last pointer only.(head and tail in the program).Thus a chain like structure is formed connecting data blocks.pointer head points to first block of data.All blocks have two things:
2.pointer to next block
Now I can use pointer in this location to access next block which again contains the above two things.The last block is also pointed to by tail pointer. Tail pointer is used when creating the link list.The algorithm for creating a link list is like this:
1. declare head and tail pointer.initiallize them to NULL.
2. Input data.Dynamically create space for data block and fill the data field(the other being the pointer field) with the data inputted.Make the pointer field NULL
3. If this is the first data block,head should point to it.
If head==NULL, this has to be the first data block. make head point to this block.Also make tail point to it.
If head is not equal to NULL, this is not the first data block.We want to place it after the last data block of the list we have created so far. The last block of list created so far is pointed to by tail. therefore, set next field of tail block(which was NULL till now) to point to this new block.(tail->next=new block);
Now the new data block is the last one.So set tail to point to this new data block.The last block will have next field as NULL.
4 repeat step 3 for more data blocks.
For accessing data, use the following algorithm:
1. Make a pointer,say temp_ptr point to where head is pointing(the first block).
That is, temp_ptr=head. This is because we dont want to disturb head. Now we will use temp_ptr.
2.if temp_ptr->next is NULL, temp_ptr is pointing to the last block.
Now if we say, temp_ptr=temp_ptr->next, temp_ptr will also become NULL; (next field of last block is NULL)
So we can do the following to access data:
temp_ptr=temp_ptr->next, that is, go to next block
I have used two classes. The first one is the node class.This stores the data block with the 2 fields for data and next.
The other class, llist stores the head and the tail pointer,size of array and functions for accessing list.
//A program to create link list
cout<<"Enter the size of your array ";
void add(int x);
void llist::add(int size)
cout<<"Enter data for slot "<<i<<":";
node *newnode=new node;//dynamically create object and allocate space
newnode->info = item; //put data item in that object
cout<<endl<<"The contents of your array are: "<<endl;
Hope this helps.