computer programming 1 problem solving and software engineering

36
Computer Programming 1 Problem Solving and Software Engineering

Post on 15-Jan-2016

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming1

Problem Solving and Software Engineering

Page 2: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming2

Objectives

Review Structure of a program Basic phases of software life cycle

– Object-centered design

Practice sessions

Page 3: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming3

Review

Computer

Software

Hardware

Drivers

Applications OS

CPU

ALU

Registers RAMSecond memory

Bus

User programs

Page 4: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming4

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++.

Page 5: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming5

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.

Page 6: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming6

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

Page 7: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming7

First program: Hello World!

#include <iostream>

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?

Page 8: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming8

Structure of program

Preprocessor directives– #include – #if– …

Namespaces to use New namespaces

– variables– Functions– Classes

The main function

Page 9: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming9

Structure of a program

#include <…>#include “xxx.h”…

using namespace xxx;….

namespace ns1 { xxx function1(xxxx) { … } … public class }…int main() {

…..return 0;

}

Page 10: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming10

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 { …}

Page 11: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming11

From problems to programs

Given a problem, how to write a program that solves it?

A simplified version of the main steps:

DesignerProblem Computer

program Resultsprogrammer

design

Page 12: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming12

Problem Solving through Software Engineering Phases (V model)

Requirements &Specifications

Design

Implementation

Testing

Maintenance

Page 13: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming13

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

Page 14: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming14

Problem

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

the twine when unrolled?

Page 15: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming15

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++

Page 16: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming16

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

Page 17: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming17

Objects

DescriptionSoftware Objects

Type Kind Name

prompt for radius of sphere

string constant none

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

Page 18: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming18

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

Page 19: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming19

More Objects

Computation of weight requires additional objects

DescriptionSoftware Objects

Type Kind Name

density of sphere double variable density

4.0 double constant

π double constant PI

3 integer constant

3.0 double constant

Page 20: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming20

Algorithm (Recipe!)

1. Initialize constant PI2. Output prompt for radius to cout3. Input real value from cin, store in radius4. Output prompt for density to cout5. Input real value from cin, store in density6. Compute

7. Output weight to cout

34.0

3.0

density radiusweight

Page 21: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming21

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.

Page 22: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming22

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 <iostream> // cin, cout, <<, >>#include <cmath> // pow()using namespace std;int main(){}

Page 23: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming23

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";

}

Page 24: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming24

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

Page 25: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming25

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

Page 26: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming26

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!

Page 27: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming27

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

Page 28: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming28

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

Page 29: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming29

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”

Page 30: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming30

Problem SessionA maintenance question?

Page 31: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming31

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

Page 32: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming32

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

Page 33: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming33

Behavior Envisioned

Enter the weight of 1 foot of twine: xxx

...

The length of the twine when unrolled is: xxxx

Page 34: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming34

Objects

Use description to fill in chart for objects.

DescriptionSoftware Objects

Type Kind Nameprompt for mass density per foot

string constant none

screen ostream variable cout

Density per foot double variable lengthDensity

keyboard istream variable cin

Length of the twine when unrolled

double variable length

Page 35: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming35

Operations

Description Name

Predefined? Library Operator

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

Yes Iostream <<

Use description to fill in chart for operations

Page 36: Computer Programming 1 Problem Solving and Software Engineering

Computer Programming36

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.