Reputation: 271
I'm having a little trouble getting my words to print out in random order. The 8 words that I insert will need to be printed out in random order. Right now I'm only able to generate a few of them in random order because it overwrites the previous space if the same number is generated twice. How can I eliminate this issue?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
int readLine(char string[]);
int main(void)
{
int i;
char str[8][MAX], temp[8][MAX];
srand((unsigned)time(NULL));
int r_num;
printf("Enter 8 words:\n");
for(i=0; i<8; ++i)
{
readLine(str[i]);
}
for (int i = 0; i < 8; i++)
{
r_num = rand()%8+1;
strcpy(temp[r_num], str[i]);
}
printf("\nRandom order of words: \n");
for(i=0; i<8; ++i)
{
printf("%s\n", temp[i]);
}
printf("\n");
return 0;
}
int readLine(char string[])
{
int ch;
int i=0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF)
{
if (i < MAX)
{
string[i++] = ch;
ch = getchar();
}
}
string[i] = '\0';
return i;
}
Upvotes: 1
Views: 1035
Reputation: 474
You need to take care that you should not copy the same data into temp array.
You can check following simple logic,
1) Find the randno r_num
2) Swap. str[r_num] and str[maxlen]
3) Decrement max len by 1
4) Repeat
for ( i = 0; i < 8; i++)
{
// Commenting your code
//r_num = rand()%8+1;
//strcpy(temp[r_num], str[i]);
memset(temp1,0,sizeof(temp1));
r_num = (rand()%(8-i))+1;
if(r_num != i) // Do not swap if r_num is max
{
//swapping
strcpy(temp1,str[r_num]);
strcpy(str[r_num],str[7-i]);
strcpy(str[7-i],temp1);
}
}
I hope this will help you :)
Upvotes: 1
Reputation: 1663
Before storing a input line string[i]
in a random output line temp[r_num]
, you have just to verify if the selected output is free.
First step, initialize the output array
temp[8][MAX]
from the first loop:
for(i=0; i<8; ++i)
{
strcpy(temp[i],""); // set each output line to free (empty).
readLine(str[i]);
}
Second step, loop on the randomize selector while the selected string is not output: The random index shall be from 0 to 7.
for (i = 0; i < 8; i++)
{
do {
r_num = rand()%8; // index is between 0 and 7
} while (strlen(temp[r_num])!=0); // output shall be free/empty
strcpy(temp[r_num], str[i]);
}
Instead of:
for (int i = 0; i < 8; i++)
{
r_num = rand()%8+1; // error index start from 0
strcpy(temp[r_num], str[i]);
}
Upvotes: 0
Reputation: 75062
One of ways to shuffle data is picking from what is not selected yet.
int main(void)
{
int i;
char str[8][MAX];
int order[8]; /* order list */
srand((unsigned)time(NULL));
int r_num;
printf("Enter 8 words:\n");
for(i=0; i<8; ++i)
{
readLine(str[i]);
}
/* initialize order list */
for (i = 0; i < 8; i++)
{
order[i] = i;
}
for (i = 7; i > 0; i--) /* select from back to front */
{
int pos = rand() % (i + 1); /* pick one position randomly */
/* pick selected element by swapping */
int temp = order[pos];
order[pos] = order[i];
order[i] = temp;
}
printf("\nRandom order of words: \n");
for(i=0; i<8; ++i)
{
printf("%s\n", str[order[i]]); /* print strings in the shuffled order */
}
printf("\n");
return 0;
}
Upvotes: 1