Case Studies C++
Case Studies C++
Case Studies C++
()1.1 Problem Description Convert a measurement given in feet to the equivalent value of (a) yards, (b) inches, (c) centimeters, and (d) meters.
()
Goal
Convert a measurement --- given in feet --- to the equivalent value --- yards --- inches --centimeters --- meters.
Givens
The number of feet to convert. The conversion factors.
Unknowns
The equivalent number of Inches The equivalent number of yards The equivalent numbest of meters The equivalent number of centimeters
Conditions
None
()2.1 Solution Strategy Get a value in feet from the user and then convert that number into equivalent value of yards, inches, centimeters, meters and then display the conversions. ()2.2 Goal Decomposition
Sub-goal 1
Get measurement in feet from the user.
Sub-goal 2
Convert to equivalent units.
Sub-goal 3
Display results. ()2.3 Resources
Relevant formulas
Yards = feet / 3 Inches = feet * 12 Centimeters = feet * 12 * 2.54 Meters = (feet * 12 * 2.54) / 100
Formula Derivation
A foot measures 3 yards. Hence a yard is the measurement in feet divided by 3. 12 Inches make up a foot. Thus to get the number of inches, given the measurement in feet, simply multiply the feet measure by 12. One inch is 2.54 cms. Hence to convert from feet to centimeters, convert it to inches as before and multiply by 2.54. A hundred cms make a meter. Thus to convert from feet to meters we divide the centimeter measure obtained above by 100. ()2.4 Data Organization and Description Input (givens): Name Description Feet Number of feet to convert Output (unknowns): Name Description inches Equivalent number of inches yards Equivalent number of yards cent Equivalent number of centimeters meters Equivalent number of meters Origin User Used in Sub-goal # 1
The first level decomposition shows the broad outline of the whole program. The three main steps in solving this problem are (a) getting the inputs, (b) doing the actual conversions and (c) displaying the results.
Goal Refinement
Sub-goal 1 Get measurement in feet from the user. Sub-goal 2 Convert to equivalent units. ()()Sub-goal 2.1 ()()Convert into yards ()()Sub-goal 2.2 ()()Convert into Inches ()()Sub-goal 2.3 ()()Convert into feet ()()Sub-goal 2.4 ()()Convert into Centimeters Sub-goal 3 Display results.
The second level decomposition shows the finer details of the conversion process and the
display process. The conversion process consists of four different conversions and similarly display process is composed of five different processes. ()3.2 Module and Data Specifications Name: Instruct - Display instructions to the user. Input: None. Output: None. Logic: Display user instructions on the screen. Name: Input: Output: Logic: Name: Input: Output: Logic: Name: Input: Output: Logic: Name: Input: Output: Logic: Name: Input: Output: Logic: Data: ConvertToYards - Convert from feet to yards. None. None. Convert from feet to yards (1 yard = 1 foot/3) and store resultant value in yards. ConvertToInches - Convert from feet to inches. None. None. Convert feet to inches (1 inch = 1 foot * 12) and store in inches. ConvertToCent - Convert from feet to centimeters. None. None. Convert feet to centimeters (1 cm = 1 foot * 12 * 2.54) and store in cent. ConvertToMeters - Convert a given number of feet to meters. None. None. Convert feet to meters (meters = (1 foot * 12 * 2.54) / 100) and store in meters. Main - Convert a given number of feet to other units. None. None. Converts feet to other units using the given formulae. Name Type Structure Feet Real number Simple Inches Real number Simple Yards Real number Simple cent Real number Simple meters Real number Simple
()3.3 Algorithm
Logic
1.0 2.0 3.0 Display user instructions. Get value of feet. Perform conversions. 3.1 Convert feet to inches - inches = feet * 12 3.2 Convert feet to yards - yards = feet / 3 3.3 Convert feet to centimeters - cent = (feet * 12 * 2.54) 3.4 Convert feet to meters - meters = (feet * 12 * 2.54) / 100
4.0
Display results. 4.1 Display original number of feet user entered. 4.2 Display equivalent number of inches 4.3 Display equivalent number of yards. 4.4 Display equivalent number of centimeters 4.5 Display equivalent number of meters
Algorithm Description
The algorithm to convert units is straightforward enough. To put it in a nutshell, we have to get the value in feet, convert it to the required units and print out the result. Additionally, we must inform the user about the program and give general instructions. This is done as the first step. The next step is to get the value in feet from the user through a keyboard entry. The conversions are performed one at a time using the previously discussed formulae and stored in appropriate variables. The final step entails printing the results on the screen. The initial value in feet, followed by each converted unit is printed on the screen.
4. Translation
()4.1 Source Code //=================================================== // Name : // SID : // Course : // Section : // Instructor : // T.A : //=================================================== //=================================================== // Assignment # : // Date : //=================================================== //=================================================== // Description : This program will convert the // given feet into its equivalent yards, inches, // centimeters and meters. //=================================================== #include <iostream.h> // function prototypes // function to display the instructions void Instruct(); // function to convert feet to yards float ConvertToYards( float ); // function to convert feet to inches // for cin and cout
float ConvertToInches( float ); //function to convert feet to centimeters float ConvertToCent( float ); //function to convert feet to meters float ConvertToMeters( float ); //================================================= // Instruct - Provide instructions to the user. // Input - none. // Output - none. //================================================= void Instruct() { cout<< endl << "This program converts a number in"; cout<< "feet to the equivalent"; cout<< endl << "number of inches, yards, meters, "; cout<< "and centimeters."; return; // return flow of control }//End Instruct //================================================= // ConvertToYards - convert a number given in feet // to yards // Input - the number representing feet to convert // Output - return the equivalent number of yards //================================================= float ConvertToYards( float feet ) { // convert feet to yards and return the result return feet/3; }// End ConvertToYards //================================================= // ConvertToInches - convert a number given in feet // to inches // Input - the number representing feet to convert // Output - return the equivalent number of inches //================================================= float ConvertToInches( float feet ) { // convert feet to inches and return the result
//================================================= // ConvertToCent - convert a number given in feet to // centimeters // Input - the number representing feet to convert // Output - return the equivalent number of // centimeters //================================================= float ConvertToCent( float feet ) { // convert feet to centimeters and return the result return feet*12*2.54; }// End ConvertToCent
//================================================= // ConvertToMeters - convert a number given in feet // to meters // Input - the number representing feet to convert // Output - return the equivalent number of meters //================================================= float ConvertToMeters( float feet ) { // convert feet to meters and return the result return ( feet*12*2.54 )/100; }// End ConvertToMeters
// Main function int main() { // declare local variables float feet, // INPUT: the number of feet to convert yards, // CALCULATED: feet to yards inches, // CALCULATED: feet to inches cent, // CALCULATED: feet to centimeters meters; // CALCULATED: feet to meters
// Instruct the user Instruct(); // prompt user to enter the number of feet cout << endl<< "Please enter the number of feet to"; cout<< "convert:"; // read in the number entered from the keyboard cin >> feet; // perform conversions by calling functions yards = ConvertToYards( feet ); inches = ConvertToInches( feet ); cent = ConvertToCent( feet ); meters = ConvertToMeters( feet ); // display results to screen cout << endl << "The number of feet entered ("; cout << feet<< ") equals:"; cout << endl << yards << " yards"; cout << endl << inches << " inches"; cout << endl << cent << " centimeters"; cout << endl << meters << " meters"; // end program and return 0 (successful exit) to OS return 0; }// End main ()4.2 Program and Module Description Instruct This function prints out the user instructions and returns. ConvertToYards The formula discussed before is used to convert the value of feet into yards. One foot is 3 yards. Hence, we divide the measure of feet by 3 to get the equivalent measure in yards. The result is stored in the variable yards. ConvertToInches The measure in feet is converted into inches using the formula inches = feet*12 and the result stored in inches. ConvertToCent
The measure in feet is converted into centimeters using the formula cent = feet*12*2 and the result stored in cents. ConvertToMeters The measure in feet is converted into meters using the formula (feet*12*2.54)/100 and the result stored in meters. Main The main function first calls the instruct routine to display the objectives of the program. The next objective is to get the input data from the user. The C++ statement cin is used to do this task. The variable feet is populated with the value of feet. Then each function is called to convert the measurement from feet to inches, centimeters, yards and meters, appropriately. The last step is to display the results. The value of each variable is displayed on the screen. The main function ends.
5. Solution Testing
Test the program with following data domain: The domain range for the feet is any real number Test the program with following data: Input: Enter Number of feet: 1 1 ft = 12 in, 1 yd = 3 ft, 1 in = 2.54 cm, 1 m = 100 cm. Output: Yards= 0.33 Inches= 12 Centimeters= 30.48 Meters= 0.30
6. Testing: Output
This program converts a number in feet to the equivalent number of inches, yards, meters, and centimeters. Please enter the number of feet to convert: The number of feet entered (12) equals: 4 yards 144 inches 365.76001 centimeters 3.6576 meters
Textbooks
(Text 1: W. Savitch, "Problem Solving with C++", Second Edition, Addison- Wesley. (Text 2: J.G. Bookshear, "Computer Science: An Overview", Fifth Edition, Addison- Wesley. Lecture Readings
Topic 1 The Machine: Hardware and Software Text 1: Sec. 1.1 Text 2: Ch. 0, Sec. 1.1 - 1.4, 2.1 - 2.4, 3.1 - 3.3 Topic 2 Introduction to Problem Solving and Programming Text 1: Sec. 1.2 - 1.4 and Sec. 2.1 - 2.3 Text 2: Sec. 4.1 - 4.3, 5.1, 5.2 Topic 3 Modular Design and Abstraction Text 1: Sec. 3.1 - 3.5 Text 2: Sec. 5.3, 5.4 Topic 4 Control Structures: Sequential, Selective, and Repetitive Text 1: Sec. 2.4 and Sec. 7.1, 7.2 (nested if) Text 2: Sec. 4.4, 4.5 Topic 5 More on Modular Design: Module Communications Text 1: Ch. 4 Topic 6 More on Control Structures Text 1: Sec. 7.2 (switch statement)- 7.4
MIDTERM
(to be announced) Topic 7 Introduction to Software Engineering Text 2: Ch. 6 Topic 8 Data Abstraction: User Defined Types
and the Class Text 1: Ch. 6, Sec. 8.1 Topic 9 More on Input and Output: Streams, Files, and Formatting Text 1: Ch. 5 Text 2: Sec. 8.1 - 8.3 Topic 10 Data Structures: Lists Text 1: Ch. 9 (except section 3 on searching and sorting) and Ch. 10 Text 2: Sec. 7.1 Topic 11 List Applications: Searching and Sorting Text 1: Ch. 9 (section 3 on searching and sorting) Topic 12 Structures and pointers Text 1: Sec. 6.1 and Sec. 11.1 Topic 13 Dynamic Structures: Linked Lists Text 1: Sec. 11.2 and Sec. 14.1 Text 2: Sec. 7.2
FINAL
(to be announced)
1. The title and number of the assignment 2. The student name, course/section and ID # 3. The name of the TA 2. Problem Formulation
Describe the problem being solved in a few sentences 2. Identify the goals, givens, and unknowns 1. 3. Other information as necessary 3. Planning
Describe the strategy used to solve the problem 2. Define data requirements: Input (describe the data that will be entered and processed by the program).Output (describe the expected output) Intermediate data Refine goals into subgoals and identify tasks 3. to be performed. 4. Provide explanations of all formulae used 1. 4. Design
1.
6. Testing
1. Provide comprehensive test run showing both input and output Naming of objects and functions should be meaningful. Declarations of objects that are logically related should be grouped together. All definition and
declaration statements should be fully commented. Modules are to be documented in a similar way like the main program. Describe exactly what each function does, the input to the function, the logic, and the output produced. Define the type and role of each parameter in a separate comment block at the top of each module. All input data must be checked for validity, when appropriate. Sixty percent of the program grade is given for the problem solving phases and fourty percent is for the implementation and testing. Submit all documentation. Points will be deducted for non-conforming programs. Assignments should be submitted during recitations only. Late programs will be penalized and must be submitted to the TAs or instructor in-person. The penalty is as follows: the total grade that the assignment is marked out of will be reduced by 10% for each day that it is late. For example, after one day the assignment will be marked out of 90%.