1. Have you tried to print out the values of newX and newY right before the following line:
Code:
```        while (((grid[newX + 1][newY] ||
grid[newX - 1][newY] ||
grid[newX][newY - 1] ||
grid[newX][newY + 1]) != 1)
)```
This is the line causing your segmentation fault. The problem I saw was newX was equal to 9 so 9 + 1 would be out of bounds.

Jim

2. Ooga,

While that code does eliminate all the segmentation errors, I am still facing the massive problem that it does not check if the "adjacent" element horizontally and vertically, has a value equal to 1.

The loop is meant to end as soon as newX and newY are adjacent to a particle with say, a and b co-ordinates. The way I've designed it is by assigning particle(a,b) with a value 1 (aka the element grid[a][b] = 1, and all other elements in **grid = 0. The while loop does a random walk, checking random elements in the grid based on this random walk to see if it does end up next to this particle, and then stops.

3. Yes, but if the "random walk" walks outside valid indices, it accesses or modifies invalid elements. You need to tackle your (cough!) major problem so, when checking "adjacent" elements, you avoid walking outside valid bounds.

That is your little challenge to fix - the fix is trivial. If you knowingly insist on using a broken technique, you cannot complain when the operating system keeps detecting that your program is accessing memory it shouldn't and terminating your program. The operating system is doing its job. You need to do yours.

4. Try this.
Code:
```        while (!(newX < size-1 && grid[newX+1][newY] ||
newX > 0      && grid[newX-1][newY] ||
newY > 0      && grid[newX][newY-1] ||
newY < size-1 && grid[newX][newY+1])) {```

5. Ooga,

It still doesn't work man. Segmentation faults are killing me arghh.

Grumpy,

It may seem "trivial" to you because you have years of experience. However I'm still starting out and have been studying C for one month. It takes me longer to solve problems like this. So stop being an ass.

6. Originally Posted by Bro
Grumpy,

It may seem "trivial" to you because you have years of experience. However I'm still starting out and have been studying C for one month. It takes me longer to solve problems like this. So stop being an ass.
You might struggle a little with solving it, but you will eventually have an "aha!" moment, and realise how trivial it is. You will learn more from that process than you will by getting someone to fix it for you.

If I solve the problem, I will learn nothing and you will learn nothing.

So stop being an ass and get on with it.

7. The newY below should be newX.
Fix that and use the while condition I gave you.
Code:
```            else if(rnd == 2)
{
if(newY == 1)
{
step = step * (-1);
}
newX = newX - (step);
}```
Also, it newX should actually be compared to 0, not 1.

8. Yess! It works now man! Thank you so much for your help Ooga dude. And the rest of you guys for your time.

Code:
```                              *    *
* ****
* *
*   ***
*   * *
*    ******
*      *      *
*     **      *
*      **   * *
**      **  ***
*  **   ** ****
** *      ******   **** *
****           *     *  *         **
*           ****  *            **
*     *       *** *           **
* *****     *  ****   ***      *  *
****** * **********  * ****  *  * *****
*****  *  *   *   * ************* **    *
** *          **   * ** * * **  ******** *
* **          *   *         **        ***
**** *          **   *         *         *
**   *        *****  **        *         *
*   **         * *  *        **
**          *    *     *****
*        ****            ***
*          *             * *
**
***
*
**
*```

10. I just realized that by putting all my source code up here that I might get plagiarized. This was meant to be a final project for one of my modules at college...
Is there any way to remove the sections where I posted my full code and just leave the section of code where I posted the while loop? Otherwise I'm gonna be screwed if everyone starts handing assignments with my code all over it. Please reply moderators as this is kind of urgent, we have severe consequences for any plagiarism. The only reason why I posted the full code is so that everyone who was helping me could get an idea of my initializations of arrays and variables used in my loop, otherwise I just would have posted the loop by itself...

11. Originally Posted by Bro
I just realized that by putting all my source code up here that I might get plagiarized. This was meant to be a final project for one of my modules at college...
Is there any way to remove the sections where I posted my full code and just leave the section of code where I posted the while loop? Otherwise I'm gonna be screwed if everyone starts handing assignments with my code all over it. Please reply moderators as this is kind of urgent, we have severe consequences for any plagiarism. The only reason why I posted the full code is so that everyone who was helping me could get an idea of my initializations of arrays and variables used in my loop, otherwise I just would have posted the loop by itself...
Naah. I suggest that it would be quite reasonable that moderators allow your plagiarism (having effectively got others to complete your homework for you) remain here for all to see. You can accept the consequences of your own action.

You might, incidentally, want to look at this site's homework policy - which you have just openly admitted to having contravened. Look here.

12. If this is really your own code, then there isn't much to worry about. You can always say "look, I published first, so I can't possibly have copied it". Providing the terms of your assignment permitted outside debugging help (like posting on a forum), you should be in the clear.

Deleting things from the board beaks the whole "share and learn" ethos. How many historic posts have you read, and how much have you learnt?

13. The only thing I needed help with was my while loop, not all of my code. I realize now that I shouldn't have posted my full code in the first place, it was pointless and unnecessary . Is there no way you can just keep the while loop which I included by itself in post no.8 and just delete my FULL source code that i've posted? No one is going to learn anything from it and it'd make my life much easier.

14. > The only thing I needed help with was my while loop, not all of my code.
Not so - there were issues all over the place.

For example, whilst your broken memory allocation checks were not responsible for the crash, the fact remains the code was wrong and was correctly commented on. If you ignore that, then you're in the wrong class.

> I realize now that I shouldn't have posted my full code in the first place, it was pointless and unnecessary
That's for us to decide - not you.

Had you only posted the loop, we would simply have asked you to post the whole code anyway. Diagnosing segfaults without ALL the code is a crap-shoot. We've seen plenty of posts where the "fault" code looked just fine, only to find out much later that the author had completely screwed up the memory allocation.

No, I'm not going to delete your posts.

15. Surely you're being a bit of a fascist douche; I created those posts and I've given you good valid reason as to why I don't think they should be kept up. The "problems all over my code" weren't contributing to the problem I was asking help for.

And @grumpy, you should really go and lose your virginity dude, it'd help with your evident emotional problems.

But w/e don't take it down, idc.