일련 번호가 불필요한 것처럼 단순화 할 수 있습니다. @iharob 및 @Jasper와 같은 몇 가지 실수가 있으며 OP는 2 차원 문자 배열의 모든 문자에 쓰기 위해 strcpy()
을 사용했습니다. 이는 실제로 1 차원 문자열 배열입니다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WORDS 10
#define ANSWERS 20
#define NUMBERS 20
int main()
{
// Words to have a number appended
char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
"avga","sampuan","rouxa","aporipantiko","aposmitiko"};
//This is the array to create the results.
char random_type [ANSWERS][35];
int i, word, numb;
srand ((unsigned)time(NULL));
for(i=0; i<ANSWERS; i++) {
numb = rand() % NUMBERS;
word = rand() % WORDS;
sprintf(random_type[i], "%s %d", type[word], numb);
}
for(i=0; i<ANSWERS; i++) {
printf ("%s\n", random_type[i]);
}
return 0;
}
프로그램 출력 : 비록
aporipantiko 19
makaronia 14
makaronia 9
aposmitiko 10
sampuan 6
feta 10
feta 2
giaourti 3
rizi 10
feta 8
sampuan 7
rouxa 4
rizi 8
giaourti 0
giaourti 19
aposmitiko 13
rouxa 2
avga 13
giaourti 13
aporipantiko 8
는, 아마도 OP 내가이 제공되는 경우 모든 숫자로 모든 단어를 바꾸어 넣 의미.
#include <stdio.h>
#include <stdlib.h>
#define WORDS 10
#define NUMBERS 20
int main()
{
// Words to have a number appended
char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
"avga","sampuan","rouxa","aporipantiko","aposmitiko"};
//This is the array to create the results.
char random_type [WORDS][NUMBERS][35];
int i, j;
for(i=0; i<WORDS; i++)
for(j=0; j<NUMBERS; j++)
sprintf(random_type[i][j], "%s %d", type[i], j);
for(i=0; i<WORDS; i++)
for(j=0; j<NUMBERS; j++)
printf ("%s\n", random_type[i][j]);
return 0;
}
OP 의견에 이어 600 번째 문자열 배열을 만드는 세 번째 솔루션이 있습니다.
#include <stdio.h>
#include <stdlib.h>
#define WORDS 10
#define NUMBERS 20
int main()
{
// Words to have a number appended
char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
"avga","sampuan","rouxa","aporipantiko","aposmitiko"};
//This is the array to create the results.
char random_type [WORDS*NUMBERS][35];
int i, j, k=0;
for(i=0; i<WORDS; i++)
for(j=0; j<NUMBERS; j++)
sprintf(random_type[k++], "%s %d", type[i], j);
for(k=0; k<WORDS*NUMBERS; k++)
printf ("%s\n", random_type[k]);
return 0;
}
"0"은 종료 바이트가 0이므로 char [2] 배열에 맞지 않습니다. – Jasper
'random_type' 요소의 예상 값이 무엇인지는 분명하지 않습니다. –
무한 루프 ->'for (j = 0; i <20; j ++)'그리고'random_type [i] [j]'란 무엇입니까? –