Я пишу программу, которая делает следующее. Я закончил с моим кодом, и я думаю, что это хорошо, но я пытаюсь сделать следующее, что я не могу понять.Как передать указатели в функции?
1. Наведите указатель на матрицу на функцию и измените ее матрицу. Основная функция должна определить массив, инициализировать его, а затем вызвать (а) функцию (ы), чтобы манипулировать им.
2.Используйте буквы 'A' через 'Z', затем буквы 'a' через 'z'. Это означает, что случайное блуждание пойдет до 52 мест. Для этого один у меня уже есть буквы от А до Z, но не знаю, как «а» до «Z»
Write a program that generates a “random walk” across a 10 x 10 array. The array will contain character (all ‘.’ Initially). The program must randomly “walk” from element to element, always going up, down, left, or right by one element. Z, in the order visited. Here’s an example of the desired output:
A . . . . . . . . .
B C D . . . . . . .
. F E . . . . . . .
H G . . . . . . . .
I . . . . . . . . .
J . . . . . . . . .
K . . R S T U V Y .
L M P Q . . . W X .
. N O . . . . . . .
. . . . . . . . . .
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
void printarray(char array[12][12], int x, int y); /*Function prototype*/
int i = 0, roll = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0; /*i = number of moves. roll = value generated by the random number generator. r = row location. col = column location. rowcheck and colcheck are the incremented and decremented values of row and col respectively, used to check whether a legal move can be performed*/
char position[12][12], chtr = 'A'; /*position[12][12] = Grid for the walking. chtr = the letters of the Alphabet.*/
for (row = 0; row < 12; row++) /*Fills the 12 x 12 array with the "." character*/
{
for (col = 0; col < 12; col++)
{
position[row][col] = '.';
}
}
srand(5000); /*Seeds the random number function*/
for (i = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0; i < 25;)
{
rowcheck = row;
colcheck = col;
roll = rand() % 4;
switch (roll) /*Determines what direction to move starting from the top left corner of the grid (bird's eye view)*/
{
case 0: /*Move up*/
{
rowcheck--;
}
case 1: /*Move left*/
{
colcheck--;
}
case 2: /*Move down*/
{
rowcheck++;
}
case 3: /*Move right*/
{
colcheck++;
}
}
if ((rowcheck < 0 || rowcheck > 9) || (colcheck < 0 || colcheck > 9) || (position[rowcheck][colcheck] != '.'))
{
continue;
}
else
{
row = rowcheck;
col = colcheck;
position[row][col] = chtr;
chtr++;
printarray(position, row, col);
i++;
}
}
exit (0);
}
/*Function declaration*/
void printarray(char array[12][12], int x, int y){
printf("CURRENT POSITION %d %d\n", x, y);
for (x = 0; x < 12; x++) /*Prints out the values of the array*/
{
for (y = 0; y < 12; y++)
{
printf("%c", array[x][y]);
}
printf("\n");
}
printf("\n");
}
Какие у вас проблемы? Синтаксис в порядке. (хотя вы также можете написать '(char (* array) [12], int x, int y);', который отражает преобразование в указатель, который имеет место.) См. также [** Как передать многомерный массив функции в C и C++ **] (http://stackoverflow.com/questions/2828648/how-to-pass-a-multidimensional-array-to-a-function-in-c-and-c?rq=1) –
Итак, что я пытаюсь сделать, это передать указатель на матрицу функции и изменить функцию матрицы. Основная функция должна определить массив, инициализировать его, а затем вызвать (а) функцию (ы), чтобы манипулировать им. Который я не могу понять. –
Также моя программа уже имеет символы от А до Я, но я также хочу иметь строчные символы.Как я могу добавить те –