1

I'm writing a program that produces a unique sequence of random integers. It will ask the user for how many rows of random numbers they want to display, and the program will print a table of random numbers. The professor suggested using the srand() and time(NULL) functions in this program to make it work.

I've tried implementing the srand and time functions, but I haven't had any success. I've also tried writing a while statement but that hasn't been working out.

Code

int rows;
int main() {

        // Use srand() function to generate random numbers and print the rows that the user specified
        srand(time(NULL));
        //int rows;
        //int minRows = 0;
        //int maxRows = 15;
        printf("Please enter the number of rows you would like: ");
        scanf("%d", rows);
        while(rows<15) {
                printf("%d", rows);
         }
        return 0;
} //class main

I expect this program to print out the rows of random integers that the user specifies, but instead I get a 'Segmentation fault (core dumped)' error after I try inputting the number of rows that I'd like.

Emma
  • 27,428
  • 11
  • 44
  • 69
Marco
  • 11
  • 2
  • 7
    the mistake is in your scanf it should be scanf("%d", &rows); – Spinkoo Mar 30 '19 at 21:17
  • while @Spinkoo is right, he doesnt address the problem in question. In the snippet you're just outputting the number of `rows` the user entered `rows` times. What you'd wanna do is first decide a boundary for your random numbers because they'll be in between 0 and `RAND_MAX`, say `upper_boundary`, then calling the `rand()` function like `printf("%d", rand() % upper_boundary)` within the loop. I strongly encourage you to check out [this link](https://www.tutorialspoint.com/c_standard_library/c_function_rand.htm) for the `rand()` function. And please, conduct a google search before asking. – alegria Mar 30 '19 at 21:42

0 Answers0