我创建了这个程序,它扫描一个二维数组以查找垂直和水平对,其中 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 ;
}