reset password
Author Message
disrael
Posts: 44
Posted 22:01 Jan 17, 2015 |

I'm working on Lab2 Problem 1 Method5 where the goal is to move all the evens to the front of the array list and just cant seem to get it right. 

I'm initializing a copy of the array, loading the copy with the original array's values, running a for statement to set an int temp= to the current array value, checking in an if statement if its even, if it is resetting the [0] to the current array value, then running a for loop to push all values up by one.

Maybe, my logic is wrong on this. Any insight would help. I've been trying at it for hours now and just going in circles.

easauceda
Posts: 3
Posted 22:41 Jan 17, 2015 |

I went about it by taking pulling out all the evens first, and then bringing in the rest of the numbers behind it. That way you don't have to keep shifting the array

I hope this helps

Last edited by easauceda at 22:43 Jan 17, 2015.
disrael
Posts: 44
Posted 00:18 Jan 18, 2015 |
easauceda wrote:

I went about it by taking pulling out all the evens first, and then bringing in the rest of the numbers behind it. That way you don't have to keep shifting the array

I hope this helps

I tried doing that. I ran a for statement to load the copy of the array but only with evens. I then did another for loop that started off with the counter so it would start at the right value, but still be less than SIZE. Its not working at all. My odd numbers are just returning random memory.

raywu64
Posts: 44
Posted 09:16 Jan 18, 2015 |

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array 

- print the evens

- traverse the array

- print the odds

jpascua
Posts: 197
Posted 12:55 Jan 18, 2015 |
raywu64 wrote:

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array 

- print the evens

- traverse the array

- print the odds

But doesn't this sort of kill the purpose of the assignment since we're supposed to manipulate with the array?

jpascua
Posts: 197
Posted 13:01 Jan 18, 2015 |

What I did was iterate through the array and look for an even number. If it's even, check if the left adjacent number is odd and swap. Keep doing this until it reaches the front of the array. This procedure goes on with the rest of the even numbers; preserving the order.

This is the second algorithm I came up with and decided to settle with. My first algorithm was more like Sauceda's.

Last edited by jpascua at 13:02 Jan 18, 2015.
raywu64
Posts: 44
Posted 15:11 Jan 18, 2015 |
jpascua wrote:
raywu64 wrote:

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array 

- print the evens

- traverse the array

- print the odds

But doesn't this sort of kill the purpose of the assignment since we're supposed to manipulate with the array?

I don't think so, the point is to simply print out the result of a certain operation for the users. I THINK traversing twice is more efficient than creating another Array. 

 

Adam Berman
Posts: 19
Posted 10:37 Jan 21, 2015 |
raywu64 wrote:
jpascua wrote:
raywu64 wrote:

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array

- print the evens

- traverse the array

- print the odds

But doesn't this sort of kill the purpose of the assignment since we're supposed to manipulate with the array?

I don't think so, the point is to simply print out the result of a certain operation for the users. I THINK traversing twice is more efficient than creating another Array.

 

My understanding was that he wanted our methods to return a new array that's been changed in the way requested; with that assumption in mind, I

1) traversed the array, splitting the evens and odds into 2 arrays and incrementing a counter to track how many went into each array

2) went through the odd array and stuck it on the end of the even array

3) returned the even array

raywu64
Posts: 44
Posted 14:54 Jan 21, 2015 |
Adam Berman wrote:
raywu64 wrote:
jpascua wrote:
raywu64 wrote:

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array

- print the evens

- traverse the array

- print the odds

But doesn't this sort of kill the purpose of the assignment since we're supposed to manipulate with the array?

I don't think so, the point is to simply print out the result of a certain operation for the users. I THINK traversing twice is more efficient than creating another Array.

 

My understanding was that he wanted our methods to return a new array that's been changed in the way requested; with that assumption in mind, I

1) traversed the array, splitting the evens and odds into 2 arrays and incrementing a counter to track how many went into each array

2) went through the odd array and stuck it on the end of the even array

3) returned the even array

Correct me if I'm horribly mistaken but, I thought we couldn't return static arrays. 

Adam Berman
Posts: 19
Posted 16:01 Jan 21, 2015 |
raywu64 wrote:
Adam Berman wrote:
raywu64 wrote:
jpascua wrote:
raywu64 wrote:

You don't really have to make new arrays, you just need to show the user the list if evens came first. Another way to tackle this would to simple be :

-  traverse the array

- print the evens

- traverse the array

- print the odds

But doesn't this sort of kill the purpose of the assignment since we're supposed to manipulate with the array?

I don't think so, the point is to simply print out the result of a certain operation for the users. I THINK traversing twice is more efficient than creating another Array.

 

My understanding was that he wanted our methods to return a new array that's been changed in the way requested; with that assumption in mind, I

1) traversed the array, splitting the evens and odds into 2 arrays and incrementing a counter to track how many went into each array

2) went through the odd array and stuck it on the end of the even array

3) returned the even array

Correct me if I'm horribly mistaken but, I thought we couldn't return static arrays. 


You're correct; however you can return pointers to heap-allocated arrays ("int * func() {int *a = new int[10]; return a;}") and the returned pointer can be used like any other array. You have to be careful with heap space because you can end up filling it up without deleting anything, which is much easier to do accidentally than with stack space ("int *a = {}" you 'could' return this pointer but the space it points to would be reused by other scopes and not protected from change) I ended up doing it by returning heap arrays and passing them to a printarray function, because printing inside the function seemed vaguely ridiculous to me, but maybe that was the intended solution. Also, these forums are awful on mobile.
Last edited by Adam Berman at 16:01 Jan 21, 2015.