问题
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 ) ;
}
回答1:
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.
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.
For example:
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
Code:
#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);
}
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.
回答2:
[edit - new information, old code removed]:
In this code segment, I generate the rows and columns similar to what you have done, but because I do not have the random();
function, I wrote my own. And in it, I made it possible to adjust so that it would guarantee pairs within the created sample. This allows you to see that the "pairs" counts within the columns and rows occur correctly. Without this ability to test, I found that the random function rarely created contiguous samples of the same value (a pair). Look for the comments in the random()
function, and run it both ways, with clock()
commented (two places), and uncommented.
Modified random()
function:
trying = 1;
//val = clock(); //To force pairs, comment here
while(trying)
{
//while(val == clock()); //To force pairs, comment here
srand(clock());
Here is the new code: (without modification shown just above)
#include <ansi_c.h>
enum {
row,
col
};
#define ROWS 20
#define COLS 30
int GetNumPairs(int arr[ROWS][COLS], int index, int type);
int random(int min, int max);
int main(void)
{
int i=0, j=0;
int array[ROWS][COLS];
int countPairs[260];
//int countPairsRows[260];
memset(array, 0, sizeof(int)*ROWS*COLS);
for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
{
array[i][j] = random('A', 'Z'); //min, max value to be produced
printf("%c", array[i][j]);
}
printf("\n");
}
printf("\n\n");
memset(countPairs, 0, sizeof(int)*260);//clear memory first
//pairs in columns, must traverse rows from 0 to 19
for(i=1;i<=ROWS;i++)
{
countPairs[i]=GetNumPairs(array, i-1, row);
printf("row %d has %d pairs\n", i, countPairs[i]);
}
printf("\n\n");
memset(countPairs, 0, sizeof(int)*260);//clear memory again
//pairs in rows, must traverse columns from 0 to 29
for(i=1;i<=COLS;i++)
{
countPairs[i]=GetNumPairs(array, i-1, col);
printf("col %d has %d pairs\n", i, countPairs[i]);
}
getchar();
return 0;
}
int random(int min, int max)
{
int random=0, trying=0;
clock_t val;
trying = 1;
val = clock(); //To force pairs, comment here
while(trying)
{
while(val == clock()); //To force pairs, comment here
srand(clock());
random = (rand()/32767.0)*(max+1);
((random >= min)) ? (trying = 0) : (trying = 1);
}
return random;
}
int GetNumPairs(int arr[ROWS][COLS], int index, int type)
{
int i, j,cnt=0;
switch(type){
case row:
for(i=0;i<COLS-1;i++)
{
if(arr[index][i] == arr[index][i+1])
{
cnt++;
}
}
break;
case col:
for(j=0;j<ROWS-1;j++)
{
if(arr[j][index] == arr[j+1][index])
{
cnt++;
}
}
break;
};
return cnt;
}
Following are two images showing (1) normally generated samples, (small number of pairs, to 0 pairs), and the second is data generated with the commented clock()
function to force many pairs:
Normally generated samples:

Samples created with modified random() function:

来源:https://stackoverflow.com/questions/19736442/c-programming-compare-rows-and-columns-of-2d-array