Assignment #1: Due Thursday, June 16th, two problems from chapter two. Page 101, Programming Projects 1 and 3. Project #1, page 101 is about metric tons and breakfast cereal. Project #3, page 101 is about workers receiving a 7.6% pay increase. Follow the process outlined below for solving the two problems and turn in your rough draft notes and diagrams showing that you went through the problem solving process shown below and that we began discussing on the first day of class. You should write up the pseudocode version of your program first, before you write the C++ code. -------------------------------------------------------------------------- THE ABOVE PROGRAMS CAN BE WRITTEN OUT AND SOLVED WITHOUT GOING TO THE COMPUTER. ON MONDAY OR TUESDAY, WE WILL GO TO THE COMPUTER LAB TO PRACTICE BOTH pcGRASP C++ and chaos.cns.uni.edu UNIX C++. I will also have additional handouts about how to compile and how to get printouts and what needs to be turned in. IF we do NOT get into the lab by then, the program due date will be EXTENDED, though I may collect just the hand-written portion of the assignment. Sorry about the Wright Hall computer lab problems! --------------------------------------------------------------------------- --------------------------------------------------------------------------- The phases or stages of the problem solving process were introduced on Monday during the first lecture. This handout develops those chapter one ideas further. The problem that we will use to illustrate the steps of the problem solving process is that of writing a program so that the user can enter the lengths of two legs of a right triangle and the program will calculate and then display the length of that right triangle's hypotenuse. The stages or steps of problem solving and software development: 1. Problem analysis and specification The precise specification of the problem 2. Design of an algorithm to solve the problem 3. The step-wise coding of that algorithm in a programming language (C++) 4. The testing and debugging of the program, until all syntax errors and logic errors are removed and we are confident it works correctly and produces correct output for all input data 5. Performing any maintenance, modifications and alterations needed over the lifetime of the program The 5th stage of the problem solving process, software maintenance and adapting programs to new situations and needs, is estimated to take up 80 to 90 percent of all software costs and programmer's time. Developing brand new software systems from scratch is the exception. The implications of this: Programs must be written to be understandable, to be as easily read and quickly understood as possible - so that they can be adapted, updated, improved and maintained in the future. ***************************************************************************** Step or phase one: Precise specification of the problem and problem analysis Stated more simply, "Understand the problem" Chief focus is on WHAT the program must achieve Precise specification of the information to be used in solving the problem is achieved by studying the problem statement and by working with the user or client when the statement is not clear or complete. Information to be used in solving the problem: 3 classifications i. identify the values that must be supplied from outside the program; i.e. identify input values ii. identify values that are given in the problem; i.e. find the needed constant values such as 3.1415 for pi or 0.05 as sales tax rate iii. identify the values which must be produced as output values, as the result of the solving of the problem input values, output values and constants Key questions for phase one, the understand what the problem step. What is given? (See i and ii above - input values and constants are given values) What is the goal? (See iii above - output values are the desired goal, the result) Step or phase two: Develop the algorithm that solves the problem The focus is on HOW to solve it now that it is understood. How to take the given input data and constants and produce the desired output values. Algorithms involve specifying how in a step by step manner. This second stage is concerned with three questions. 2.1 Describe the data objects used in solving the problem Identify the attributes of the data objects listed in phase one. Attributes: Name by which we will refer to the object, ---- Kind (variable or constant), ---- Type of object (Real or Integer or Character ---- or String) The result of step 2.1 is that you have a list or a table consisting of the following four columns: DATA OBJECT DESCRIPTION KIND OF VALUE TYPE OF VALUE NAME ----------------------- ------------- ------------- ---- 2.2 Describe the operations that must be applied to the objects in solving the problem. Describe what will happen to the objects. Operations such as multiplying, input, displaying along with a message, adding, assigning to, etc. Note that in describing the operations we do not need to be concerned with what order they will occur, during phase 2.2 of the whole process. Just isolate and describe the different operations that will have to happen sometime during the process of solving the problem. 2.3 Construct the algorithm from the given objects in 2.1 and operations of 2.2 ---- arrange the operations in the proper step by step order so that the problem solution is specified in this informal pseudocode. Analyze your solution to see whether or not it can be improved on. See if you can think of a different approach. If you can come up with a 2nd approach, compare the two algorithms and choose the "better" one. Step or phase three: Translate the algorithm into C++ code, i.e. code it and then enter it in the computer. The variable and constant names are already decided from part 2.1 of the process. The basic step by step process is already decided from step 2.3 of the process, i.e. the logic is already developed BEFORE you ever start writing or recording VBA code. The operations have been ascertained in step 2.2 above. Later we will learn how to create entirely new data types and objects in C++ that closely model the objects and operations we identify as needed by and natural to a problem. We will also learn how to reuse existing objects so we can take advantage of work that has already been done. Friday's example program: Problem: Write a program that will let the user enter the lengths of two legs (perpendicular sides) of a right triangle and then have the program display the length of its hypotenuse. Problem specification and analysis: What are the values that must be provided to the computation? The length of each leg, say a and b as shown here. (Use a prompt then cin << to get the length |\ a, and then to get | \ hypotenuse the length b). a | \ | \ --------- b What are the values that must be produced by the computation? The length of the hypotenuse (Use a cout << to display the answer) Designing the algorithm: 2.1 The objects involved are: data object kind of value type name 1st leg of rt triangle variable Real leg1 2nd leg of rt triangle variable Real leg2 Hypotenuse of rt triangle variable Real hypotenuse 2.2 The operations that will be applied: Input two Real values from the keyboard, storing them in variables (leg1 and leg2 are the named chunks of memory). Square two Real values (leg1 and leg2) by using multiplication operation and the operator is the * operator. Add two Real values (the squares will be added, i.e. 2 2 leg1 + leg2 Take the square root of a Real value, i.e. take the square root of the sum of the two squares. (In VBA????) Store the Real value square root of the sums of the squares of the two sides in a Real variable (Assign result to "hypotenuse"). Output a Real value (hypotenuse). cout << will work for this. 2.3 The algorithm, written in pseudo-code and numbered step-by-step fashion. 1. Display a message on the screen telling the user what the program does. 2. Prompt the user for and then input the lengths of leg1 and leg2. 3. Calculate ((leg1 times leg1) + (leg2 times leg2)) and take the square root of this sum and then store this result in (or assign this result to) hypotenuse. 4. Output a Real value (hypotenuse).