C编程团队随机

时间:2015-01-23 20:38:08

标签: c arrays random assign

我有一个c程序,我有一系列团队,团队应分为4组(groupA,groupB,groupC,groupD),每组由4个团队组成。团队应随机分组。程序给我的错误是“错误类型char [4]不可分配”非常感谢:)

以下是编码:

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

2 个答案:

答案 0 :(得分:2)

更改每一行

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

答案 1 :(得分:0)

您需要使用strcpy(),数组不可分配。

相关问题