vendredi 1 avril 2016

randomly insert list of chars into 2d array

I'm currently writing a word search game program however i've became stuck. I'm completely lost on how to insert random chars from a list into a 2D array I know usually you're supposed to use a nested for loop to do this but i have no idea how i can randomly insert the list in the const char's.

The programs output is currently a 16 by 16 square made from '.' to avoid confusing myself.

Any help or tips on how to insert the char's from my categories into the 2D array would be greatly appreciated. :)

Here is my code as it is at the moment;

char name[80];
char **create2DArray(); //function prototype
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array


int categorie;



int main(int argc, char** argv)
  {



    categoriesAnimals();
    categoriesFruits();
    categoriesCountries();
    myArray = create2DArray();
    system("PAUSE");
    return 0;
   }
  //----------------------------Categories----------------------------------

    categoriesAnimals(){
        const char *A[6];
        A[0] = "CAT";
        A[1] = "IGUANA";
        A[2] = "MOUSE";
        A[3] = "MONKEY";
        A[4] = "ORANGUNTAN";
        A[5] = "ZEBRA";
    }

    categoriesFruits(){
        const char *F[6];
        F[0] = "TOMATO";
        F[1] = "APPLE";
        F[2] = "ORANGE";
        F[3] = "KIWI";
        F[4] = "DRAGONFRUIT";
        F[5] = "DURIANS";

    }

    categoriesCountries(){
        const char  *C[6];
        C[0] = "COLUMBIA";
        C[1] = "GERMANY";
        C[2] = "MEXICO";
        C[3] = "AFGHANISTAN";
        C[4] = "ARGENTINA";
        C[5] = "HUNGARY";
    }



//-----------------------array ---------------------------------------------

        char **create2DArray()
    {
        int i,j;
        char **array = (char **) malloc(sizeof(char *) * WIDTH);

        for(i=0; i<WIDTH; i++)
        array[i] = (char *) malloc(sizeof(char) * HEIGHT);

        for(i=0; i<WIDTH; i++)
        for(j=0; j<HEIGHT; j++)

 // array[i][j] = 65 + rand() % 25;

        array[i][j] = '.';
    //--------prints array----------------------------------------          
        for(i=0; i<WIDTH; i++){
        for(j=0; j<HEIGHT; j++){
        printf("%c ", array[i][j]);
         }
        printf("\n");
        }   
        return array;
        }
    //------------------------------------------------------------  

//-----------------end of array --------------------------------------------    

//-----------------------------insert into 2d arrray------------------------   
for(i=0; i<WIDTH; i++){


        for(j=0; j<HEIGHT; j++){




Aucun commentaire:

Enregistrer un commentaire