Moving Elements in an array

Started by
16 comments, last by rip-off 11 years, 2 months ago


I was thinking a recursive approach of moving one element at a time until we get to the end of the array.

Note that iteration is preferred to recursion in almost all cases. Loops are simpler to reason about in most popular languages. Recursion is most typically used for the kind of problems where you have keep track of where you were. rather than just where you are.

As an example of why you might avoid recursion, recursion relies on the algorithm completing before stack space is exhausted. Some platforms default to a small amount of stack space that doesn't grow (e.g. executables on Windows typically have up to one Megabyte of stack space). Thus your application may now have an unintentional arbitrary limit - once the data size crosses a certain threshold it may crash!

Actually thanks for that. Because I'm not far into my CS degree (mostly a theory class studying ideas of some techniques) the ideas taught aren't always fully explained and just the basics behind them. Although I of course have known about recursion and have used it before (I'm sure a loop would had prolly been better), did know about all that on recursion. Though I did know a little about the runtime on it (did study that a little bit last semester).

Tomorrow I plan to work on this fully to see what I can come up with. Did a little reading and I think I have some ideas. Thanks everyone!!
Advertisement

Code reviewer's dictionary:

Curse (v) \?k?rs\ - To say a bad word.

Recurse (v) \?hwät th? hel\ - To say a bad word more than once.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Code reviewer's dictionary:

Curse (v) \?k?rs\ - To say a bad word.

Recurse (v) \?hwät th? hel\ - To say a bad word more than once.

lol.

Also I talked to my my professor today. She said that I do need to preserve the order. She would like us to move every element after the one you are removing to the left. This is what I figured she would want actually.

Sadly I do not like this project really. Mostly because I don't think I would ever use an array for what she wants us to do (in the class we haven't gotten to other data structs to use so she just describes everything we lean as implementing it as an array). Because of this I am a little lost on what to do exactly on removing the element, then preserving the order by moving every element afterwards to the left.

If I could get some ideas, maybe some pseudo code or something that would stay within the guidelines of this site. I have every other part of the project finished as it was pretty easy, but I guess because I would have never thought of using an array for this I am not very sure how to use arrays to do this.

Thanks for any help.

Pseudocode you say?

remove ith_element(array, i):
while i < array.size - 1 do
{
array = array[i+1]
i = i + 1
}
array.size = array.size - 1

/*cocking formatting bugs on this site*/
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Pseudocode you say?

remove ith_element(array, i):
while i < array.size - 1 do
{
array = array[i+1]
i = i + 1
}
array.size = array.size - 1

/*cocking formatting bugs on this site*/

That was pretty close to what a function that I just started writing after I thought about during my last class this evening. Works great right now.

I just pass in the array, the number of elements by reference, and the element to delete. Everything gets updated perfectly. Thanks everyone!

It's something that you'd get yelled at for professionally, but instructors have to build coursework around manipulating what the class has covered already, so if you've learned arrays and loops then she's going to throw together arbitrary exercises using arrays and loops just to get your fingers moving. It's kind of like practicing katas at the dojo - you endlessly repeat pointless motions so that when it's time to spar the actions are automatic and you can focus on what to do rather than how to do it.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

It's something that you'd get yelled at for professionally, but instructors have to build coursework around manipulating what the class has covered already, so if you've learned arrays and loops then she's going to throw together arbitrary exercises using arrays and loops just to get your fingers moving. It's kind of like practicing katas at the dojo - you endlessly repeat pointless motions so that when it's time to spar the actions are automatic and you can focus on what to do rather than how to do it.

Yea I understand that. The class thought is beyond the first two programming courses you take at a University really. So we all know loops, arrays and all that. The assignment is a first part assignment just to get us going this semester really. I believe we will later take this assignment and move it to use Sets something we are studying now. It's not quite a Data Struct class though we are studying and implementing some basic data structs in a very basic way (sometimes too basic that our programs will actually leak memory as they don't have us go more advanced and have write the remove/delete code for some of these).

Though I should know that what we are doing must help me later on as the University is rated pretty high in Computer Science in the country. Some of it just seems odd to do (I don't like writing some code that is very basic that could self destruct and leak memory).

I don't like writing some code that is very basic that could self destruct and leak memory

Then don't. Write a proper implementation. You will not be penalised for going beyond the call of duty, and you'll learn a lot in the process. That said, get your program working the way the university expects first, and ensure you have a copy of that first (hint: version control is your friend).

This topic is closed to new replies.

Advertisement