Question

Write a program in C to generate random numbers. The program recieves 4 items on activation...

Write a program in C to generate random numbers. The program recieves 4 items on activation through argv:

1. Name of the data file

2. Number of items for program to generate

3. Range of numbers

4. Seed of the random number generator

Example run: ./rand datafile.txt 20 1000 3127

Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate the number of items, all numbers are generated by rand() call initiated by seed and are limited using range and modular division, all generated numbers are saved in datafile.txt, and finally the program must read the file by printing all numbers separated by a tab in rows of 10 until the end of the file.

Only use raw I/O interaction with appropriate buffers. Only use printf, fgets, scanf, read/write system calls (no cin/cout). Also check that argc takes the correct amount of parameters (5 counting rand executable).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

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

if(argc!=5){

printf("USAGE: ./rand <out-filename> <no-of-items> <range> <seed>\n");

return 0;

}

char filename[100];

strcpy(filename, argv[1]);

int n = atoi(argv[2]);

int range = atoi(argv[3]);

int seed = atoi(argv[4]);

srand(seed);

FILE *fp;

fp = fopen(filename, "w");

for(int i=0; i<n; i++){

fprintf(fp, "%d\n", rand()%range);

}

fclose(fp);

printf("Successfully stored into file\n");

fp = fopen(filename, "r");

int i=0, num;

printf("File contents in file is: \n");

while(!feof(fp)){

if(fscanf(fp, "%d", &num)<0)

break;

printf("%d\t", num);

i++;

if(i%10==0)

printf("\n");

}

printf("\n");

return 0;

}

nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/29092018$ gcc gen_rand.c -o rand nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/29092018$ ./rand USAGE: ./rand <out-filename> <no-of-items><range> <seed> nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/29092018$ ./rand datafile.txt 20 1000 3127 Successfully stored into file File contents in file is: 881 544 522 266 252 591 173 47 488 161 653 423 625 174 595 176 916 324 917

Add a comment
Know the answer?
Add Answer to:
Write a program in C to generate random numbers. The program recieves 4 items on activation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write program above in C only Write a program to find statistics on some random numbers....

    write program above in C only Write a program to find statistics on some random numbers. Seed the random number generator with time. In a for loop generate 12 random numbers between the values of 2 and 20. Print the numbers. As the numbers are generated, find the following: the number of even numbers (those evenly divisible by 2) e the sum of the even numbers the product of the even numbers the maximum value of all the numbers e

  • For this assignment, write a program that will generate random numbers in the range of 50-100...

    For this assignment, write a program that will generate random numbers in the range of 50-100 and count how many fall into particular ranges. Processing: The program should first seed the random number generator. This is done one time in the program. Use srand(0). Next, generate and display a series of 10 random numbers between 50 and 100. There are several ways to ensure that a random number falls between 50 and 100. One possibility is to use the following...

  • Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers...

    Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers 1-10, in an output.txt file. Fill the generated numbers in two such class objects (first 16 numbers in one matrix and next 16 numbers in another. Create member functions each for adding, subtracting and multiplication of matrices. Write the algorithm defining steps of your program.

  • Javascript Write a program to generate random numbers between a user-specified range, then count and display...

    Javascript Write a program to generate random numbers between a user-specified range, then count and display the frequencies of the most / least appeared numbers. Sample Output How many random numbers? 1000000 Enter the minimum number: 100 Enter the maximum number: 200 Generated 1000000 random numbers between 100 (inclusive) and 200 (exclusive). Number 133 has the most occurrences (10197). Number 154 has the least occurrences (9749).

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • Help please! Program in C Problem-4 Write a function that receives an unsigned number and generates...

    Help please! Program in C Problem-4 Write a function that receives an unsigned number and generates and returns a new number that may or may not have an error in it. An error on a random bit must be introduced randomly. Use the rand function to generate 0 or 1 to see if the error needs to be generated and use a rand function in the range of 1 to 32 to see on which bit positon the error needs...

  • programming language Write a program that will generate 5 random numbers between 20 and 80 and...

    programming language Write a program that will generate 5 random numbers between 20 and 80 and assign each to a variable of datatype double. You should use a random number generator seeded with time Using a series of if statements, determine which of the 5 variables contains the smallest value generated Print all five random numbers to the screen and print out the value that you determined was the smallest. All values should be printed with zero decimal places. 2.

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT