-1

I have an array of 6 rows and 20 columns :

char name[6][20];

And I enter the names with for :

puts("Enter with 6 names :");

for(i=0; i< 6 ; i++)
{
   scanf("%19[^\n]%*c",name[i]);
}

After that, I need to randomly choose three names of the array and display them in the screen. How can I do that ?

PS : Different from the other questions similar to that, I want not to take just one word, but the full word of the array.

Mondial
  • 119
  • 1
  • 3
  • 6
    Generate a random number in the range [0-5] and use it to pick the name... – Eugene Sh. May 24 '17 at 17:13
  • What is the difference between "just one word" and "the full word"? – Scott Hunter May 24 '17 at 17:14
  • @ScottHunter Hey, you won't ask it about "one time job" and "full time job" ? :) – Eugene Sh. May 24 '17 at 17:15
  • Going off of what @EugeneSh. said look at the accepted answer here https://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c BTW, you have 6 rows. – jiveturkey May 24 '17 at 17:15
  • Did you mean to pick three *different* names? – Weather Vane May 24 '17 at 17:26
  • @WeatherVane To get the names from the array. 3 of them. – Mondial May 24 '17 at 18:02
  • I understand "3". But suppose the names are Albert, Boris, Charlie, Dean and Edgar (lets say 5 as originally posted, not the 6 you meant). Are you allowed to get Boris, Boris and Dean as the three (3) names? These are not three *different* names, there is some repetition. My question is: do you want three *different* names? – Weather Vane May 24 '17 at 18:05
  • If you search on Stack Overflow with the term '`[c] random array`', there are many questions asking for roughly what you're looking for. Many of them will have answers that will work for you, whether duplicate names are acceptable or not. You can consider that you're generating an array with 3 entries that do not repeat, or you could be shuffling fairly an array of 6 entries with the values 0..5 and then using the first 3 entries in the shuffled array as the indexes of the names, or … – Jonathan Leffler May 24 '17 at 22:58
  • @WeatherVane It need to be only 6 names, and 3 of them without repeating the names itself. – Mondial May 25 '17 at 19:23

2 Answers2

1

Here's a possible solution to your problem, assuming you've stored the array of names, just create an array of positions and then shuffle it few times so the positions will be random ones, finally pick 3 positions (for instance, the first 3 ones):

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

#define ROWS 6
#define COL 20
#define RND_NAMES 3

void shuffle(int *array, int n, int num_shuffles) {
    srand((unsigned)time(NULL));
    for (int j = 0; j < num_shuffles; j++) {
        for (int 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;
        }
    }
}

int main() {
    int i;
    char name[ROWS][COL] = {"name1", "name2", "name3",
                            "name4", "name5", "name6"};
    int positions[ROWS] = {0, 1, 2, 3, 4, 5};
    shuffle(positions, ROWS, 100);

    printf("%s\n", name[positions[0]]);
    printf("%s\n", name[positions[1]]);
    printf("%s\n", name[positions[2]]);

    return 0;
}

With this way, you're guaranteed to pick up 3 random non-repeated names.

BPL
  • 9,632
  • 9
  • 59
  • 117
  • Thanks for the code, it worked amazing. I am new to C, and I don't know what this shuffle is doing, why there is 100 there ? Can you explain to me, please? – Mondial May 25 '17 at 19:26
  • The function shuffles the positions - 100 times, that might be overkill, perhaps once would be enough. – Weather Vane May 25 '17 at 19:29
  • @Mondial Sure, as Weather Vane suggested maybe shuffling 100 times is too much and shuffling only once is good enough. Imagine a card deck and a dealer, now... imagine he's shuffled once the whole deck... and you ask him to shuffle 99 more times because you're extra paranoid about cheating, here's the same :) – BPL May 25 '17 at 19:50
0

Here I wrote a simple solution for what you're trying to achieve.

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

#define ROWS      6
#define COL       20
#define RND_NAMES 3

int main()
{
    int i;
    char name[ROWS][COL];

    // init pseudo-random number generator
    srand((unsigned)time(NULL));

    puts("Enter with 6 names: ");

    for (i=0; i < ROWS; i++) {
        scanf("%19[^\n]%*c", name[i]);
    }

    puts("Random names: ");

    for (i=0; i < RND_NAMES; i++) {
        printf("%s\n", name[rand() % ROWS]);
    }

    return 0;   
}
Vic
  • 301
  • 1
  • 9