C 随机序列中的数组排序

C 随机序列中的数组排序,c,arrays,string,sorting,C,Arrays,String,Sorting,我有下面的代码,我希望应该从文件中读取文本,将单词存储在数组中,然后以随机顺序打印出来。最后一个数组是int,但应该是char,它并没有给出正确的答案 #include<stdio.h> #include<string.h> #include <stdlib.h> #include <time.h> int main() { char message[10][150], buffer[150]; int i = 0; in

我有下面的代码,我希望应该从文件中读取文本,将单词存储在数组中,然后以随机顺序打印出来。最后一个数组是int,但应该是char,它并没有给出正确的答案

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

int main()
{
    char message[10][150], buffer[150];
    int i = 0;
    int cntr = 9;
    char freeArray[9];

    srand(time(NULL));
    freeArray[i] = rand() % cntr;
    FILE *file_in;

    file_in = fopen("test.txt", "r");

    while (fgets(buffer, 150, file_in))
    {
        i = rand() % cntr;
        strcpy(message[freeArray[i]], buffer);
    }

    while (cntr >= 0)
    {
        i = rand() % cntr;
        strcpy(message[freeArray[i]], buffer);
        freeArray[i] = freeArray[cntr--];
        printf("%s", freeArray[i]);
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
字符消息[10][150],缓冲区[150];
int i=0;
int-cntr=9;
无字符数组[9];
srand(时间(空));
freeArray[i]=rand()%cntr;
文件*FILE_in;
文件_in=fopen(“test.txt”、“r”);
而(fgets(缓冲区,150,文件_in))
{
i=rand()%cntr;
strcpy(消息[freeArray[i]],缓冲区);
}
而(cntr>=0)
{
i=rand()%cntr;
strcpy(消息[freeArray[i]],缓冲区);
freeArray[i]=freeArray[cntr--];
printf(“%s”,freeArray[i]);
}
返回0;
}
我有其他代码,但这一个给我的文本没有洗牌

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    /*declare and initialise variable*/
    char message[10][150],buffer[150];
    int i=0;
    int j;
    srand(time(NULL));
    FILE *file_in;
    file_in=fopen("test.txt", "r");
    /*stores and prints the data from the string*/
    while(fgets(buffer,150,file_in)){
        strcpy(message[i],buffer);

    }
    while(i < 10)
{
  j = rand() % 10;
  printf("%s\n",message[j]);
  i++;
}

    return 0;
#包括
#包括
#包括
#包括
int main()
{
/*声明并初始化变量*/
字符消息[10][150],缓冲区[150];
int i=0;
int j;
srand(时间(空));
文件*FILE_in;
文件_in=fopen(“test.txt”、“r”);
/*存储并打印字符串中的数据*/
而(fgets(缓冲区,150,文件_in)){
strcpy(消息[i],缓冲区);
}
而(i<10)
{
j=rand()%10;
printf(“%s\n”,消息[j]);
i++;
}
返回0;
以下是答案栏中给出的问题的答案
#包括
#包括
#包括
//#包括
#定义最大消息数(10)
#定义最大消息长度(150)
静态字符消息[MAX_MESSAGES][MAX_message_LEN]={{{{'\0'};
静态字符缓冲区[最大消息长度]={'\0'};
静态字符t[MAX_MESSAGE_LEN]={'\0'};
int main()
{
/*声明并初始化变量*/
int i=0;
int j=0;
FILE*FILE_in=NULL;
if(NULL==(文件_in=fopen(“test.txt”,“r”))
{//然后,fopen失败了
perror(“fopen for test.txt失败”);
退出(退出失败);
}
//否则,fopen成功了
/*存储、排序和打印字符串中的数据*/
i=0;
而((i 0)
{
strcpy(t,message[j-1]);
strcpy(消息[j-1],消息[j]);
strcpy(消息[j],t);
}//如果结束,则结束
}//结束
}//结束
printf(“\n按顺序排列的字符串为:”);
对于(i=0;i<3;i++)
{
printf(“消息:%d:%s\n”,i+1,消息[j]);
}//结束
getchar();
返回0;
}//结束函数:main
以下代码:
--干净利落
--包含对OPs发布代码所需的所有更改
#包括
#包括
#包括
#包括
#定义最大消息数(10)
#定义最大消息长度(150)
静态字符消息[MAX_MESSAGES][MAX_message_LEN]={{{{'\0'};
静态字符缓冲区[最大消息长度]={'\0'};
int main()
{
/*声明并初始化变量*/
int i=0;
int j;
文件*FILE_in;
if(NULL==(文件_in=fopen(“test.txt”,“r”))
{//然后,fopen失败了
perror(“fopen未能通过test.txt”);
退出(退出失败);
}
//否则,fopen成功了
srand(时间(空));
/*存储并打印字符串中的数据*/

虽然((iIt有助于为正确的编程语言添加标记:)“它没有给我正确的答案”太模糊了。正确的答案是什么,您得到了什么?编译器说:第23行:数组下标的类型是char。第25行:格式%s要求参数的类型是char*,但参数2最初的类型是'int',
freeArray[0]
被分配了一个随机数,其余的未初始化。然后你读入每一行,将其分配给
消息的索引,该索引由
freeArray
的一个随机选择元素给出。你有很多工作要做…@潜伏者:事实上,任何高于9的索引都会是一个问题。它只在文本在列中时起作用(1个单词/行)由于答案给出了最短的单词(3次),如何避免重复相同的单词?是否可以阅读一行(一句话)中的文本。
The following is to answer the question given in an answer block

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

#define MAX_MESSAGES (10)
#define MAX_MESSAGE_LEN (150)

static char message[MAX_MESSAGES][MAX_MESSAGE_LEN] = {{'\0'}};
static char buffer[MAX_MESSAGE_LEN] = {'\0'};
static char t[MAX_MESSAGE_LEN] = {'\0'};

int main()
{
    /*declare and initialise variable*/
    int i=0;
    int j = 0;
    FILE *file_in = NULL;

    if( NULL == (file_in=fopen("test.txt", "r") ) )
    { // then, fopen failed
        perror("fopen for test.txt failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    /*stores, sorts and prints the data from the string*/
    i = 0;
    while( (i<10) && (fgets(buffer,150,file_in)) )
    {
        strcpy(message[i],buffer);
        i++;
    } // end while

    for (i = 1; i < 3; i++)
    {
        for (j = 1; j < 3; j++)
        {
            if (strcmp(message[j - 1], message[j]) > 0)
            {
                strcpy(t, message[j - 1]);
                strcpy(message[j - 1], message[j]);
                strcpy(message[j], t);
            } // end if
        } // end for
    } // end for

    printf("\nStrings in order are : ");

    for (i = 0; i < 3; i++)
    {
        printf("message: %d: %s\n", i+1, message[j]);
    } // end for

    getchar();
    return 0;
} // end function: main
The following code:
-- compiles cleanly
-- contains all the changes needed to the OPs posted code

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

#define MAX_MESSAGES (10)
#define MAX_MESSAGE_LEN (150)

static char message[MAX_MESSAGES][MAX_MESSAGE_LEN] = {{'\0'}};
static char buffer[MAX_MESSAGE_LEN] = {'\0'};

int main()
{
    /*declare and initialise variable*/
    int i=0;
    int j;

    FILE *file_in;
    if( NULL == (file_in=fopen("test.txt", "r") ) )
    { // then, fopen failed
        perror( "fopen failed for test.txt" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    srand(time(NULL));

    /*stores and prints the data from the string*/
    while( (i<MAX_MESSAGES) && fgets(buffer,150,file_in) )
    {
        strcpy(message[i],buffer);
        i++;
    } // end while

    printf("\ndisplay %d messages in random order\n", MAX_MESSAGES);
    printf("with possible repeated messages and skipped messages\n");
    for( i=0; i < MAX_MESSAGES; i++)
    {
        j = rand() % MAX_MESSAGES;
        printf("message: %d: %s\n",j, message[j]);
    } // end for

    return 0;
} // end function: main