CS 1044 Project 1 Fall 2011

Similar documents
CS 1044 Project 2 Spring 2003

Decision Logic: if, if else, switch, Boolean conditions and variables

CS 1044 Program 2 Spring 2002

The Program Specification:

CS 1044 Project 5 Fall 2009

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

CS 1044 Program 6 Summer I dimension ??????

CS 1044 Program 1 Fall 1998

CS 2604 Minor Project 1 DRAFT Fall 2000

CS 2604 Minor Project 1 Summer 2000

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

gcc o driver std=c99 -Wall driver.c bigmesa.c

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

Given that much information about two such rectangles, it is possible to determine whether they intersect.

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

CS 1044 Programming in C++ Test 1 Spring 2011 Form B Page 1 of 12 READ THIS NOW!

3. When you process a largest recent earthquake query, you should print out:

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

Pointer Casts and Data Accesses

gcc o driver std=c99 -Wall driver.c everynth.c

CS 142 Style Guide Grading and Details

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS2304 Spring 2014 Project 3

READ THIS NOW! Do not start the test until instructed to do so!

CS ) PROGRAMMING ASSIGNMENT 11:00 PM 11:00 PM

CS 2604 Minor Project 3 DRAFT Summer 2000

Chapter 2 Basic Elements of C++

C++ Input/Output: Streams

CS 2704 Project 2: Elevator Simulation Fall 1999

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

Setting Justification

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

Creating a String Data Type in C

I/O Streams and Standard I/O Devices (cont d.)

Objectives. In this chapter, you will:

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

We will exclusively use streams for input and output of data. Intro Programming in C++

a f b e c d Figure 1 Figure 2 Figure 3

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

C++ for Python Programmers

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Week 3: File I/O and Formatting 3.7 Formatting Output

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

C++ Programming: From Problem Analysis to Program Design, Third Edition

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Expressions, Input, Output and Data Type Conversions

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Invoice Program with Arrays and Structures

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

CS 2704 Project 1 Spring 2001

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Full file at

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

To become familiar with array manipulation, searching, and sorting.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Do not turn to the next page until the start of the exam.

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Definition Matching (10 Points)

Chapter 3. Numeric Types, Expressions, and Output

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Chapter 3 - Notes Input/Output

Simple C Dynamic Data Structure

PROGRAMMING EXAMPLE: Checking Account Balance

CS 2704 Project 3 Spring 2000

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 111X - Fall Test 1

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

EECS 183 Fall 2015 Exam 2 Free Response (134 points)

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 3: Input/Output

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Sequential Program Execution

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Spring 2002 Page 1 of 8 READ THIS NOW!

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

This watermark does not appear in the registered version - Slide 1

Intro. Speed V Growth

Programming with C++ as a Second Language

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Engineering Problem Solving with C++, Etter/Ingber

File Navigation and Text Parsing in Java

Transcription:

Simple Arithmetic Calculations, Using Standard Functions One of the first things you will learn about C++ is how to perform numerical computations. In this project, you are given an incomplete program (see the end of this specification), which already includes the statements necessary to read input and specify output. You must add statements so that your completed program will perform the specified calculations. This programming assignment uses many of the ideas presented in sections 3 and 4 of the course notes, so you are advised to read those carefully. Read and follow the following program specification carefully. The following problem specification will be given as though you were creating the entire program. This will allow you to become familiar with the standard style of assignment specifications. The Program Specification: the Free-Falling Body Suppose an object is dropped from a point at a known distance above the ground and allowed to fall without any further interference; for example, a skydiver leaps from an airplane flying at an altitude of 5000 feet. An object that falls in this manner is said to be in free fall. The altitude of the object, its distance above the ground, will change as time passes. The velocity at which the object falls will also change (increase, actually) as time passes. Let H represent the altitude of the object and V represent its velocity. It s also clear, intuitively, that the altitude of the object depends on how its velocity changes; in fact, there s a nice mathematical relationship between the altitude and the velocity involving a definite integral. But we re interested only in the velocity of a free-falling object. It turns out that the velocity of a free-falling object is modeled by the formula: where: m g V tanh k g k m t m is the mass of the object g is the acceleration due to gravity t is the number of seconds the object has been in free fall k is a constant that is determined by the shape of the object and the density of the air through which it falls tanh( ) is the hyperbolic tangent function For simplicity, we will call k the drag constant. We also assume that the distance the object falls is small enough that the density of air doesn t change appreciably from its starting point down to the last point of interest. For example, suppose a skydiver weighing 130 lb exits her plane and falls for 3 seconds. Her velocity could be computed as follows. First of all, weight equals mass times g. Second, in English units, g is about 32.2. So, her mass m would be about 130/32.2 4.037. Applying the given formula: V 4.037 32.2 tanh 0.005 32.2 0.005 3 161.24 tanh(0.599) 86.48 4.037 giving her a velocity of about 86.48 feet per second or about 58.96 miles per hour, three seconds after her jump began. This problem is adapted from an exercise in Calculus, 9 th Ed., by Thomas and Finney, page 527. It s not necessary, or even useful to understand the derivation of the formula given above to complete this assignment. However, if you re interested, see the Appendix for Project 1 on the website. Due Date: Check Web Site Page 1 of 9

Input file description and sample: Your program must read its input from a file named fall.dat use of another input file name will result in massive deductions. The first line of the input file contains column labels which should be ignored. Each remaining line of the input file will contain three numbers, separated by whitespace: the weight of an object, assumed to be in pounds and greater than zero the drag coefficient of that object, assumed to be greater than zero the time the object has been falling, assumed to be in seconds You may assume that all the input values will be logically correct (no negatives, for instance). For instance: Weight k Time 572.900 0.008 26.3 120.400 0.006 23.1 0.048 0.040 17.8 127.500 0.236 6.7 157.100 0.024 12.7 550.300 0.081 24.6 535.000 0.165 6.1 240.700 0.040 22.4 3.330 0.010 16.6 290.100 0.026 19.4 The input values won't always have the nice patterns shown in this example so don't make any unjustified assumptions. Note that you must also 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 discussed in class and was used in the code from project 1; see also slides 4.24 4.25 in the course notes. What to Calculate: You will write a program which will read each line of the input file and use the given values to compute: the velocity of the object that data line describes, in feet per second the velocity of the object that data line describes, converted to miles per hour To perform these calculations, you will need to use the formula given above. Use the value 32.2 for g. The formula requires taking square roots and also using the hyperbolic tangent function. Fortunately, both of those may be found among the standard library functions in C++. The square root function is called sqrt( ); it takes one parameter of type double and returns the square root of that parameter. So, the statement: double aroot = sqrt(3.24); would assign the value 1.8 to the variable aroot. The hyperbolic tangent function is called tanh( ); it also takes one parameter of type double and returns the hyperbolic tangent of that parameter. So, the statement: double hyptangent = tanh(2.72); would assign the value 0.9914 to the variable hyptangent (approximately). You must use a compiler directive to include the standard header file cmath in order to use these functions. Due Date: Check Web Site Page 2 of 9

Output description and sample: The first line of your output should include your name only; the second line should include the title Falling Bodies only; the third line should be blank; the fourth line should display the column labels shown below; the fifth line should consist of some delimiting symbol to separate the column labels from the body of the table. Next your output file will contain a table, with one line of output for each line of numeric data in the input file. Each line of the table should list the object s mass (not its weight), its coefficient of drag, the amount of time it s been falling, and its velocity in both feet per second and miles per hour. There should be a line of delimiters immediately after the last line of the table body. The values for mass and the coefficient of drag should have precision 3 (i.e., show three digits after the decimal), while the velocity values should have precision 2 and the time should have precision 1. Your program must write output data to a file named fall.out 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 Falling Bodies mass drag time fps mph ---------------------------------------------- 17.792 0.008 26.3 266.65 181.81 3.739 0.006 23.1 141.65 96.58 0.001 0.040 17.8 1.10 0.75 3.960 0.236 6.7 23.24 15.85 4.879 0.024 12.7 80.90 55.16 17.090 0.081 24.6 82.42 56.20 16.615 0.165 6.1 56.83 38.75 7.475 0.040 22.4 77.57 52.89 0.103 0.010 16.6 18.25 12.44 9.009 0.026 19.4 105.63 72.02 ---------------------------------------------- You are not required to use this exact horizontal spacing, but your output must satisfy the following requirements: You must use variables of type double for all the real numbers. If you use float variables, your answers may not be as accurate as needed. All decimal values must be printed to show the same number of decimal places as in this sample. You must use the specified header and column labels, and include your name in the first line as shown. You must arrange your output in neatly aligned columns, with a label identifying the contents of each column. Note that while the Curator doesn t deduct points for horizontal alignment, the TA who grades your source code for programming standards may do so. You must use the same ordering of the columns as shown here. You must print a newline at the end of each line, including the line of hyphens marking the end of the table. Due Date: Check Web Site Page 3 of 9

Programming Standards: You'll be expected to observe good programming/documentation standards. All the discussions in class about formatting, structure, and commenting your code will be enforced. A copy of Elements of Programming Style is included with the course notes if you don't have a copy I strongly suggest you read the on-line edition (available from the course web page). Some specifics: You must include header comments specifying the programmer, their email, platform, compiler and operating system used and the date completed. Your header comment must describe what your program does; don't just plagiarize language from this spec. You must include comments explaining the purpose of every variable or named constant you use in your program. You must use meaningful identifier names that suggest the meaning or purpose of the constant, variable, function, etc. Use named constants instead of variables where appropriate. Precede every major block of your code with a comment explaining its purpose. You don't have to describe how it works unless you do something so sneaky it deserves special recognition. You must use indentation and blank lines to make control structures like loops and if-else statements more readable. Your submission that receives the highest score will be graded for adherence to these requirements, whether it is your last submission or not. If two or more of your submissions are tied for highest, the earliest of those will be graded. Therefore: implement and comment your C++ source code with these requirements in mind from the beginning rather than planning to clean up and add comments later. There are NO exceptions to this policy which will be in effect for all programming assignments in this course. Incremental Development: You ll find that it s easier and faster to produce a working program by practicing incremental development. In other words, don t try to solve the entire problem at once. First, develop your design. When the time comes to implement your design, do it piece by piece. Here s a suggested implementation strategy for this project: First, write the code necessary to read the entire input file. This should be similar to the input code used in the payroll program. To test your work, include code to write what you re reading (and nothing else) to the output file. There s not much point in worrying about processing the data further until you know you re reading it correctly. Second, add the code to compute the mass and change your output code to print that to the output file, instead of the weight. Also print the specified header to the output file. Verify that the results are correct; if not, determine why and fix the problem. Third, add the code to compute the velocity in feet per second and print that to the output file. Verify that the results are correct; if not, determine why and fix the problem. Fourth, add the code to convert the velocity from feet per second to miles per hour and print that to the output file. Verify that the results are correct; if not, determine why and fix the problem. Now you have a substantially complete program. At this point, you should clean up your code, eliminating any unnecessary instructions and fine-tuning the documentation you already wrote. Check your implementation and output again to be sure that you ve followed all the specifications given for this project, especially those in the Programming Standards section above. At this point, you re ready to submit your solution to the Grader. Due Date: Check Web Site Page 4 of 9

Hints: This program requires that you know how to manage file-oriented input/output operations the slides and the text provide good examples and guidelines. Your program must read lines of input data until there is no more data to be processed. You may want to use the same logical design as in the payroll program from Program 1. You'll have to use manipulators to manage the formatting of your output. (Use of printf is strictly forbidden, you must use C++ streams for I/O in the programs for this course.) Read the discussion on slides 4.17 4.21 carefully, there are important clues there. The program provided for Programming Project 1 also shows how this is done. You also have to do some mathematical calculations that go beyond mere arithmetic. The C++ language includes most of the standard mathematical functions, including a square root function and a hyperbolic tangent function. To use them, you need to add another #include at the beginning of your program: #include <cmath> Testing: At minimum, you should be certain that your program produces the output given above when you use the given input file. However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing regimen. You should make up and try additional input files as well; of course, you'll have to determine by hand what the correct output would be. What to turn in and how: You will submit this assignment to the Curator System (read the Student Guide), and it will be graded automatically. Instructions for submitting, and a description of how the grading is done, are contained in the Student Guide. You will be allowed up to five submissions for this assignment. Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every sample input file posted on the course website. If you do not get a perfect score, analyze the problem carefully and test your fix with the input file returned as part of the Curator e-mail message, before submitting again. Evaluation: Your submitted program will be assigned a score based upon the runtime testing performed by the Curator System. We will not be evaluating your submission of this program for documentation style. However, you should examine the given program as a guide to acceptable documentation, and include similar comments for the statements you add to it. For a number of the later projects, we will also evaluate your submission for documentation, and for other requirements. It is best to begin preparing for that now. Due Date: Check Web Site Page 5 of 9

Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement at the end of your program: /////////////////////////////////////////////////////////////////////////// // Virginia Tech Honor Code Pledge // // 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. // // <Student Name> // /////////////////////////////////////////////////////////////////////////// Failure to include this pledge in a submission is a violation of the Honor Code. Due Date: Check Web Site Page 6 of 9

// SkyDiver.cpp: Calculating Velocity of a Falling Body // // Programmer: Dwight Barnette // Email: barnette@vt.edu // Compiler: Visual Studio C++ 2010 // Platform: Intel Core i7-2600k / Windows 7.7601 // Date: September 3, 2011 // // Purpose: // To compute the velocity of a free-falling body, given // the number of seconds since its release, its weight, // and its coefficient of drag. //C++ Libraries #include <fstream> #include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; int main() { // Constant declarations: const double FeetPerMile = 5280.0; // feet in a mile const double SecPerHour = 3600.0; // seconds in an hour const double g = 32.2; // gravitational acceler. // Variables declarations: int NumLines = 0; // number of lines of data processed double Weight, // weight of object in pounds Mass, // mass of object Drag, // coeff of drag for object Time, // seconds object has been falling fpsv, // object velocity in feet per second mphv; // and miles per hour ifstream ifile; ofstream ofile; // input file stream // output file stream Due Date: Check Web Site Page 7 of 9

// The following statements prepare the input and output files for use: ifile.open("fall.dat"); // open input file ofile.open("fall.out"); // open output file ofile << fixed << showpoint; // Print the header info to output file: ofile << "Programmer: " << "<replace with your name>" << endl; ofile << "Falling Bodies" << endl; ofile << endl; ofile << " mass drag time fps mph" << endl << "---------------------------------------------" << endl; // Read first line of data from input file: ifile.ignore(80, '\n'); ifile >> Weight >> Drag >> Time; // skip header line // Process lines of data until none remain: while (ifile) { ifile.ignore(80, '\n'); // skip to start of next line // (not really necessary) NumLines = NumLines + 1; // Count input lines (unnecessary) // --------------------------------- Processing Section //computation goes here //computation goes here //computation goes here // calculate mass of object // calculate velocity in feet per second, using specified formula // convert velocity to miles per hour // --------------------------------- Output Section // print mass, drag, time and velocity as per the project spec: ofile << setw( 6) << setprecision(3) << Mass << setw(10) << setprecision(3) << Drag << setw(10) << setprecision(1) << Time << setw(10) << setprecision(2) << fpsv << setw(10) << setprecision(2) << mphv << endl; Due Date: Check Web Site Page 8 of 9

// Read the next line of input: ifile >> Weight >> Drag >> Time; } // End of while loop. ofile << "---------------------------------------------" << endl; // Close the files: ifile.close(); ofile.close(); } return NumLines; // return number of input lines (unnecessary) /////////////////////////////////////////////////////////////////////////// // Virginia Tech Honor Code Pledge // // 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. // // <Student Name> // /////////////////////////////////////////////////////////////////////////// Due Date: Check Web Site Page 9 of 9