0

i have below code, that reads and gives as output words from text file. How can i shuffle these words?

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

int main()
{
    /* Declare and initialise variable */
    char message[10][150], buffer[150];
    int i = 0;
    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);
        printf("%s", message[i]);
        i++;
    }

    getchar();
    return 0;
}
Ashwani
  • 1,938
  • 11
  • 15
Rimas
  • 25
  • 4
  • 2
    Why do you want to shuffle the words? Just leave them as they are and use a random index to access ... – Mawg says reinstate Monica Jan 05 '15 at 13:02
  • If you *must* shuffle them, then generate 2 random, but not equal numbers, uses these as indices and use strcpy() and a temp string to exchange them. Repeat until "sufficiently random". – Mawg says reinstate Monica Jan 05 '15 at 13:03
  • Better don't ask why :) I just need to print it out in random order. – Rimas Jan 05 '15 at 13:04
  • The too-obvious answer: More code will be required. What, if *anything*, have you tried? – WhozCraig Jan 05 '15 at 13:04
  • 1
    possible duplicate of [Shuffle array in C](http://stackoverflow.com/questions/6127503/shuffle-array-in-c) – Shahriar Jan 05 '15 at 13:06
  • 2
    @Mawg: That's not usually a good algorithm, exactly because "sufficiently random" is hard to define. Check out [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). – Thomas Jan 05 '15 at 13:14

3 Answers3

0

Use

int j;
srand((unsigned)time(&t));

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

The above ensures that the words are shuffled and gets printed out but no way this will make sure that there are no repeated words in the output in order to handle this check the link below

Shuffle array in C

Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 3
    Note:, While this will certainly print random slots, it does nothing to ensure the *same* slot is only printed *once*. – WhozCraig Jan 05 '15 at 13:11
  • @WhozCraig Agree if that needs to be done then I should keep an array and knock off the already printed indices? Right? – Gopi Jan 05 '15 at 13:20
  • 2
    No, you should [shuffle the array](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle) itself. It isn't hard. Searching a bed of indices for previously used instances is anything-but-efficient, and depending on the size it can get ridiculous (ex: an array of 1,000,000 strings. You *really* want to wait for those last few unused indices to finally get hit by the prng?). – WhozCraig Jan 05 '15 at 13:23
0

You can print each word from a random position of an array message using e.g function rand().

char message[10][150], buffer[150];
int position,i=0;
srand(time(NULL));

while (fgets(buffer, 150, file_in)) {

    strcpy(message[i], buffer);

    i++;
}
for(i=0;i<10;i++){
   position = rand() % 10;
   printf("%s", message[position]);
}

Or you can store them in random positions in the array and print the data continuosly from array using a simple for loop .

Thanos Pappas
  • 167
  • 1
  • 1
  • 9
0

You should maintain 2 arrays, in the first you will store the words and in the second you will store the still free locations, use a counter to manage the iterations and still free numbers, at first counter=9.

at the beggining: the word array is free the free location holds numbers [0-counter]

choose i randon [0-counter] and get the freeArray[i], use it to store the read word:

strcpy(message[freeArray[i]], buffer);

then move i to the end and get number from 0 to 8 - do it until you run out of numbers, use the counter to manage the number of iterations you have done so far until you reach 0.

while(cntr>=0)
 {
  //get random number to i from [0 .. cntr]
  strcpy(message[freeArray[i]], buffer);
  freeArray[i] = freeArray[cntr--];
 }

and iterate until cntr is -1 - i.e. while cntr>=0. this means you have read all the words in a random order.

antonpuz
  • 3,256
  • 4
  • 25
  • 48
  • hi, i use function i= rand() % 9, but still code won't work for me. – Rimas Jan 06 '15 at 13:21
  • thats because you need to use i= rand() % cntr – antonpuz Jan 06 '15 at 13:22
  • so the very beginning i should describe int cntr = 9? – Rimas Jan 06 '15 at 13:26
  • my code looks so, but still not works. could check what's wrong? int main() { char message[10][150],buffer[150]; int i=0; int cntr=9; char freeArray[9]; srand(time(NULL)); FILE *file_in; file_in=fopen("test.txt", "r"); while(fgets(buffer,150,file_in)){ strcpy(message[freeArray[i]], buffer); } while(cntr>=0) { i=rand() % cntr; strcpy(message[freeArray[i]], buffer); freeArray[i] = freeArray[cntr--]; printf("%s",freeArray[i]); } – Rimas Jan 06 '15 at 13:35
  • Anton, could you check my code here http://stackoverflow.com/questions/27800653/array-sorting-in-randomly?noredirect=1#comment44010306_27800653 – Rimas Jan 06 '15 at 14:40