gpt4 book ai didi

c - 二维数组函数逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:46 25 4
gpt4 key购买 nike

我创建了这个程序来扫描二维数组以查找垂直和水平对,其中 row = 20 和 column = 30,然后显示输出。

我想我可能搞砸了函数 2 和 3 中的逻辑。任何人都可以帮我检查一下并帮助我找出我做错了什么吗? 我会非常感谢。

完整程序代码:

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

#define ROW 20
#define COLUMN 30

void intro_msg (void);
char function1 (char [ROW] [COLUMN]);
char function2 (char [ROW] [COLUMN]);
char function3 (char [ROW] [COLUMN]);
void function4 (int , int);
void goodbye_msg (void);

int main( void )
{ // MARKS THE BEGINNING OF THE main( ) BLOCK OF STATEMENTS

char randchar_array [ROW] [COLUMN];
int hor_pairs = 0 , vert_pairs = 0;
srandom ( (unsigned) time (NULL) );

intro_msg ( ) ;

function1 (randchar_array);

hor_pairs = function2 (randchar_array);

vert_pairs = function3 (randchar_array);

function4 (hor_pairs , vert_pairs);

goodbye_msg ( ) ;

return ( 0 ) ;

} // MARKS THE END OF THE main( ) BLOCK OF STATEMENTS

void intro_msg (void)
{
printf( "\n Welcome user, this program creates a 2D array of characters "
"that has 20 rows\n and 30 columns and then populates it with random"
" characters. The program then\n displays the number of vertical "
"and horizontal pairs in the array.\n\n");
return ;
}

char function1 (char randchar_array[ROW] [COLUMN])

{
int r = 0 ;
int c = 0 ;
char nextchar = 'A';

for (r = 0; r < ROW ; r++)
{
for (c = 0; c < COLUMN ; c++)
{
nextchar = 'A' + random() % 26;
randchar_array [r] [c] = nextchar;
printf(" %c", nextchar);
}

printf("\n");
}

return (randchar_array [ROW] [COLUMN]);
}

char function2 (char randchar_array[ROW] [COLUMN])

{
int r = 0 ;
int c = 0 ;
int horpairs = 0;

for (r = 0; r < ROW -1; r++)
{
for (c = 0; c < COLUMN ; c++)
{
{
if(randchar_array[r][c] == randchar_array[r][c+1])
horpairs++;
}
}
}

return (horpairs);
}

char function3 (char randchar_array[ROW] [COLUMN])

{
int r = 0 ;
int c = 0 ;
int vertpairs = 0;

for (r = 0; r < ROW ; r++)
{
for (c = 0; c < COLUMN -1; c++)
{
{
if(randchar_array[r][c] == randchar_array[r+1][c])
vertpairs++;
}
}
}

return (vertpairs);
}

void function4 (int hor_pairs, int vert_pairs)
{
printf ( " \n Number of horizontal pairs: %d /n" , hor_pairs );
printf ( " \n Number of vertical pairs: %d /n" , vert_pairs );
return ;
}

void goodbye_msg (void)
{
printf ( "\n Thank you for using this program.\n\n") ;
return ;
}

最佳答案

for (r = 0; r < ROW -1; r++)
{
for (c = 0; c < COLUMN ; c++)
{
if(randchar_array[r][c] == randchar_array[r][c+1])
horpairs++;
}
}
循环条件中的

ROW-1 和循环体中的 c+1 将产生数组越界异常。您想要 COLUMN-1 而不是 ROW-1 或者您想要 r+1 而不是 c+1 .

function2()function3() 都存在这个问题。我想你想要:

char function2 (char randchar_array[ROW] [COLUMN]) {
int r = 0 ;
int c = 0 ;
int horpairs = 0;
for (r = 0; r < ROW ; r++){
for (c = 0; c < COLUMN-1 ; c++){
if(randchar_array[r][c] == randchar_array[r][c+1])
horpairs++;
}
}
return (horpairs);
}

char function3 (char randchar_array[ROW] [COLUMN]) {
int r = 0 ;
int c = 0 ;
int vertpairs = 0;
for (r = 0; r < ROW -1; r++){
for (c = 0; c < COLUMN ; c++){
if(randchar_array[r][c] == randchar_array[r+1][c])
vertpairs++;
}
}
return (vertpairs);
}

关于c - 二维数组函数逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788014/

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