Question

Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error.

For instance,

2 5 7  means that you need to generate 7 random numbers in the range of 2 - 5 (including 2 and 5).

You need to develop a complete C++ program to read each line from the .txt file and print the result on screen in a formatted way. The output format will be <first number> - <second number>: <random numbers > . For the above example, the output to screen should be

2 - 5: 6 5 4 6 3 7 5

The attached input file has,

3 6 8
2 19 10
4 12 4
1 6 0
7 4 3
0 8 3
10 9 8

Your program should generate this (or with different random numbers) output for this file

3 - 6: 5 4 3 5 6 3 4 5
2 - 19: 6 2 11 14 17 9 8 19 2 7
4 - 12: 5 4 10 9
1 - 6:                         // no output here since quantity is 0
7 - 4: error
0 - 8: 0 6 4
10 - 9: error

Note that your program should any number of lines in the file. You strictly assume that all numbers are in the range of [0,50].

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

#include <iostream>
#include<sstream>
#include<fstream>
#include<time.h>
#include<random>
using namespace std;

int main()
{
// Open file to read
fstream fileIn;
fileIn.open("RandSource.txt");

// Check if file exists, If not end the program
if(!fileIn)
{
cout<<"File cannot be opened"<<endl;
return 0;
}

srand(time(NULL));
string line = "";
int low,high,totalNum;

// Iterate the loop till the last line in file
while(getline(fileIn,line))
{
// Read through stringstream and then store each respective value to its variables
stringstream ss(line);
ss>>low>>high>>totalNum;

// Print low-high
cout<<low<<" - "<<high<<": ";

// If low>high, print error
if(low>high)
{
cout<<"error"<<endl;
}
else
{
// Generate random number between given range totalNum times
// and print it
for(int i = 1; i<=totalNum; i++)
{
int val = rand()%(high-low+1)+low;
cout<<val<<" ";
}
cout<<endl;
}

}
// Close the file
fileIn.close();
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Assume that you will be given a *.txt file, which in the same format of RandSource.txt...
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
  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space....

    Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and adds all the integers between those two numbers (starting from the first number and ending at the second. Show your output in B1.txt. Sample Input: 1 6 18 74 29 38 Sample Output: 21 2622 335 (For further clarification, the first line of the sample output was done by 1+2+3+4+5+6 =...

  • Assume that an array A is given to you in a txt file. You should read...

    Assume that an array A is given to you in a txt file. You should read it. If you think this is a time-dependent data, print the number of local minimums in the array to a txt file. Local minimum means the element which is less than the previous element and the following element. Please do not use any additional library. For example, if A=[3, 2, 9, 8, 7, 8, 6], the code should print 2 and 7. (Local minimums...

  • Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

    Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample Input: 3 17 42 91 Sample Output: 6...

  • In C++, You are given a file of customer data with records in the following format...

    In C++, You are given a file of customer data with records in the following format : account number, last name, first name, middle name. Write a program to convert the input data and display it to the monitor in the following format : Accout Name     Logon ID Initial Pswd 21-88282712-B Keith S. Adams ADAM21 Adam88282 08-36847734-A John R. Sturm STUR08 Stur36847    etc. Notes :     1) The account number on the input is 10 digits     2)  The names on...

  • Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

    Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt. Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code. File format: apple banana pear . . . Hint: Program will need to utilize double pointers. More Hints: 1. Assume no word is bigger that 50...

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

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