Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project you will be writing a program (which in most IDEs, would mean separate projects created) to create a file filled with random

In this project you will be writing a program (which in most IDEs, would mean separate projects created) to create a file filled with random numbers. The next part of the program will then test to see if any of those numbers are prime.

[Task 1] Your first task is to write a function that will generate random numbers(from 1-100) and save them to a particular file, with the name given by the user. So your program will start by prompting the user for a string (the filename it will create). It should then create that file and then generate a random number from 1-20. It should then write that number to a file, followed by an endl character. Suppose that number is stored in the variable num. It should then go into a loop, based on that number, and generate a random number from 1-100 and write that number to a file, followed by an endl. This should result in a text file that internally looks something like this.

7

50

100

5

13

7

8

15

This function should then close the file.

[Task 2] Write another function that reads in data from a file given by the user (as a string, so prompt the user for this string, as this could be different from the first part). Your program should then read in the first number, which will tell it how many numbers are in the rest of the file. The program should then go into a loop processing each of those numbers.

How should it process it? It should use a function that checks to see whether the number is prime. If the number is prime, it should add it to a total variable.

When the function is done, it should output the total number of primes and the sum total of the prime numbers only. So a file of the numbers below were given:

4

7

7

7

8

it should output an average of 7 and a sum of 21.

You are required to use a function to discover if a number is prime. This function should return true or false based on if the number is a prime number or not. It should have the following prototype:

bool isAPrime(int numberToTest);

Probably the easiest way to test whether a number num is prime is to simply try to divide it by 2, 3, ..., up to num-1. If all of these divisions lead to a remainder left over, it is prime.

Verification: You must ensure that the program outputs an error message if any of the file reads fail or if the files cannot be opened. Your program should also output an error message if the total numbers to test is not equal to the number entered. For example, the following cases of files should lead to an error:

3

7

8

9

10

or:

3

7

8

You are NOT required to test for what would happen if there is a non-digit in the file. NOTE: I will be testing your programs, with my own files and the files your program generates, and they will only have a combination of spaces, digits and endline characters.

Hints:

1) Write the first function. Make sure it works

2) Write the second function, but just read from the file and output the numbers to make sure you are reading the file correctly.

3) Write the prime function and test to see that it works with fixed numbers

4) Then total up the prime numbers and get their average

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago