gpt4 book ai didi

c - 如何在函数中传递指针?

转载 作者:行者123 更新时间:2023-11-30 16:56:04 25 4
gpt4 key购买 nike

我正在编写一个执行以下操作的程序。我已经完成了我的代码,我认为很好,但是我正在尝试执行以下操作,但我无法弄清楚。

1.将指向矩阵的指针传递给函数,并让该函数更改矩阵。主函数应该定义数组,初始化它,然后调用函数来操作它。

2.使用字母“A”到“Z”,然后使用字母“a”到“z”。这意味着随机游走将达到 52 个位置。对于这个,我已经有了字母 A 到 Z,但不知道如何将 'a' 变为 '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");





}

最佳答案

好吧,你的问题是纯粹的逻辑问题。您遇到的主要问题是:

if ((rowcheck < 0 || rowcheck > 9) || 
(colcheck < 0 || colcheck > 9) ||
(position[rowcheck][colcheck] != '.'))

每当 rowcheckcolcheck 小于 0 或大于 时,这都会调用未定义的行为> 11 。 (无论值如何,都可以设置 position[rowcheck][colcheck],这些值可以轻松设置在 position 的限制之外)

您的其余问题与控制 switch 语句本身内的 rowcheckcolcheck 值类似。您需要在增加或减少值之前对其进行测试 - 强制值保持在限制范围内,例如:

    switch (roll) {
case 0: /*Move up */
if (rowcheck > 0)
rowcheck--;
break;
...

我不完全确定您想要完成什么,但以下内容将是对您的代码的清理,这似乎是您正在寻找的(尽管我不确定它是否会迭代或增量超过您的全部预期) range)您也不需要 math.h 作为您的代码编写,并且我包含 string.hmemset 消除您的 '.'初始化循环。

如果您使用 srand (5000) 以外的其他内容,您还将获得更好的随机分布; 一个简单的选择是包含 time.h 并使用 srand(时间(NULL));

最后,请避免在代码中使用魔数(Magic Number)(例如129)。如果您需要常量#define它们或使用enum来设置全局常量(如下所示):

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

enum { MAXC = 9, DIM = 12 };

void printarray (char array[DIM][DIM], int x, int y); /*Function prototype */

int main (void) {

int i = 0, roll = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0;
char position[DIM][DIM], chtr = 'A';

/*Fills the DIM x DIM array with the '.' character */
memset (position, '.', sizeof position);

srand (time (NULL)); /* Seeds the random number function */

for (i = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0; i < 25; i++) {
rowcheck = row;
colcheck = col;
roll = rand () % 4;

switch (roll) {
case 0: /*Move up */
if (rowcheck > 0)
rowcheck--;
break;
case 1: /*Move left */
if (colcheck > 0)
colcheck--;
break;
case 2: /*Move down */
if (rowcheck < DIM - 2)
rowcheck++;
break;
case 3: /*Move right */
if (colcheck < DIM - 2)
colcheck++;
break;
}

if (rowcheck > MAXC || colcheck > MAXC ||
position[rowcheck][colcheck] != '.')
continue;

row = rowcheck;
col = colcheck;
position[row][col] = chtr;

if (chtr < 'Y') /* allow lower and upper case */
chtr++;
else if (chtr == 'Z')
chtr = 'a';
else if (chtr > 'a' && chtr < 'z')
chtr++;
else
chtr = 'A';

printarray (position, row, col);
}

exit (0);
}

/*Function declaration */
void printarray (char array[DIM][DIM], int x, int y)
{
printf ("CURRENT POSITION %d %d\n", x, y);
for (x = 0; x < DIM; x++) { /*Prints out the values of the array */
for (y = 0; y < DIM; y++) {
printf ("%c", array[x][y]);
}
printf ("\n");
}
printf ("\n");
}

仔细检查一下,如果您还有其他问题,或者您想为您打算做的事情添加其他信息,请告诉我。

关于c - 如何在函数中传递指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com