CO311: Introduction to Modern Programming II

Lab Practical #5: February 28, 2000

You will be randomly assigned one of two programs to write. Please read the entire problem before jumping into the code! Your program should consist of several functions in addition to main().

Use good programming style including useful object/variable names, and descriptive function names. Take a moment before jumping into the code to sketch out the framework of the program: what objects are needed to store what information, which functions you’ll be needing (including the input and output of the function itself), etc. Only once you have an outline for your program should you begin to code it! Use comments in your code where you know what should occur, but you can’t get it to work: this may help to improve your score if I can tell that your understanding of the concept was correct, but you’re just getting stuck on the implementation. Turning in your non-code design notes may also help in the same fashion, and will be accepted after the test is over.

Keep in mind that the “open note, open book” test format includes the website: you may “borrow” from examples and answers where appropriate to help speed up the coding process. As with all tests like this, however, you should know what you’re looking for before you go looking, or you’ll waste time trying to apply inappropriate examples to your problem.

Submit the finished CPP file to the FTP site in its own folder called P5.

You will have 60 minutes to complete the assignment; late submissions will be penalized 10 points for every 5 minutes late.

The following are the two different program requirements:

Problem A

Read a data file specified by the user containing a set of floating point numbers. There will be somewhere from 2 to 1000 numbers, but never more than 1000 numbers, one number per line. Display the following results with descriptive headings to the console (screen):

A sample data file can be found on the FTP site in the COMMON folder called data-a.txt.

Problem B

Create a program that generates random floating point numbers and saves them to a file, one number per line. The program should query the user for the following criteria:

The program should generate a new set of random numbers each time it is run.

Hints for working with random numbers:

By taking the modulus of the random number and some limit, you can generate a range of smaller numbers (e.g.: rand()%15 gives you numbers from 0 to 14). Because these numbers are integers, they have NO digits to the right of the decimal point.

You can move the decimal point any number of places by dividing by powers of 10 (e.g.: 345 / 102 = 3.45).

If you combine the two concepts, you can use the following formula to create a range from 0 to X with at most Y digits to the right of the decimal point:

[ random % X ] + [ (random % 10Y)  / 10Y ]

The first part generates the left side of the decimal point. The second part generates the right-hand side of the decimal point, and works like this: If your “maximum digits” is 3, then the 10Y part evaluates to 1000. This limits the random numbers to the range 0..999 in one place, then moves the decimal three places to give you the range 0.0 to 0.999. Adding the two parts together gives you numbers in the range 0 to 19.999 if the “maximum value” is 20. You will need to keep track of the type when coding this, however, because the modulus operator (%) will only work on integers, and the “decimal shift” trick only works on floating-point numbers.

General Hints

In order to use a string object to specify a filename when creating an ifstream or ofstream object, you must use the c_str() string member function. For example, if you had declared the following:

string name;

and then needed to use the data stored in name to open a file to read or write, you use one of the following: