Docsity

C++ Programming Assignment: Map Distance Calculation - Prof. Noah D. Barnette, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS 1044 Program 1 Fall 2008
Due Date: Check Web Site Page 1 of 7
Learning to Use the Development Environment
Obviously you cannot program unless you understand how to create a file containing your C++ language source code, how
to compile and link the source code to produce a program, how to execute (run) the program, test its behavior, fix errors,
etc. For the first programming project, you will be given C++ source code for a program that meets the specification given
below. Your task is to type in the given source code, without modification, and verify that it does indeed perform as
specified. Along the way, you'll be exposed to quite a bit of C++ language that we hasn’t yet been covered in class, so don't
get paranoid. Try to understand the given source code (there are lots of comments included to help), but realize that this
program uses elements of the C++ language from later topics in the course notes.
What to turn in and how:
This project will be submitted to the Curator automated grader. Instructions for submitting to the Curator grader are linked
from the CS1044 Web home page. Be sure to follow those instructions carefully. In particular, do not alter the labels used
in the sample output file below, and do not produce additional lines of output for aesthetic reasons. No hard-copy
submission is required for this project.
The Program Specification:
Arithmetic Expressions and File I/O: Map Mileage
You are to write, (actually its already been written for you), a very simple map distance calculation program, which will
read data from a file, perform some arithmetic calculations, and write results in a nice tabular form to a file. The input and
output files formats are described below, with examples. Be sure to pay careful attention to both.
Input file description and sample:
Your program must read its input from a file named “inmap.dat” — use of another input file name will result in
massive deductions. The first line of the input file will contain two values, separated by whitespace:
• Number of places to visit (integer between 1 and 100)
• Map scale factor (real number between 0.0 and 1000.0)
The first value represents the total number of locations on a map to which one wishes to travel in sequence. The second
value represents the number of miles per inch on the map.
Each following line of the input file will contain one value representing the map distance in inches between each sequential
pair of locations to be visited:
• Distance between (real number between 0.0 and 1000.0)
next two places
You may assume that all input values will be logically correct (no negatives, for instance), and that all values will be in the
specified ranges. For instance:
4 0.25
1.5
2.3
5.9
4.0
Note that you must not make any assumptions about the number of lines of data in the input file. Your program must be
written so that it will detect when it's out of input and terminate correctly, (a technique for this will be presented at a later
time in the course).
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming Assignment: Map Distance Calculation - Prof. Noah D. Barnette and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Learning to Use the Development Environment

Obviously you cannot program unless you understand how to create a file containing your C++ language source code, how to compile and link the source code to produce a program, how to execute (run) the program, test its behavior, fix errors, etc. For the first programming project, you will be given C++ source code for a program that meets the specification given below. Your task is to type in the given source code, without modification, and verify that it does indeed perform as specified. Along the way, you'll be exposed to quite a bit of C++ language that we hasn’t yet been covered in class, so don't get paranoid. Try to understand the given source code (there are lots of comments included to help), but realize that this program uses elements of the C++ language from later topics in the course notes.

What to turn in and how:

This project will be submitted to the Curator automated grader. Instructions for submitting to the Curator grader are linked from the CS1044 Web home page. Be sure to follow those instructions carefully. In particular, do not alter the labels used in the sample output file below, and do not produce additional lines of output for aesthetic reasons. No hard-copy submission is required for this project.

The Program Specification:

Arithmetic Expressions and File I/O: Map Mileage

You are to write, (actually its already been written for you), a very simple map distance calculation program, which will read data from a file, perform some arithmetic calculations, and write results in a nice tabular form to a file. The input and output files formats are described below, with examples. Be sure to pay careful attention to both.

Input file description and sample:

Your program must read its input from a file named “inmap.dat” — use of another input file name will result in massive deductions. The first line of the input file will contain two values, separated by whitespace:

  • Number of places to visit (integer between 1 and 100)
  • Map scale factor (real number between 0.0 and 1000.0)

The first value represents the total number of locations on a map to which one wishes to travel in sequence. The second value represents the number of miles per inch on the map.

Each following line of the input file will contain one value representing the map distance in inches between each sequential pair of locations to be visited:

  • Distance between (real number between 0.0 and 1000.0) next two places

You may assume that all input values will be logically correct (no negatives, for instance), and that all values will be in the specified ranges. For instance:

Note that you must not make any assumptions about the number of lines of data in the input file. Your program must be written so that it will detect when it's out of input and terminate correctly, (a technique for this will be presented at a later time in the course).

What to Calculate:

You will write a program that computes the number of miles between two locations on a map given their distance in inches and the scale factor of the map. The map distances in inches and the corresponding computed mileage distances between each two locations must be output to the nearest tenth of an inch and mile. All of the distances are to be output in a labeled tabular format as shown below. In addition, the total mileage traveled between all of the distances on the map will be output to the nearest tenth of a mile.

Output description and sample:

Your program must write output data to a file named “outmap.dat” — use of any other output file name will result in a massive deduction of points.

The sample output file shown below corresponds to the input data given above:

Dwight Barnette Simple Map Distance Computations

Map Scale Factor: 0.25 miles per inch

Map Mileage Measure Distance ============================================================

1 1.5 0.

2 2.3 0.

3 5.9 1.

4 4.0 1.

============================================================ Total Distance: 3.5 miles

You are not required to use this exact spacing, but your output must satisfy the following requirements:

  • All distances (inches and miles) must be printed to show one place after the decimal. The map scale factor is to be printed to two decimal places.
  • You must use the specified header, line and column labels, and include your name as shown.
  • You must arrange your output in neatly aligned columns, with a label identifying the contents of each column.
  • You must show the required total distance below the corresponding columns.
  • You must use the same ordering of the columns as shown here.

The Source Code:

// On my honor: // // - I have not discussed the C++ language code in my program with // anyone other than my instructor or the teaching assistants // assigned to this course. // // - I have not used C++ language code obtained from another student, // or any other unauthorized source, either modified or unmodified. // // - If any C++ language code or documentation used in my program // was obtained from another source, such as a text book or course // notes, that has been clearly noted with a proper citation in // the comments of my program. // // - I have not designed this program in such a way as to defeat or // interfere with the normal operation of the Curator Server. // // // Mapmiles.cpp: A Simple Map Mileage Program // // You should substitute the correct information in the following // lines: // // Programmers: N.D., C.W. & M.H. // Modifier: Dwight Barnette // Course: CS // Compiler: Visual C++ version 6. // Platform: Intel Quad Core / Windows XP Pro x64 Build 3790 SP 2 // Date: Aug. 14, 2008 // //****************************************************************** // Walk program // This program computes the mileage (rounded to tenths of a mile) // for distances between points in a city, given // the measurements on a map with the scale the number of points // and the map distance between each. //****************************************************************** // // Purpose of the program: // // This program reads information about a a map and an arbitrary number of // distances between locations from an input file named "inmap.dat": // // Line 1: // - number of locations // - map scale factor (miles per inch) // Line x: (all following lines) // - distance between locations (inches) // // and computes the correct mileage for: // // - map distances // - Total distance traveled. // // It then prints out a labeled table of results, showing for // each location distance the actual mileage to reach it. // In addition, the total distance to be traveled is computed and // printed. //

// This is the #include section. These statements tell the compiler // to read and use variables and functions declared in the specified // files. //

#include #include // For setprecision()

using namespace std;

// Every C++ program must have a function named main. This is the // beginning of the definition of the main function for this // simple program. In this case the whole program consists of just // the main function --- usually programs involve more than one // function. //

int main( ) {

// This section is where named constants are declared and given // their values. We could just use the numbers themselves in the // code that follows, but the names make the code more readable; // the name DEFSCALE carries meaning where the number 0.25 doesn't. //

const double DEFSCALE = 0.25; // Default map scale (miles per inch)

// This section is where the variables used in main() are declared. // Each variable has a type (integer, float, etc) but no particular // value at this point. The variable names like the constant names // used above are supposed to be meaningful, and each declared // variable is also given a short comment explaining what it will // be used for. // ifstream inMap; // input file stream ofstream outMap; // output file stream

double mapScale, // Map scaling factor (miles per inch) distance, // Map distance between locations (inches) totMiles, // Total of rounded mileages miles; // An individual rounded mileage

int numPoints, // Number of locations on map to visit visited; // Number of locations' distances converted

// This is the initialization section of main(). We must make sure that // every variable is given a value before it is used in a calculation // or printed. Some variables get their values by being read from the // input file, or assigned so they don't need to be initialized here.

visited = 0; // Initialize counters and running totals totMiles = 0.0; // to zero. mapScale = DEFSCALE; // Initialize map scale factor to default value

// The following four statements prepare the input and output files // for use. Don't worry too much about what's going on here just yet, // we'll cover the details shortly. // inMap.open("inmap.dat"); outMap.open("outmap.dat");

// This section of the loop body prints the results calculated // above into the output file.

outMap << "#" <> distance;

} // End of while loop.

// Once we've exited the loop, we're done reading and processing data for // each location. It's time to sum things up. The following statements // prints out the total distance in a nice form. First print a line to mark // the end of the table body:

outMap << "============================================================"; outMap << endl; outMap << "Total Distance:" ; outMap << setw(7) << setprecision(1) ; outMap << totMiles << " miles" <