Lecture Notes 83005 Program Design Intro to Algorithms

  • Slides: 22
Download presentation
Lecture Notes 8/30/05 Program Design & Intro to Algorithms

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

Important Links v www. cs. unr. edu/~alina v Web. CT – www. webct. unr.

Important Links v www. cs. unr. edu/~alina v Web. CT – www. webct. unr. edu

Homework for Thursday v Login to Web. CT. Picture. Introduction. v Download a copy

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

Main Objectives of This Course v Develop problem solving skills v Learn to program

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

Algorithms * Informally - a general method for solving a problem; a recipe. *

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 v Outline the solution v Develop the outline into

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

Algorithms v Algorithm is required to be: * Well defined -- it is clearly

Algorithms v 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 1. Define the problem Input Processing Output

Steps in Program Development 2. Outline the solution This is a rough draft of

Steps in Program Development 2. Outline the solution This is a rough draft of the solution. The outline may include: v v v 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

“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

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 Get 3

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

Steps in Program Development 4. Test the algorithm for correctness v Very important step!!

Steps in Program Development 4. Test the algorithm for correctness v Very important step!! v 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

“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 v

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

“Find an average of 3 numbers” C++ (code segment): …. . cout<<“Enter three numbers”:

“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

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

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. v v External documentation (algorithm, test data, etc…) Internal documentation (commenting your code)

“Find the average of 3 numbers for 4 sets of data. ” v Define

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

Structured Programming Top down development: Look at the problem as a whole v Outline

Structured Programming Top down development: Look at the problem as a whole v Outline general solution v Break down into main components v Functional decomposition v 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

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

Pseudocode v Pseudocode standard which we will follow in this class: - Statements are

Pseudocode v 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.