2015-01-18 1 views
1

For 루프를 C 언어로 실행하여 결과를 연결 한 다음 새 배열로 복사하려고합니다.C - 하나의 루프를 사용하여 배열 2 개를 연결하십시오.

// Simple array with numbers to be appended at the end of the array "type" below 

char numbers[20][2]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"}; 

//after each of these words i want to append the number found above. 

char type[10][30]={"rizi","makaronia","kafes","giaourti","feta","avga","sampuan","rouxa","aporipantiko","aposmitiko"}; 

//This is the array i wish to add the results. This way i will create 20 of each type 

char random_type [20][30]; 

    int i,j; 
    for(i = 0; i < 10; i++) 
    { 
     for (j = 0; i < 20; j++) 
     { 
      strcpy(random_type[i][j],type[j]); 
     } 

    } 
+4

"0"은 종료 바이트가 0이므로 char [2] 배열에 맞지 않습니다. – Jasper

+0

'random_type' 요소의 예상 값이 무엇인지는 분명하지 않습니다. –

+3

무한 루프 ->'for (j = 0; i <20; j ++)'그리고'random_type [i] [j]'란 무엇입니까? –

답변

1

일련 번호가 불필요한 것처럼 단순화 할 수 있습니다. @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

감사합니다! 내가 실제로 필요로하는 것은 구조체로 전달할 수 있도록 총 200 개의 결과로이 정보를 배열에 추가하는 것입니다. – falsobuio

+0

@falsobuio 내 대답이 유용하다면 "받아 들여주세요", 고맙습니다. –

+0

@ falsobuio 내 대답의 두 번째 부분은 배열로 총 200 개의 결과를 만듭니다. 크기 때문에 인쇄하지 않았습니다. –

관련 문제