 # Lecture Notes 8/30/05 Program Design & Intro to Algorithms.

## Presentation on theme: "Lecture Notes 8/30/05 Program Design & Intro to Algorithms."— Presentation transcript:

Lecture Notes 8/30/05 Program Design & Intro to Algorithms

Important Links  www.cs.unr.edu/~alinawww.cs.unr.edu/~alina  WebCT – www.webct.unr.eduWebCT

Homework for Thursday  Login to WebCT. Picture. Introduction.  Download a copy of the syllabus and read it.  Write an algorithm for a problem from class.  Read Chapter 1 in “C++ Programming”.  Read Chapters 1 – 3 in “Simple Program Design”

Main Objectives of This Course  Develop problem solving skills  Learn to program using C++

Algorithms * Informally - a general method for solving a problem; a recipe. * Our book - a step-by-step problem-solving process in which a solution is arrived at in a finite amount of time. *According to Webster.. al.go.rithm - a rule or procedure for solving a mathematical problem that frequently involves repetition of an operation.

Program Development  Define the problem  Outline the solution  Develop the outline into an algorithm  Test the algorithm for correctness  Code the algorithm into a specific programming language  Run the program on the computer – test and debug  Document and maintain the program

Algorithms  Algorithm is required to be: * Well defined -- it is clearly and unambiguously specified. * Effective -- its steps are executable. * Finite -- it terminates after a bounded number of steps. * Must produce a correct solution 100% of the time.

Steps in Program Development 1. Define the problem Input Processing Output

Steps in Program Development 2. Outline the solution This is a rough draft of the solution. The outline may include:  Major processing steps involved.  Major subtasks  Major Control Structures  Major variables and record structures  The mainline logic.

“Find an average of 3 numbers” Outline - obtain three numbers from the user - calculate the average - display the result

Steps in Program Development 3. Develop an outline into an algorithm The algorithm represents the precise steps to be taken and the order in which they are to be carried out.

“Find an average of 3 numbers” Algorithm: Prompt user for 3 numbers Prompt user for 3 numbers Get 3 numbers Get x, y and z Add numbers together OR Sum = x+y+z Divide the sum by 3 Average = sum/3 Display the result Display Average

Steps in Program Development 4. Test the algorithm for correctness  Very important step!!  Check for logical errors, create test data and test your algorithm.

“Find an average of 3 numbers” Test Algorithm (Desk check or Trace): Get 3 numbers Add numbers together Divide the sum by 2 Display the result

Steps in Program Development 5. Code the algorithm into a specific programming language  C,  C++,  Java,  Fortran,  Cobol,  Basic,  etc…

“Find an average of 3 numbers” C++ (code segment): ….. cout<<“Enter three numbers”: cin >> x >> y >> z; sum = x+y+z; avg = sum/3.0; cout<< “The average is “<< avg; …..

Steps in Program Development 6. Run the program on a computer – test and debug The most fun and the most frustrating part of the program development. Two main types of errors:  Syntax errors (the program will not run)  Logic errors (the program will run but will not produce correct results)

Steps in Program Development 7. Document and maintain the program This is NOT the last step. You should document throughout the process of developing your program.  External documentation (algorithm, test data, etc…)  Internal documentation (commenting your code)

“Find the average of 3 numbers for 4 sets of data.”  Define the problem  Outline the solution  Develop the outline into an algorithm  Test the algorithm for correctness

Structured Programming Top down development:  Look at the problem as a whole  Outline general solution  Break down into main components  Functional decomposition This systematic, disciplined approach to program design results in a higher precision of programming.

Structured Programming Structure Theorem Any computer program can be written using the following three control structures: * Sequence * Selection (If-Then-Else) * Repetition (DoWhile)

Pseudocode  Pseudocode standard which we will follow in this class: - Statements are written in simple English; - Each instruction is written on a separate line; - Each set of instructions is written from top to bottom, with only one entry and one exit; - Groups of statements may be formed into modules, and that group given a name.