1

I want to run a For Loop in C language to concatenate and then copy the results in a new array.

    // 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]);
        }

    }
6
  • 4
    "10" will not fit in a char[2] array because of the terminating zero byte. Commented Jan 18, 2015 at 20:01
  • It's not clear to me what the expected values of the elements of random_type are. Commented Jan 18, 2015 at 20:04
  • 3
    infinite loop -> for (j = 0; i < 20; j++), and also what is random_type[i][j]? Commented Jan 18, 2015 at 20:04
  • @Jasper i have set char numbers[20][2] and i have seen it fits likes this. isn't it? Commented Jan 18, 2015 at 20:47
  • A string of length 2 will need a char array of size 3 to contain it, and its terminator. Commented Jan 18, 2015 at 20:49

1 Answer 1

1

Some simplifications can be made, such as not needing an array of consecutive numbers. There are some mistakes, such as @iharob and @Jasper pointed out, and OP used strcpy() to write to every char of a 2-dimensional char array, which really is a 1-D string array.

#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;
}

Program output:

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

Although, perhaps OP meant to permute every word with every number, in which case I offer this.

#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;
}

Following OP comments, yet a third solution creating one array of 600 strings.

#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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this! What i actually need is to add this information in total of 200 results in an array so that i can transfer it to a struct.
@falsobuio the second part of my answer creates in total 200 results in an array, I didn't print them due to size.
@falsobuio I added a third part that creates one string array of 200 strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.