Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,264 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,504 people online right now. Registration is fast and FREE... Join Now!




Shifting Arrays

 
Reply to this topicStart new topic

Shifting Arrays, adding one, and shifting some to the right

aznballerlee
3 Nov, 2006 - 09:05 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I have a problem with my program .. I want to add a string to an array position, and then
shift the strings after that position to one position after.


prototype: int insert(string a[], int n, string s, int pos, int max);

Here's what my specs say:

1) insert "string s" into array at index "pos".
2) move (n-pos) existing elements starting at "pos"
one space to the rgiht to make room.
3) return element index inserted.
4) return -1 if inserting element would cause array
to have more than max elements.


Bascially, I'm stuck, and I believe my code doesn't work so far.
It would be great if anyone could help me out.

CODE

int insert(string a[], int n, string s, int pos, int max)

int n;
int pos;

if (n-pos > max - 1)
    return -1;

else
    for (int i = pos; i <= n-pos; i++)
    {
        a[pos] = a[pos + 1);
    }

    string s = a[pos];
    return pos;




This post has been edited by aznballerlee: 3 Nov, 2006 - 09:17 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Shifting Arrays
3 Nov, 2006 - 10:32 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 3 Nov, 2006 - 10:05 PM) *

I have a problem with my program .. I want to add a string to an array position, and then
shift the strings after that position to one position after.


prototype: int insert(string a[], int n, string s, int pos, int max);

Here's what my specs say:

1) insert "string s" into array at index "pos".
2) move (n-pos) existing elements starting at "pos"
one space to the rgiht to make room.
3) return element index inserted.
4) return -1 if inserting element would cause array
to have more than max elements.

Hi Az

We all get stuck on different things.

On of the tricks to problem solving is to move the specification list around a little to make it easier to code (provided that the changes don't change the outcome). Specifications are usually written by users that want a specific outcome, they know little about coding and won't know or care if their specification is sequenced for coding the outcome.

The following details must be known
  1. Max_Dimension of Array
  2. Current_Count of stored strings
  3. position to insert
  4. string to insert
Your interface seems to have all these elements, so lets see what you need to check first. You will see that I have moved the sequence the specification to make it easier to code.

The first test should be to ensure the insertion won't exceed max index, ie if ( n +1 > max). I know that I am stating the obvious but when we bury your face in a screen and line sof code, we often loose sight of the logical steps to successfully realise an outcome.

Shuffling the data would have to occur before you can insert, the spec says shuffle to the right, however, that is only the outcome desired, if you think too literally, you may write code that overwrites the strings while shuffling.

Lets look at a simple example using some letters instead of strings
Sample data:
  • auejdwipks
  • Max Size = 12
  • current size = 10


Insertion data 'z' into position 4

This attemp will clearly pass the size test, so we can just work on the way the algorithm should work. Remember that these are C based arrays and start at 0 (offset of -1)

position 4 is the letter j.

If we think like the specification, we can try the idea of shuffling to the right.
copies over d, which copies over d which... oh... that way will obliterate the strings....

Write an exercise based onthis and see if you can determine the right way to do this.. remember.. think outside the square.
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Shifting Arrays
4 Nov, 2006 - 10:04 AM
Post #3

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I was given an example, that shuffling meant overwriting whatever was there before .

ex.
CODE
string h[6] = { "peter", "lois", "meg", "chris", "stewie", "brian" };
int k = insert(h, 3, "quagmire", 1, 6);  // returns 1
    // h is now   "peter"  "quagmire"  "lois"  "meg"  "stewie"  "brian"


But anyways, I tried coding again .. it doesn't work in the for loop ..


CODE

int n;
int pos;

if (n + 1 > max)
    return -1;

else
    for (pos; pos < n; pos++)
    {
        a[pos] = a[pos+1];
    }

    string s = a[pos];
    return pos;



Gregory, I didn't ignore what you wrote .. but I can't seem
to take your approach to solve this problem ..

This post has been edited by aznballerlee: 4 Nov, 2006 - 10:18 AM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Shifting Arrays
4 Nov, 2006 - 08:15 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 4 Nov, 2006 - 11:04 AM) *

Gregory, I didn't ignore what you wrote .. but I can't seem
to take your approach to solve this problem ..


Az

Thanks, I was aiming to have you think the process though a little more. And you look like ur quiet close now.

I'll just go over a few steps using my previous material
Lets look at a simple example using some letters instead of strings
Sample data:
  • auejdwipks
  • Max Size = 12
  • current size = 10
Same scenario, assuming you are trying the first method (way) of shuffling.

CODE
Pass 012345678901
0    auejdwipks
1    auejjwipks
2    auejjjipks

You can see that copying j to the right is not going to work.... but if you try this
CODE

Pass 012345678901
0    auejdwipks
1    auejdwipkss
2    auejdwipkks
etc
N   auejjdwipks

you can see we have made space at position 4, which preserves the original data and enables insertion of the record.

The algorithm is:
point at the next available location (L) (ie the position just past the data)
Loop
copy atr[L-1] to str[L]
move the "pointer" L to L-1
Repeat unitl L = insert point


In your code, you are using pos++.. it should be pos-- that will then match the correct way to shovel the data inside the array without loss.

This post has been edited by gregoryH: 4 Nov, 2006 - 08:15 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:20PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month