0

我正在尝试让 function2() 检查水平对,让 function3() 检查垂直对并将两个值传回 main 以供 function4() 使用,我知道不正确命名它们是不好的做法,但请耐心等待...任何帮助将不胜感激...

我的问题是为什么我得到 countX 和 countY 的输出 0 以及如何解决它..

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


#define maxrow 20  //defines maxrow as a constant of 20
#define maxcol 30  //defines maxcol as a constant of 30


void function1(char array[][maxcol]);
int function2(char array[][maxcol]);
int function3(char array[][maxcol]);
void function4(int, int); //function to display the pairs count

int main( void )
{ 

int countX = 0;
int countY = 0;
srandom( (unsigned) time(NULL) );
char array[maxrow][maxcol];

function1(array);

function2(array);

function3(array);

function4 (countX, countY);

    return ( 0 ) ;

} 


void function1(char array[][maxcol])
    {
    int x = 0;
    int y = 0;

     for (x=0;x<maxrow;x++)
        {
             for (y=0;y<maxcol;y++)
                {
                 array[x][y] = random() % 26 + 'A';
                 printf("%c ", array[x][y]);
                }
         printf("\n");
        }
    printf("\n");
    }
int function2(char array[][maxcol])
    {
    int col = 0;
    int row = 0;
    int countX = 0;

    for (row=0;row<maxrow-1;row++)
            {
            for (col=0;col<maxcol; col++)
                   {
                     if (array[row][col] == array[row+1][col])
                       {
                            countX++;
                       }
                   }
             }
    return(countX);
    }
int function3(char array[][maxcol])
    {
    int col = 0;
    int row = 0;
    int countY = 0;

    for(col=0;col<maxcol-1;col++)
        {
         for (row=0;row<maxrow;row++)
                {
                 if (array[row][col] == array[row][col+1])
                   {
                    countY++;
                   }
                }
        }
    return(countY);
    }
void function4(int countX, int countY)
    {
    printf("\nNumber of horizontal pairs: %d\n", countX);
    printf("\nNumber of vertical pairs: %d\n\n", countY);

    }    

输出:

N E R J B Z Y R I T Y J P P B G K R A Z X O A Y V W S E V V 
M L C F U D G S U O L U Z C M C K W T N V T B V T W U L B R 
I N C K U Y N A R K Q E G Q J S U T P Q G M J I K E E G P H 
X Z W C K Q A Z Q T L H Y S Z H K V A B L J O W R A A X G P 
H F R D H B U K A M D O V D G W K S S N U D W K C P K C O Q 
U V V L B F M X R O J U E F A K B K F V Z Z B V J D M V F D 
L B A I M B N A Y E R K B V P B I S N N O N O R K Z U X U Z 
C F D C N P G D S G J J Q K G H N Q C B F S O T J Y S D X M 
E Z T H E J Z M M R U V C L I K U V B Y W I Q M C B L W E K 
L L M E U Q P V E B O Y Z Q L J D G E G E D O X P S Y A P D 
L A Q X G M P W I T Z W T A P F J S L O A R R O O G H N J W 
S W Y K V E W K A E F C B Y C Q D M K Q C K I T A W B H L K 
F D I F N D K M N M Q U Q T T T L Y F V P H H X C I V D R J 
Q X O Y E E C Q Q R D G M V C H O N F V J U E Q T G A R M S 
A E R O E V S I M I A R R O M T V D I C Y R X D K S J K J X 
E L B V A I R U Q F F S Y Y G K T D N B G M V F R F Z C R J 
A W W D R W L K T E P A W P Y D A R I P U O D P V U W V X O 
G Z M C E F B S Q U W H U S X S X Z L G O F W U X S Q T P P 
H V O V X V B Y N R U J A O D X I B Y V H P D D J A X Z T M 
P D H F Y H A Z H N Q C Y T S C S B D R Y M G B R R B P Q V 


Number of horizontal pairs: 0

Number of vertical pairs: 0
4

3 回答 3

2

原因如下:

function2(array); // in main
function3(array); //in main

这两个函数都返回一些东西,但在 main 中你只是调用它们并且不收集返回的值.. 上面的更改是main()

 countX = function2(array); // this will put the value ( countX ) returned by this function in countX
 countY = function3(array); // this will put the value ( countY ) returned by this function in countY
于 2013-11-05T06:57:31.123 回答
1

在函数function2function3变量中countX,和countY(分别)是局部变量。就像countXcountYinmain是本地的 inside一样main

解决问题的一种方法是使变量全局化,但我不建议这样做。相反,我建议您return使用函数中的变量,并在main.

所以例如在function2

int function2(...)
{
    ...
    return countX;
}

并在main

countX = function2(...);
于 2013-11-05T06:55:58.090 回答
0

使 countX 和 countY 成为全局变量(并且不要在函数中重新声明)。

或者更好 - 在 main() 中:

countX = function2(array);
countY = function3(array);

还有一个免费提示:你不需要()所有的回报......

于 2013-11-05T06:56:41.987 回答