C++/Arrays

Advertisement


Question
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  

Answer
Hi Stacy.

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:
1.Data
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.
5 END

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:  
 while(temp_ptr!=NULL)
  {
     print temp_ptr->data
     temp_ptr=temp_ptr->next, that is, go to next block
   }

3.End


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
#include<iostream.h>
#include<conio.h>

class node   
{public:
  float info;
  node *next;
};

class llist  
{
 protected:
   node *head;
   node*tail;
   int size;
 public:
   llist()
   {
     head=NULL;
     tail=NULL;
     clrscr();
     cout<<"Enter the size of your array ";
     cin>>size;
     add(size);
     }
   void add(int x);
   void display();
};

void llist::add(int size)
{
 int i;
 float item;
 for(i=1;i<=size;i++)
 {
  cout<<"Enter data for slot "<<i<<":";
  cin>>item;
  node *newnode=new node;//dynamically create object and allocate space
  newnode->info = item;  //put data item in that object
  if(head==NULL)
   {
    head=newnode;
    tail=newnode;
    newnode->next=NULL;
    }
  else
    {
    tail->next=newnode;
    tail=newnode;
    newnode->next=NULL;
    }
  }
}


void llist::display()
{
  node *ptr=head;
  while(ptr!=NULL)
  {
    cout<<ptr->info<<" ";
    ptr=ptr->next;
  }
}


void main()
{

llist list;
cout<<endl<<"The contents of your array are: "<<endl;
list.display();
getch();
}

Hope this helps.
Samarth.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Samarth Bartaria

Expertise

I can answer questions about pointers behaviour, their implementations and anamolous behaviour.Also, I speciallize in object oriented design and modelling,polymorphism in C++ and algorithm efficiency. Even questions related to database design,or simple basics about programming are welcome.

Experience

I have been using C++ for five years now for software development and scientific analyses.

Organizations
Currently, I am a student doing my computer engineering.

©2016 About.com. All rights reserved.