1

I have been thinking on this for a while. I was given this assignment that says the following:

There is a text file containing numbers. (MIN, MAX, NUMBER) I have to read the contents of the file, then generate a random number between MIN and MAX. If generated number matches the number, then print "Match."

MIN: 7 MAX: 17 NUMBER: 15 ...

I was able to read every number into an array, with this code:

int main(void)
{
    srand(time(NULL));
    int nums[50] = {0};
    int i = 0;
    FILE * fp;
    /*if(fp == NULL) return 1;*/

    if (fp = fopen("numbers.txt", "r")) {
        while (fscanf(fp, "%d", &nums[i]) != EOF) {
            ++i;
        }
        fclose(fp);
    }

    for (--i; i >= 0; --i)
        printf("num[%d] = %d\n", i, nums[i]);

    return 0;
    }

However, I have no idea how to continue. How can I assign the specific values to my random number generator function?

int random(int min, int max) {
    return rand()%(max-min+1)+min;
}
8
  • if(fp == NULL) return 1; ?? You just declared that variable on the previous line with indeterminate content. That line seems pretty worthless (and invokes undefined behavior as well). Commented Apr 19, 2016 at 19:51
  • 1
    Regarding your problem. no array is needed for this task. You need three int variables to read the data (minval, maxval, testval) from the current line.Then, using minval and maxval, compute your random number and see if it is the testval. If it is, report Match, otherwise, not. You can do it all inside one single-scanning for-loop. Commented Apr 19, 2016 at 19:55
  • And how can I specifically read the data? For example: the first number should be min, the second max, the third the test. Commented Apr 19, 2016 at 19:57
  • How did you read the one value you're reading now ? That is your code, right ? Commented Apr 19, 2016 at 19:58
  • 1
    Exactly. There ya go. Knew you'd see it eventually. Commented Apr 19, 2016 at 20:09

1 Answer 1

0

Solution -

You shouldn't use an array since you don't need to store the numbers, you only need to see them once to accomplish your task. We can calculate a random number within our constraints as soon as we read in the numbers. Let's do something like this:

int main(void)
{
    srand(time(NULL));
    int nums[50] = {0};
    int min = 0, max = 0, match= 0;
    FILE * fp;

    fp = fopen("numbers.txt", "r")

    if(fp == NULL) {
        return 1;
    }

    // Compare the return value of fscanf to 3 to make sure
    // three values were read in from the file during each
    // iteration.
    while (fscanf(fp, "%d %d %d", &min, &max, &match) == 3) {
        if(random(min, max) == match) {
            printf("Match.\n");
        } else {
            //No match!
        }
    }

    fclose(fp);

    return 0;
}

int random(int min, int max) {
    return rand() % (max - min + 1) + min;
}

A few other issues with your code could be improved:

  • What are you using srand for? I don't see it used, so I'll take it out for this bit of code. No need for extra distractions.

    srand(time(NULL));    // Removing this line.
    
  • Why have you initialized nums to {0}? It makes the purpose unclear, and you should just leave it uninitialized.

    int nums[50] = {0};
    

Could also be:

int nums[50];
  • We can clean up the initial part of the file opening, from:

    /*if(fp == NULL) return 1;*/
    
    if (fp = fopen("numbers.txt", "r")) {
        ...
    }
    

To this:

fp = fopen("somenumbers.txt", "r");

if(fp == NULL) {
    //Tell the user an error occured.
    return 1;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.