Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming 1 Problem Solving and Software Engineering.

Similar presentations


Presentation on theme: "Computer Programming 1 Problem Solving and Software Engineering."— Presentation transcript:

1 Computer Programming 1 Problem Solving and Software Engineering

2 Computer Programming 2 Objectives Review Structure of a program Basic phases of software life cycle – Object-centered design Practice sessions

3 Computer Programming 3 Review Computer Software Hardware Drivers Applications OS CPU ALU Registers RAM Second memory Bus User programs

4 Computer Programming 4 Review A program – A sequence of instructions that specifies for the computer how to solve a problem. – Remember that computers are dumb! – Computers only understands machine language: A sequence of ones and zeros Cause the computer to perform a particular action, such as add, subtract, multiply,... Write machine languages is very hard to do, to understand, and correct. Thus the invention of high-level languages such as C++.

5 Computer Programming 5 Compiler Versus Interpreter Program in high-level language Compiler Machine language  A Compiler translates a file written in a high-level language into a machine language file that can be executed on the computer.  An interpreter executes each high-level statement directly. On fly, the interpreter translates the statement to the corresponding machine language instructions and executes/runs them.

6 Computer Programming 6 Contrast Assembler and HLL Compiler Assembler translates one mnemonic into one ML statement Compiler translates one HL statement into several ML statements 1010110011110101 0000000000010000 0010111010110101 0000000000010010 0010111011111101 0000000000010100 z = x + y; Compiler

7 Computer Programming 7 First program: Hello World! #include int main() { std::cout << “Hello World!”<<std::endl; return 0; } What is the difference between this program and the one given in the previous lecture?

8 Computer Programming 8 Structure of program Preprocessor directives – #include – #if – … Namespaces to use New namespaces – variables – Functions – Classes The main function

9 Computer Programming 9 Structure of a program #include #include “xxx.h” … using namespace xxx; …. namespace ns1 { xxx function1(xxxx) { … } … public class } … int main() { ….. return 0; }

10 Computer Programming 10 Variables, Functions, and Classes Variables – In C++ all variables must be declared before they are used. Furthermore, variables must be used in a manner consistent with their associated type. – E.g. double radius; Functions – All C++ functions must be declared before being used, their return type defaults to int. However, it is considered good style to fully declare all functions. Use void keyword to specify that a function does not return a value. – E.g. int power(double x, int y) { … return …;} Classes – Combines data objects and functions to provide an Abstract Data Type (ADT). – E.g. class Sphere { …}

11 Computer Programming 11 From problems to programs Given a problem, how to write a program that solves it? A simplified version of the main steps: Designer Problem Computer program Results programmer design

12 Computer Programming 12 Problem Solving through Software Engineering Phases (V model) Requirements & Specifications Design Implementation Testing Maintenance

13 Computer Programming 13 Problem Solving through Software Engineering Phases Requirements & specifications – Formally specify and document the problem we are solving Design – Analysis, specify algorithms to solve problem Implementation – Write solution in syntax of language Testing – Execution and Debugging – Get rid of “bugs” Maintenance – Update, modify to meet changing needs

14 Computer Programming 14 Problem World’s largest ball of twine found in Cawker City, Ks.Cawker City, Ks. – How much does the ball weigh? – What is the length of the twine when unrolled?

15 Computer Programming 15 Object-Centered Design Steps State how you want the program to behave Identify and categorize the objects involved in the problem Identify operations needed to solve the problem Develop algorithms by arranging objects and operations in an order which solves the problem Implement your algorithm using C++

16 Computer Programming 16 Behavior To find the weight of a ball of string: Enter radius of sphere : 9 Enter the density of twine: 1 Now computing... Weight of ball of string = 999.99

17 Computer Programming 17 Objects Description Software Objects TypeKindName prompt for radius of sphere string constantnone screen ostream variable cout radius of sphere double variable radius Twine density double variable density keyboard istream variable cin weight of ball double variable weight

18 Computer Programming 18 Operations Output prompt for radius of sphere to cout Input real value from cin Store it in radius Output prompt for density of twine to cout Input real value from cin Store it in density Compute weight Output weight to cout

19 Computer Programming 19 More Objects Computation of weight requires additional objects Description Software Objects TypeKindName density of sphere double variable density 4.0 double constant π double constant PI 3 integer constant 3.0 double constant

20 Computer Programming 20 Algorithm (Recipe!) 1. Initialize constant PI 2. Output prompt for radius to cout 3. Input real value from cin, store in radius 4. Output prompt for density to cout 5. Input real value from cin, store in density 6. Compute 7. Output weight to cout

21 Computer Programming 21 Coding First, create a program stub that contains opening documentation – Compiler directives that add items in libraries needed for some of the objects and operations – An empty main function Convert each step of the algorithm into code – If it uses a software object that hasn’t already been declared, add a declaration statement that specifies the object’s type and name.

22 Computer Programming 22 Coding /* sphereWeight.cpp computes the weight of a sphere. * * Input: The radius (feet) and * the density (pounds/cubic foot) of a sphere * Output: The weight of the sphere (pounds) ************************************************/ #include // cin, cout, > #include // pow() using namespace std; int main() { }

23 Computer Programming 23 Coding int main() { const double PI = 3.14159; cout << "Enter the sphere's radius (feet): "; double radius; cin >> radius; cout << "Enter its density (pounds/cubic feet): "; double density; cin >> density; double weight = density * 4.0 * PI * pow(radius, 3) / 3.0; cout << "\nThe weight of the sphere is approximately " << weight << " pounds.\n"; }

24 Computer Programming 24 Testing Enter radius of sphere (feet) : 6.5 Enter its density (pounds/cubic feet) : 14.6 The weight of the sphere is approximately 16795 pounds

25 Computer Programming 25 Testing, Execution, Debugging Common error sources Syntax errors – Violations of grammar rules of the high level language Semantics/run-time errors – Errors that occur during execution Logic errors – Errors in the design of the algorithm

26 Computer Programming 26 Syntax Errors Example: double radius – Missing semi-colon Compiler finds most of this kind of errors Different compilers give varying degrees of helpful diagnostics Do many but learn from them!

27 Computer Programming 27 Run-time Errors Not detected until program runs Examples – Division by zero – Taking square root of negative numbers Program must be modified to prevent such run-time errors from happening

28 Computer Programming 28 Logic Errors Program compiles, runs without crashing, but gives incorrect results These are the hardest errors to find Find by using sample data and hand calculating the correct results, comparing Note: Testing grows increasingly more difficult with larger programs – Some will run for years without logic error appearing

29 Computer Programming 29 Maintenance Student programs run only a few times Real-world programs are used for many years – Due to significant investment of resources – They may need to be upgraded or/and changed New features may be required during life of program usage Upgrading is called “maintenance”

30 Computer Programming 30 Problem Session A maintenance question?

31 Computer Programming 31 Problem Upgrade the program we previously wrote to solve the following problem: What is the length of the twine when unrolled? Follow the OCD steps: State how you want the program to behave Identify and categorize the objects involved in your problem Identify operations needed to solve the problem Develop algorithms by arranging objects and operations in an order which solves the problem

32 Computer Programming 32 Describe Behavior of Program In addition to the behavior already described, Program should display prompt for the weight of 1 foot of the twine User enters values from keyboard Program computes, displays on screen the length (in feet) of the twine when unrolled

33 Computer Programming 33 Behavior Envisioned … Enter the weight of 1 foot of twine: xxx... The length of the twine when unrolled is: xxxx

34 Computer Programming 34 Objects Use description to fill in chart for objects. Description Software Objects TypeKindName prompt for mass density per foot string constantnone screen ostream variable cout Density per foot double variable lengthDensity keyboard istream variable cin Length of the twine when unrolled double variable length

35 Computer Programming 35 Operations Description Name Predefined?LibraryOperator Display string using cout Yes iostream<< Read the density per foot using cin Yes iostream>> Compute the length using weight of ball divided by the density per foot Yes / Output the result using cout YesIostream << Use description to fill in chart for operations

36 Computer Programming 36 Algorithm and Coding Work together to determine the steps necessary to have the objects manipulated by the operations Write the program – Compile and link to fix syntax errors – Test the program to fix run-time and logical errors.


Download ppt "Computer Programming 1 Problem Solving and Software Engineering."

Similar presentations


Ads by Google