0

I'm writing a class as an assignment and i am struggling on one function inside of the class. The class is called PowerArray and has, for the purposes of the question, two instance variables, one is an inner array known as data. The other is the count. Data can contain 20, but the count keeps track of the current length of the array. Now that the background is there, on to the question, I am trying to add a character to the front of the array, but I can't seem to get it to work. My current effort is below

  public int addFront(char ch)
  {
    if(count<MAX)
    {
      for(int i = 0; i<count; i++)
      {
        data[i] = data[i+1]; 
      }//for
      data[0] = ch; 
      count++;
      return 0;
    }//if
    else
      return 1;
  } //addFront(char)

P.S: Max is the data array's maximum size, so it's the true size of the array. Also, i'm not worried about accounting for overflow in this case, I simply don't want to preform the function.

1
  • Think about what your array will look like before and after--i.e. what characters are at what indexes. Write something down on paper to help visualize this. Once you've done that, what assignments will you need to perform to turn the "before" into the "after", and in what order? Commented Dec 12, 2013 at 2:26

2 Answers 2

1

It should be:

for (int i = count-1; i >= 0; i--)
{
    data[i+1] = data[i]
}

Right now you are shifting your characters in the wrong direction.

If you initial array were ['a', 'b', 'c', 'd', null, ... ], your for loop would do this:

// count = 4
data[0] = data[1] // 'b'
data[1] = data[2] // 'c'
data[2] = data[3] // 'd'
data[3] = data[4] // null

Leaving your array to be ['b', 'c', 'd', null, ... ].

By iterating in the other direction you'll get what you are looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

You should explain why
0

Since you're shifting everything to the right, you need to start at the right-hand end of the word. Otherwise, if you take abc and start by shifting a to the right, now you have aac, and on your next step, you'll try shifting the second character right... you're thinking it's b, but your first step made it a... so you end up copying the first character over the entire word.

With the assumption that MAX is a constant that will always be equal to the array's size, your code should look like this:

if(count < MAX) {
    for(int i = count; i >= 0; --i) {
        data[i+1] = data[i];
    }
    data[0] = ch;
    count++; //since we added a character to it
    return 0;
} else {
    return 1;
}

As a note unrelated to the problem you're having, would you care to explain why your function is returning an int and returning 0 or 1? Is the return supposed to represent whether or not the characters were shifted? If so, why not return a boolean?

And regardless, either way, 0 evaluates to false (if you evaluate it as a boolean), while 1 would evaluate to true, so it would make sense to swap the returns (return 1 or true if the movement happened, and 0 or false if it didn't).

7 Comments

Regarding the integer return. I have no idea why the professor of this course chose to use an int, but he did. The class was given to me as a skeleton, meaning it had the boiler plate (brackets and method names) in, and what he wanted written above. Onto the answer. Now in my testing (when I print out the array) I am getting nothing. The count is 0, and the array is blank. According to my return, it is working, but im not seeing the results. Your answer makes sense, but I'm not sure what problem it has created.
Then in regards to the return, I'd probably keep in mind my comments about it, and ask your professor about it. In terms of not getting returns, have you stepped through it with a debugger?
I wish I could say I knew how to use a debugger, but I have only used it once or twice, and am in an introductory class. And honestly, if I were doing this myself, I would have probably written this void until I ran into a problem, then switched it to boolean, and once I solved the problem, back to void.
Are you using Eclipse? Can you edit the original question to show how you're calling the method? And where this method exists in code relative to the data array?
Not using Eclipse. Using Dr. Java...LOL. I'll try. Give me a couple of minutes to try and figure this out, I almost have it working. If I can't get it, i'll do as you stated above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.