Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program,randint.c, to generate a given number of random integers in an

ID: 3672717 • Letter: W

Question

Write a C program,randint.c, to generate a given number of random integers
in an interval and save them to a text file. Your program will be run from the
command line as follows:
$ ./randint -n 512 -l 11 -h 33 -o numbers.txt

randint: name of your program (executable file name)
-n 512: generate 512 random integers
-l 11: the integers should be greater than or equal to 11
-h 33: the integers should be less than or equal to 33
-o numbers.txt: save the generated integers to this file; the first line in the file should be the number of integers in the file, e.g., 512.

Explanation / Answer

Here is the required code. I have used some predefined functions listed below:

atoi: For converting string to integer.

Sleep: Here, this function has certain functionality. Delaying the system so that it can generate different function. (Other options are also available.)

#include<conio.h>

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

#include<windows.h>

#define SLEEPTIME 500

//Here SLEEPTIME is very important. As because of the computer speed, in normal flow random function can have same values.

int main( int argc, char *argv[] )

{

int totalNum;

int lowerLimit;

int upperLimit;

totalNum = atoi(argv[2]);

lowerLimit = atoi(argv[4]);

upperLimit = atoi(argv[6]);

int n;

FILE *fptr;

fptr=fopen(argv[8],"w"); //argv[8] is file name. Specified in the output

if(fptr==NULL)

{

printf("Error!");

exit(1);

}

fprintf(fptr,"%d ",totalNum);

for(int i = 0; i < totalNum; i++)

{

srand(time(NULL));

int r = rand()%(upperLimit-lowerLimit+1)+lowerLimit; // Here mod function is used to make the range of random number lie between lower and higher limit.

printf(" %d", r);

Sleep(SLEEPTIME);

fprintf(fptr," %d",r);

}

fclose(fptr);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote