C编程团队随机化

C programming teams randomise

我有一个 c 程序,其中有一组团队,团队应分为 4 个组(groupA、groupB、groupC、groupD),每个组由 4​​ 个团队组成。队伍随机分组。该程序给我的错误是 "Error type char[4] is not assignable" 非常感谢:)

编码如下:

#include <time.h> 
#include <stdio.h> 
#include <string.h>

int main(void) {
    char teams[20][17] = {
       "Germany", "Spain", "France", "Brazil", 
       "Italy", "Argentina", "USA","Uruguay", 
       "Hungry", "England", "Portugal", "Sweden", 
       "Netherlands", "Poland", "Mexico", "Croatia"
    };

char groupA[20][4];
char groupB[20][4];
char groupC[20][4];
char groupD[20][4];
char s[20];

int gA = 0 ,gB = 0 , gC = 0 , gD = 0;
int r , i;
int ok = 0;


for (i = 0; i < 20; i++)
{
    do{
        ok=0;
        printf("group = %s and team is %s", s,teams[i]);
        if(s==0 && gA<4){ //Group A
            groupA[gA]= teams[i];
            gA++;
            ok=1;
        }

       if(s==1 && gB<4){ //Group B
            groupB[gB]= teams[i];
            gB++;
            ok=1;
        }
        if(s==2 && gC<4){ //Group C
            groupC[gC]= teams[i];
            gC++;
            ok=1;
        }
        if(s==3 && gD<4){ //Group D
            groupD[gD]= teams[i];
            gD++;
            ok=1;
        }
    } while (!ok);
}   

for(i = 0;i < 4;i++) {
    printf("%.15s\t\t%.15s\t\t%.15s\t\t%.15s\n",
      groupA[i],
      groupB[i],
      groupC[i],
      groupD[i]
    );
}

改变每一行

groupX[gX]=teams[i];

其中 X 是 A 或 B 等

strcpy(groupB[gB],teams[i]);

问题是您不能使用运算符 = 在 C 语言中分配字符串(我指的是字符数组) 您需要使用 memcpy()strcpy()

等函数

您上面写的代码还需要修正。这是一个可以满足您需求的注释代码!

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
//important if you want to get different result in every execution (change the seed)
    srand(time(NULL));
//this is the list of all the teams
    char teams[16][12] = {"Germany" , "Spain" , "France" , "Brazil" , "Italy" , "Argentina" , "USA" ,"Uruguay" , "Hungry" ,"England" , "Portugal" , "Sweden" , "Netherlands" , "Poland" , "Mexico" , "Croatia"};

//each group will hold 4 teams each one
    char groupA[4][12];
    char groupB[4][12];
    char groupC[4][12];
    char groupD[4][12];

//the array s contain the name of each group only for print
    char s[4]= {'A','B','C','D'};
//the gX variables are the number of team of each group during the draw
    int gA = 0 ,gB = 0 , gC = 0 , gD = 0;
//the r is the random number representing the group on which every team will be
    int r , i;
    int ok = 0;

    printf("The World CUP Group Draw \n");
    printf(" **************************** \n");
    //this for loop is for the draw of groups
    for (i = 0; i < 16; i++)
    {
        //when ok is zero that means the team is still without group
        ok=0;
        do
        {
            //we use the rand() in order to get a random number
            r=(rand()%4);

            if(r==0 && gA<4)  //Group A
            {
                printf("group = %c and team is %s\n", s[r],teams[i]);
                strcpy(groupA[gA],teams[i]);
                gA++;
                ok=1;
            }

            if(r==1 && gB<4)  //Group B
            {
                printf("group = %c and team is %s\n", s[r],teams[i]);
                strcpy(groupB[gB],teams[i]);
                gB++;
                ok=1;
            }
            if(r==2 && gC<4)  //Group C
            {
                printf("group = %c and team is %s\n", s[r],teams[i]);
                strcpy(groupC[gC],teams[i]);
                gC++;
                ok=1;
            }
            if(r==3 && gD<4)  //Group D
            {
                printf("group = %c and team is %s\n", s[r],teams[i]);
                strcpy(groupD[gD],teams[i]);
                gD++;
                ok=1;
            }

        }
        while (ok==0);
    }

//print the final result of the draw
    printf("\n%-15s %-15s %-15s %-15s\n","GroupA","GroupB","GroupC","GroupD");
    printf("\n%-15s %-15s %-15s %-15s\n","______","______","______","______");
    for(i = 0; i < 4; i++)
    {
        printf("\n%-15s %-15s %-15s %-15s\n",groupA[i],groupB[i],groupC[i],groupD[i]);
    }

    return 0;
}

您需要使用strcpy(),数组不可赋值。