so I decided to write a boggle board generator, and it doesn't exactly work.

the problem is that it prints a 4x3 grid... what....

also in the place where i have used[thenum] check, used[thenum] is always 1 even at the very beginning of the program. i initialize it to all zeros.

any ideas?

ps don't bug me about using rand() this is just a PoC

also: i use mac 10.8 xcode


Code:
//
//  main.c
//  boggle
//
//  Created by Ethan ________ on 8/11/13.
//  Copyright (c) 2013 Ethan _________. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char * argv[])
{
    char *cubes[16] = {
        "FORIXB", "MOqABJ", "GURILW", "SETUPL",
        "CMPDAE", "ACITAO", "SLCRAE", "ROMASH",
        "NODESW", "HEFIYE", "ONUDTK", "TEVIGN",
        "ANEDVZ", "PINESH", "ABILYT", "GKYLEU" };
    int i;
    int used[16] =
        {0,0,0,0,
        0,0,0,0,
        0,0,0,0,
        0,0,0,0};
    int thenum;
    /* Intializes random number generator */
    srand((unsigned) time(NULL));
    for (i = 1; i <= 16; i++) {
        // 15 because array starts at 0
        do {
            thenum = rand() % 15;
        } while (used[thenum] == 1);
        char letter = cubes[thenum][rand() % 5];
        used[thenum] = 1;
        if (letter == 'q') {
            printf("Qu\t");
        } else {
            printf("%c\t", letter);
        }
        if (!(i % 4)) {
            printf("\n");
        }
    }
    return 0;
}
First of all, your rand() mod for thenum should be 16, not 15. modding with 16 will give you values 0 through 15, which is what you need. Secondly, your i loop should go from i=0 to i<16, then change your printf conditional to "!((i+1) % 4)".
On this line
Code:
            thenum = rand() % 15;
Change 15 to 16.

The reason is that, when you modulus 15, you only get [0, 14] which means that by the time you reach the very last number, every spot of your array is filled with 1s. Imagine the array was smaller, and you had only 3 spots, and your modulus was by 2. That would mean the array has used[0], used[1], and used[2]. And the modulus would give you either 0 or 1. The first two runs would fill used[0] and used[1] (maybe not in that order), but by the last run, you would run out of space because modulus will never give you an index that will reach the last spot of the array.

Basically, it gets caught in an infinite loop since it never finds a location that hasn't been used.

(Sidenote: Why do you use "Qu" instead of "q"?)

Edit: Since Kerm ninja'd me, regarding his post, I don't think your loop is "wrong" the way it is. It confused me for a while why you would start from 1 instead of 0, but seeing the different logic you need for line breaks (which is really the only thing "i" does), I think it makes better sense to keep it the way it is. That said, you might want to add a comment explaining that that is the reason why you start at 1. (Of course, that's just my opinion on it)
Thanks, both of you. I'd forgotten that about modulus.

Also, in boggle, Qu is a single dice ( die? Lol).

I'll fix this when I get back to a computer. It's actually for an app.

Side note: mobile site does not do syntax highlighting!
Regarding Qu: Right after I posted, Benryves helped me figure it out, I just didn't feel like re-editing my post (for the 4th time).

Syntax highlighting relies on JavaScript, which the mobile site tries not to use (and doesn't use it except for the chatbox, which is its own page). So it's not a bug that it isn't working on the mobile site, it's by design Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement