-1

I have code that prints words from text, but it need to be shuffled. Code works, but words are repeated. What should i change to get unique words only?

#define MAX_MESSAGES (3)
#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("%s\n",message[j]);
    } // end for

    return 0;
} 

i'm aware about Fisher-Yates shuffle method, i found how function is described, but i don't understand how to call it in my code.

void shuffle(int *array, size_t n)
{
    if (n > 1) {
        size_t i;
    for (i = 0; i < n - 1; i++) {
      size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
      int t = array[j];
      array[j] = array[i];
      array[i] = t;
    }
    }
}
Nippey
  • 4,708
  • 36
  • 44
rimasbimas
  • 35
  • 1
  • 6
  • possible duplicate of [Shuffle array in C](http://stackoverflow.com/questions/6127503/shuffle-array-in-c) – Joe Jan 07 '15 at 09:07
  • `int array[MAX_MESSAGES] = { 0,1,2};`//set value by loop; then call `shuffle(array, MAX_MESSAGES);` ... `printf("%s\n",message[array[j]]);` – BLUEPIXY Jan 07 '15 at 09:48
  • guys, maybe you know how can i generate random, but unique numbers from 1 to 10? i think this will solve my problem. – rimasbimas Jan 07 '15 at 09:56
  • start from 0 because array's index start from 0 in C. – BLUEPIXY Jan 07 '15 at 10:36

1 Answers1

0

Resubmitted answer. I have changed almost every line of your question's code, for one reason or another. I suggest you go through and compare line by line. In particular, you were not aways using the #define values, and, your loops were based on max capacity rather than the number of lines actually read.

I added the random but unique output previously posted.

Be aware that fgets() leaves the trailing newline as shown by the double line spacing of the output.

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

#define MAX_MESSAGES        20
#define MAX_MESSAGE_LEN     150

char message[MAX_MESSAGES][MAX_MESSAGE_LEN+1];  // allow 1 spare
char buffer [MAX_MESSAGE_LEN+1];
int pool    [MAX_MESSAGES];

int main()
{
    int size=0, n;
    FILE *file_in;
    if (NULL == (file_in=fopen("test.txt", "rt"))) {
        perror ("fopen failed for test.txt");
        exit (EXIT_FAILURE);
    }
    while (size<MAX_MESSAGES && fgets(buffer, MAX_MESSAGE_LEN, file_in))
        strcpy (message[size++],buffer);
    fclose (file_in);       // was omitted

    for (n=0; n<size; n++)  // set up message index pool
        pool [n] = n;
    srand((unsigned)time(NULL));

    printf ("\ndisplay %d messages in random order\n", size);
    printf ("with possible repeated messages and skipped messages\n");

    while (size) {          // print and remove random message
        n = rand() % size;
        printf("%s\n",message [ pool[n] ]);
        pool [n] = pool [--size];            // remove index from pool
    }
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56