Solved

Inserting into an array using pointers

Posted on 2004-10-06
16
227 Views
Last Modified: 2011-04-14
I have a struct:

struct ListArray
{
      int *list;                  
      int listMax;            
      int listCount;            
};

I created a list:

      ListArray zot;
      constructList(&zot, 15);

//function for the construct list
void constructList(ListArray *listx, int maxLength)
{
      (*listx).list = new int[maxLength];
      (*listx).listCount = 0;
      (*listx).listMax = maxLength;
}
// End of contructList

I populated the list like so:
      zot.listCount = 6;
      zot.list[0] = 47;
      zot.list[1] = 99;
      zot.list[2] = 36;
      zot.list[3] = 2;
      zot.list[4] = 15;

Now, I want to create a function, where i can send a value that will get added to this list. It will first scan through the list to see if the list is full, if so it quits. If the list is empty it adds it to the first element. If it is a non empty list (this is where i am stuck!!!) it first finds the insertion point , moves everything down one, inserts the item and adjusts the count.

Any idea how this can be done...
0
Comment
Question by:KalluMama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12242806
Why not use an std::vector or std::list container?
0
 

Author Comment

by:KalluMama
ID: 12242820
it is an assignment and this is the way we gotta do it.....and i am stuck!!!
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242835
You can use std::copy function to move the items.

#include <algorithm>
Example:

std::copy(zot.list + startPos, zot.list + ListSize, zot.list + startPos + 1);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KalluMama
ID: 12242857
can't have not been taught that...
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242875
By using std::copy, you can move the list object if you know StartPos, ListLen, and Quantity to move.

std::copy(zot.list + startPos, zot.list + ListSize, zot.list + startPos + MoveQty);
0
 

Author Comment

by:KalluMama
ID: 12242907
not allowed to use any of that...since we are not supposed to know that...since we have not covered that...
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242914
>>can't have not been taught that...

Then you can do it manually, via a for-loop.

for(int* it = zot.list + startPos; it != zot.list + ListSize - MoveQty; ++i)
 *(it+MoveQty) = (*it);
0
 

Author Comment

by:KalluMama
ID: 12242964
where do startpos listsize and moveqty come from??? not sure we can use these if we have not been taught these....
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242971
startPos should be a zero based index into the array.
MoveQty should be the number of objects you want to move.
ListSize is the size of the Current List.

So for your code it might look like this to insert one item:
for(int* it = zot.list + startPos; it != zot.list + zot.listCount ; ++i)
*(it+1) = (*it);
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242991
>>where do startpos listsize and moveqty come from???

These are variables that you create.
You need to create code to search for the insertion point.
Then make the startPos variable equal to the insertion point.

More then likely, you can startPos will be an int variable or size_t;
int startPos = 3; //Modify this value to the insertion point.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12242998
FYI:
It's against EE policy for me to provide a full and complete answer to a homework question.

We can help you along with existing code, but we can not post a complete answer.
0
 

Author Comment

by:KalluMama
ID: 12243019
i understand...i don't really need the answer..but just a nudge....
0
 

Author Comment

by:KalluMama
ID: 12243162
OK i have figured out how to determine where to insert, now the question is how do i make room for the new insertion??

I have already sorted the list...

      //Find insertion point
      int xx;
      int insertPt = listx->list[0];
      for (xx = 0; xx < listx->listCount; ++xx)
      {
            if (target < listx->list[xx])
            {
            insertPt = listx->list[xx];
                        //Here i know the element  from where i want to make an insertion by shifting the rest of the data down
                  cout << endl << insertPt << endl << xx;    
                  return true;
            }
      }
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 12245419
Example:
   //Find insertion point
     int xx;
     int insertPt = listx->list[0];
     for (xx = 0; xx < listx->listCount; ++xx)
     {
          if (target < listx->list[xx])
          {
            insertPt = listx->list[xx];
                        //Here i know the element  from where i want to make an insertion by shifting the rest of the data down
               cout << endl << insertPt << endl << xx;    
               
               //Now move xx to xx+1 until the end of the list
               for(int* it = zot.list + xx; it != zot.list + zot.listCount ; ++i)
                  *(it+1) = (*it);

               //Now set xx to target
               listx->list[xx] = target;

               return true;
          }
     }
0
 

Author Comment

by:KalluMama
ID: 12253732
I got it to work. The thing was i was not supposed to sort the list, just plug each value in as it comes in, so this is what it did:

/////////////////////////////////////////////////////////////////////////
// Insert an item into the list (ordered)                                             
/////////////////////////////////////////////////////////////////////////
bool insertOrdered(ListArray *listx, int target)
{
      //If list is full insertion fails
      if(listx->listCount == listx->listMax)
            return false;

      //Insert into empty list
      if(!listx->listCount)            
      {
            listx->list[0] = target;
            ++listx->listCount;
            return true;
      }

      //If the list is not empty or full, do this...

      //First, find the insertion point by putting a work pointer variable
      //(insertionPoint) on the first item of the list
      int *insertionPoint;
      int myCount = 0;
      bool insertFound = false;            //if False, then not found, if True then record found

      insertionPoint = listx->list;

      //use this while statement to find the insertion point
      while(myCount <= listx->listCount && insertFound == false)
      {
            if(target >= *insertionPoint)            //insertion point not found
            {
                  ++insertionPoint;                        //move the insertion point to the next element
            }
            else                                                //insertion point found
            {
                  insertFound = true;                        //set this to true since you found the insertion point
                  cout << endl << endl << "Addess: " << insertionPoint << endl << "Contents:" << *insertionPoint;
            }//end if

            ++myCount;                                          //count to see how many elements i have read
      }//end while


      //There are two reasons the loop finished, one coz we reached the end of the list or
      //we found the insertion point
      if (myCount > listx->listCount)                  //if we reached the end of the list then...
      {
            listx->list[listx->listCount] = target; //...append target to the end of it
            ++listx->listCount;
            return true;
      }
      else                                                            //if we found the insertion point...
      {
            int *moveDown;
            moveDown = &listx->list[listx->listCount - 1];

            while(moveDown >= insertionPoint)
            {
                  moveDown[1] = *moveDown;
                  --moveDown;

                  
            } //end while

            *insertionPoint = target;
            //listx->list[listx->listCount - 1] = target;
            ++listx->listCount;
            return true;

      }//end if


      return true;
}
// end of insertOrdered

I called it this way::::

      int target = 0;
      target = 420;
      cout << endl
             << "inserting: "
             << target;
      if(!insertOrdered(&zot, target))
            cout << endl
                   << "List zot: insertion failed";
            cout << endl
             << "list zot";
      displayList(zot);
getch();

      target = 68;
      cout << endl
             << "inserting: "
             << target;
      if(!insertOrdered(&zot, target))
            cout << endl
                   << "List zot: insertion failed";
            cout << endl
             << "list zot";
      displayList(zot);
getch();


Thanks for your help Axter!!!
0
 
LVL 30

Expert Comment

by:Axter
ID: 12255750
Thank you for the A grade :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 279
FMX enumerated colours 2 133
Safe conversion? 4 75
HTTPSendRequest with WinINet delays on first call 11 80
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question