-2

I need to get Questions from a file and then randomly show them to enter answers. What I have done was I generate a random number and then file is reading line by line.When it meets the randomize number it will shows the relevant line. now all working but i don't want to duplicate numbers.How can I fix this.

Here what i tried

   int main()
{
    int numb;
    int answer;
    int ar[5];
    int count =0;

    numb = randomgen();
    ar[count]=numb;
    char input[512];

    printf("Line number to print :%d\n",numb);
    count++;
    while(count != 6)
    {
        FILE * pToFile= fopen("data.txt","r");
        int line =0;
        while(fgets(input,512,pToFile))
            {
                line++;
                if(line == numb)
                {
                    printf(" %s",input);
                }

            }

            printf("Enter the answer:");

            scanf("%d",&answer);
            printf("\n");
            answermethod(numb,answer); //to check the answers ;WORKING

            numb = randomgen2(ar);
            ar[count] = numb;
            count++;

            fclose(pToFile);
    }

    return 0;

}
    int randomgen()
    {
         int r;
    srand(time(NULL));
    r=rand()%(5)+1;
    return r;

    }


    int randomgen2(int ars[]) //generate random n and search it in the 
                            //array if it is not in array return the random 
                            //  value.But this is not working
    {
    int r;
    srand(time(NULL));
     r=rand()%(5)+1;
    int num,i,c=0;

    int n= sizeof(ars)/sizeof(ars[0]);
          for(i=0;i<n;i++)
          {
              if(ars[i]==r)
              {
                  //printf("%d",r);

                  randomgen2(ars);
                  c=1;
                  break;
              }

              else
              {
                   printf("not found%d",r);
                  c=0;
              }
          }

            if(c==0)
            {
                printf("nooo");
                return r;
            }
    }
pdm LVW
  • 149
  • 1
  • 11

1 Answers1

1

Uninitialized variables and the uninitialized array ar will cause undefined behavior.

The file needs to be opened only once. Use rewind to go back to the begining.

As noted in comments, use srand only once. And you have pass the array size to the function because in foo(int ar[]) the size of ar is the pointer size, not the allocation size.

Just check to see if the random number is used already. If it is not used, then store it in the array. The range of random number and array size should match exactly. Don't add +1

int main(void)
{
    srand((unsigned int)time(NULL));
    FILE *pToFile = fopen("data.txt", "r");
    char input[512];
    int line;
    int arr[5];
    int array_size = sizeof(arr) / sizeof(arr[0]);

    for (int count = 0; count < array_size; count++)
    {
        arr[count] = -1; // initialize to -1 to indicate it is not set
        while(arr[count] == -1)
        {
            arr[count] = rand() % array_size;
            for(int i = 0; i < count; i++)
                if(arr[i] == arr[count])
                    arr[count] = -1;
        }

        rewind(pToFile);
        line = 0;
        while(fgets(input, 512, pToFile))
        {
            if(line++ == arr[count])
            {
                printf("%s", input);
                break;
            }
        }
    }

    fclose(pToFile);
    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77