0

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

2 回答 2

2
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循环体中会产生一个 Array-out-of-bounds Exception。要么你想要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);
}
于 2013-11-05T11:49:02.963 回答
2

你犯了两次同样的错误:

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

在这里,并且:

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

您正在for循环遍历 char 数组的所有元素,其中cr将分别递增到COLUMN -1ROW -1。但是在上述循环中,您在语句的右手操作数中增加c或增加 1。 上次循环运行时,这意味着您实际上超出了界限,并且正在访问(或至少尝试访问):rif

randchar_array[r][COLUMN]
//and
randchar_array[ROW][c]

这是错误的。将循环更改为:

for(c = 0;c < COLUMN -1;++c)

或者,对于更微观优化的方式

for(c = COLUMN - 1;c > 0; --c)

当然,后一个循环将字符迭代到前面,因此您必须考虑到这一点。
请注意,在最后一个循环 ( for(c = COLUMN - 1;c > 0; --c)) 中,c永远不会达到 0,但那是因为它不需要,因为,when ,无论如何c = 1你都在比较它......c-1

您的代码最终可能看起来像:

int function2(char randchar_array[ROW][COLUMN])
{
    int r,c, horpairs = 0;
    for (r = 0; r < ROW ; ++r)
    {
        for (c = COLUMN -1; c > 0 ; --c)
        {
            if(randchar_array[r][c] == randchar_array[r][c-1]) horpairs++;
        }
    }
    return horpairs;
}
//and
int function3(char randchar_array[ROW][COLUMN])
{
    int r,c, vertpairs = 0;
    for (r = ROW - 1; r > 0 ; --r)
    {
        for (c = 0; c < COLUMN; ++c)
        {
            if(randchar_array[r][c] == randchar_array[r-1][c]) vertpairs++;
        }
    }
    return vertpairs;
}
于 2013-11-05T11:58:34.877 回答