2014-04-03 3 views
1

나는 단어 검색 프로그램을 만들고 있으며, 시작 단계에 있습니다. 사용자가 단어 검색에 들어가는 단어를 입력하기 전에 사용자가 '+'크기의 그리드를 입력하게하려고합니다. 프로그램을 실행할 때 '+'하나만 인쇄합니다. 왜 단지 하나의 값만 인쇄하고 모든 값이 '+'로 표현되도록 코드를 조정해야합니까? 나는 프로그래밍 초보자이므로 조언이 도움이 될 것입니다. 감사합니다.왜 내 2D 배열은 하나의 값만 인쇄합니까?

#include<stdio.h> 

void printmatrix(char matrix[][20],int); 

int main(void) 
{ 
    char matrix[20][20]= {{'+'}}; 
    int x=1; 

     printf("How large would you like the puzzle to be (between 10 and 20):\n"); 

    scanf("%d",&x); 

    printmatrix(matrix,x); 

    return 0; 

} 

/******************************************** 

Function Name: Printmatrix...this function prints out an empty matrix of the grid size the user asked for 

Inputs: Variables x and y...the row and column variables the user inputted 

Outputs: Empty matrix filled with '+' thats the size the user asked for 

********************************************/ 

void printmatrix(char matrix[][20],int x) 
{ 
    int i,j; 

    printf("Empty Puzzle:\n"); 

    for (i=0;i<x;i++) 
    { 
     for (j=0;j<x;j++) 
     { 
      printf("%c", matrix[i][j]); 
     } 
     printf("\n"); 
    } 

} 

답변

1

char 배열을 사용하므로 memset을 사용하여 배열의 값을 동일한 값으로 설정할 수 있습니다.

memset(matrix , '+' , sizeof(matrix)) ; 

당신 코드

char matrix[20][20]= {{'+'}}; 

matrix[0][0] = '+' ; 

및 배열의 ​​나머지 당신은 초기화하기 위해서 중첩 루프를 작성 할 수 있습니다 0

1

로 설정됩니다 할 것 행렬 char에서 '+'까지 :

char matrix[20][20]; 
int i; 
int j; 

/* Fill matrix with '+' */ 
for (i = 0; i < 20; i++) { 
    for (j = 0; j < 20; j++) { 
     matrix[i][j] = '+'; 
    } 
} 
+0

이전에이 작업을했지만 프로그램의 나중 단계에서이 행렬의 특정 값을 사용하여 단어를 삽입해야합니다. – user3376431

관련 문제