-1

I'm going to write a quiz game. I have a txt file full of questions and answers. Each question is in a different row and has a difficulty level (1-10) (not sorted). I want to read in and store them in an array. (I did a question structure). My question is: How should I read in to an array randomly? A question's index would be its difficulty level-1.

Kara
  • 6,115
  • 16
  • 50
  • 57
Goba
  • 65
  • 5
  • Have you already read the questions into an array? If you have you can simply shuffle the array. – Linus Nov 02 '15 at 12:00
  • 3
    Don't read randomly , read whole data using `fgets` or `fscanf` as per need and then get question according to index you want. – ameyCU Nov 02 '15 at 12:02

1 Answers1

0

You do not have to randomly read them into the array. What you do is simply to read them and store them in the array and then you can simply shuffle the array and go through it.

For shuffling an array you can refer to this Stack Overflow post or try to modify the code from here a little to make it work with char*.

An other option would be to store them in the array and then get random indexes. You can generate a random number with:

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

srand(time(NULL));
int r = rand()%array_size;
Community
  • 1
  • 1
LBes
  • 3,366
  • 1
  • 32
  • 66