0

having some trouble with pset3 fifteen.c. Not sure what is wrong. Watched the walkthrough. After the greeting message, nothing appears.

Here is pastebin: http://pastebin.com/Vd48ENBc

Here is check50 link: https://sandbox.cs50.net/checks/fdf47f94e404485bba0bfaeecd06e2bc

Thanks so much! Any help is appreciated!

1 Answer 1

1

You have created an infinite loop through the improper use of the decrement operators. Look at the following:

            // switch numbers 2 and 1
            board[j][i--] = 2;
            board[j--][i - 2] = 1;

Both i and j control the inner and outer loops and are incremented on each pass of their respective loops. In these two operations, the code is trying to access the indexes that are 1 less than i or j. However, the -- operator actually permanently decrements these values. Let's say that when the code gets to these two lines that i=2 and j=2. After these two lines process, i =1 and j = 1. If you put a printf statement after these two lines, you'd see that with d=3, both i and j keep getting set to 1 and the loop runs forever.

Instead of using the -- operator which permanently alters the contents of a variable, you should use i-1. This will be evaluated and the result will only be used for the array index without altering the value of i. It is a transient evaluation that will not reassign a new value to i. Same applies to j.

This addresses your question and this particular problem, but there are more issues that you will need to debug. For starters, the init() function is not initializing the array correctly. But I wouldn't want to deprive you of the fun of debugging it! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .