wordsearch c程序分配

时间:2015-04-22 21:33:04

标签: c

编辑:我的新问题是如何将文字放在网格上的随机位置,我尝试过如下,但只是在同一个地方获得一个随机字母。而且我也正确地完成了这些功能,因为我觉得这可能是个问题。



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

char **createArray(); //function prototype
void printArray(char** array);
void insertHorizontally(char*word, char** array);
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array
void main()
{
	char words[4] = {'f','r','e','e'};
	myArray = createArray();
	insertHorizontally(words,myArray);
	printArray(myArray);
	
}
	//Creates a 2D array of WIDTH * HEIGHT and returns a pointer to it
char **createArray()
{
	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++)
 		{
			 //words[i][j] = 65 + rand() % 25;
			 array[i][j] = '_';
 		}
 	}
 	return array;
}
void printArray(char** array)
{
	int i,j;
	array =myArray;
	for(i=0; i<WIDTH; i++)
 	{
		for(j=0; j<HEIGHT; j++)
 		{
			printf(" %c ", array[i][j]);
		}
		printf("\n");
	}
}
void insertHorizontally(char*word, char** array)
{
	array=&myArray;
	array=&rand();
	word = (char *) malloc(sizeof(char) * HEIGHT);
	int i,j,a;
	for(j = 0; j<HEIGHT; j++)
	{
		for(a=0;a<sizeof(word);a++)	
		{
			array[i][j] = *word;
		}
	}
	
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

array=&myArray;//overwrite
array=&rand();//overwrite?, `&rand()` invalid
word = (char *) malloc(sizeof(char) * HEIGHT);//overwrite
sizeof(word)//pointer size, change to `char words[] = "free";...strlen(word)

在随机位置沿水平方向书写:

void insertHorizontally(char*word, char** array)
{
    int word_len = 4;
    int i = rand() % (HEIGHT-word_len+1);
    int j = rand() % WIDTH;
    int k;
    for(k=0;k < word_len;k++) {
        array[j][i+k] = word[k];
    }
}