C编程比较2d数组的行和列 [英] C Programming compare rows and columns of 2d array

查看:182
本文介绍了C编程比较2d数组的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,所以我试图比较一个2d数组的行和列,看看是否有任何对的字母直接相邻。我可以使数组没有问题,但我唯一的问题是我不知道如何去比较行分别,然后列。如果你可以帮助我在所有这将是惊人的!谢谢!

Hey guys so I am trying to compare the rows and columns of a 2d array to see if there are any pairs of letters directly near each other. I can make the array no problem, but my only problem is I have no idea how to go about comparing the rows separately, and then the columns..If you could help me out at all that would be amazing! thank you!

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

int main( void )
{  

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


for (x=0;x<20;x++)
    {
     for (y=0; y<30; y++)
      {
       array[x][y] = random() % 26 + 65;
       printf("%c ",array[x][y]);
      }
printf("\n");
    }
printf ("\n\n");

return ( 0 ) ;

} 


推荐答案

列中的匹配对,扫描除最后一行之外的每行,并且对于每个列,将 array [row] [col] 处的值与 array [row + 1] [col] 并报告匹配。

To find the matching pairs in columns, scan over each row except the last, and for each column, compare the value at array[row][col] with the value at array[row+1][col] and report matches.

要查找行中的匹配对,除了最后,并且对于每一行,将 array [row] [col] 的值与 array [row] [col + 1] 并报告匹配。

To find the matching pairs in rows, scan over each column except the last, and for each row, compare the value at array[row][col] with the value at array[row][col+1] and report matches.

例如:

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


Scan columns:
a[ 0][15] = a[ 1][15] = Q
a[ 0][27] = a[ 1][27] = G
a[ 1][11] = a[ 2][11] = I
a[ 1][12] = a[ 2][12] = P
a[ 2][20] = a[ 3][20] = W
a[ 3][ 3] = a[ 4][ 3] = Z
a[ 3][25] = a[ 4][25] = L
a[ 3][26] = a[ 4][26] = F
a[ 4][11] = a[ 5][11] = J
a[ 4][17] = a[ 5][17] = I
a[ 4][26] = a[ 5][26] = F
a[ 5][ 0] = a[ 6][ 0] = H
a[ 6][ 6] = a[ 7][ 6] = R
a[ 7][ 8] = a[ 8][ 8] = L
a[ 7][20] = a[ 8][20] = C
a[ 8][ 7] = a[ 9][ 7] = S
a[ 8][ 8] = a[ 9][ 8] = L
a[ 8][16] = a[ 9][16] = H
a[ 9][ 1] = a[10][ 1] = U
a[10][ 7] = a[11][ 7] = H
a[11][ 2] = a[12][ 2] = C
a[13][ 7] = a[14][ 7] = E
a[14][11] = a[15][11] = T
a[15][ 5] = a[16][ 5] = T
a[15][26] = a[16][26] = K
a[17][15] = a[18][15] = I
a[18][23] = a[19][23] = K
Scan rows:
a[17][ 0] = a[17][ 1] = L
a[12][ 1] = a[12][ 2] = C
a[15][ 2] = a[15][ 3] = E
a[17][ 2] = a[17][ 3] = W
a[ 9][ 4] = a[ 9][ 5] = T
a[16][ 4] = a[16][ 5] = T
a[17][ 5] = a[17][ 6] = R
a[ 2][ 8] = a[ 2][ 9] = H
a[11][ 8] = a[11][ 9] = R
a[18][ 8] = a[18][ 9] = T
a[ 3][11] = a[ 3][12] = L
a[13][12] = a[13][13] = E
a[14][12] = a[14][13] = A
a[10][13] = a[10][14] = B
a[ 3][14] = a[ 3][15] = N
a[10][15] = a[10][16] = C
a[ 4][17] = a[ 4][18] = I
a[13][19] = a[13][20] = Y
a[14][19] = a[14][20] = Z
a[ 3][20] = a[ 3][21] = W
a[19][23] = a[19][24] = K
a[ 7][24] = a[ 7][25] = I
a[13][24] = a[13][25] = N
a[ 1][26] = a[ 1][27] = G
a[ 0][27] = a[ 0][28] = G
a[ 4][27] = a[ 4][28] = A

代码:

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

enum { MAXROW = 20, MAXCOL = 30 };

int main(void)
{
    int x = 0;
    int y = 0;
    char array[MAXROW][MAXCOL];

    srandom(time(NULL));

    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\n");

    printf("Scan columns:\n");
    for (int row = 0; row < MAXROW - 1; row++)
    {
        for (int col = 0; col < MAXCOL; col++)
        {
            if (array[row][col] == array[row+1][col])
                printf("a[%2d][%2d] = a[%2d][%2d] = %c\n", row, col, row+1, col, array[row][col]);
        }
    }

    printf("Scan rows:\n");
    for (int col = 0; col < MAXCOL - 1; col++)
    {
        for (int row = 0; row < MAXROW; row++)
        {
            if (array[row][col] == array[row][col+1])
                printf("a[%2d][%2d] = a[%2d][%2d] = %c\n", row, col, row, col+1, array[row][col]);
        }
    }

    return(0);
}

如果您没有C99支持,可以声明 row col ,而不是根据需要在循环中。

If you don't have C99 support, you can declare row and col at the top of the function instead of in the loops as needed.

这篇关于C编程比较2d数组的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆