280 likes | 433 Views
Lecture1: Introduction to Programming and Problem Solving. Topics. 1. Computer Operations 2. What is programming? 3. What are the steps of program development? 4. What is structured programming 5. Data and data structures. Six Basic Computer Operations.
E N D
Topics • 1. Computer Operations • 2. What is programming? • 3. What are the steps of program development? • 4. What is structured programming • 5. Data and data structures
Six Basic Computer Operations 1. A computer can receive (input) data 2. A computer can store data in memory 3. A computer can perform arithmetic and manipulate text strings 4. A computer can compare the contents of two memory locations and select one of two alternatives 5. A computer can repeat a group of operations 6. A computer can output information (processed data)
What is Programming • Program – a very specific set of instructions (or command lines) that making a computer do what you want it to do • Programming – the process of creating a program • the development of a solution to an identified program, and setting up of a related series of instructions which, when directed through computer hardware, will produce the desired results
Steps in program development 1. Define the problem 2. Outline the solution 3. Develop the outline into an algorithm 4. Test the algorithm for correctness 5. Code the algorithm into a specific programming language 6. Run the program on the computer 7. Document and maintain the program
Define the Problem • Divide the problem into three components (called IPO): • Inputs – what do you have? • Outputs – what do you want to have? • Processing • how do you go from inputs to outputs? • A defining diagram is recommended
Outline the Solution • The major processing steps involved • The major subtasks (if any) • The major control structures (e.g. repetition loops) • The major variables and record structures • The mainline logic
Develop the Outline into an Algorithm • Algorithm is a set of precise steps that describe exactly the tasks to be performed, and the order in which they are to be carried out • Pseudocode (a form of structured English) is used to represent the solution algorithm
Test the Algorithm for Correctness • The main purpose of desk checking the algorithm is to identify major logic errors early, so that they may be easily corrected • Test data needs to be walked through each step in the algorithm, to check that the instructions described in the algorithm will actually do what they are supposed to
Code the Algorithm into a Specific Programming Language • Only after all design considerations have been met should you actually start to code the program into your chosen programming language (e.g. Visual Basic, Java, C++)
Run the Program on the Computer • This step uses a program compiler and programmer-designed test data to machine test the code for syntax errors • Program complier translate high-level languages (e.g. VB) to low-level machine language before execution
Document and Maintain the Program • Not the last step in the program development process • An ongoing task from the initial definition of the problem to the final test result • Involves both external documentation (such as hierarchy charts) and internal documentation that may have been coded in the program
Example 1: Circle • Calculate the circumference and area • IPO • I: radius • P: calculate circumference and area as circumference = 2 * P I* radius area=PI * radius ^ 2 • O: radius, circumference, area
Circle (Pseudocode) Begin program get radius, PI circumference = 2 * P I* radius area=PI * radius * radius display circumference, area End program
Example 2: Payroll Problem • I: time cards, pay rate, deductions • P: calculate gross pay, calculate net pay hours worked = end time – start time gross pay = hours worked * pay rate net pay = gross pay - deductions • O: payroll records (gross pay, deductions net pay)
Payroll problem (cont.) • Subtasks • Deductions include required (taxes) and optional (staff benefits) • Gross pay may include vacation time, sick time, personal time. • Major control structures • Repeat payroll processing for all employees • Major variables • Employee {pretty much} static data • Name, address, department, … • Employee non-static historical data • Year to date earnings, … • Employee current data • Hours worked, vacation hours, sick hours, … • Global data • Tax tables [federal & state] , tax cutoffs
Payroll Problem Pseudocode (…assuming deductions are given) Begin program Read Start_time, End_time, Pay_rate, Deductions Hours_worked = End_time – Start_time Gross_pay = Hours_worked * Pay_rate Net_Pay = Gross_pay – Deductions Display Gross_pay, Deductions, Net_pay End program
Structured Programming • Top-Down • outline a general solution to the problem first • start coding at the beginning of the problem and work systematically through each step until reaching the end • Modular • keep things together that belong together • e.g. calculating sales tax or printing report headings • Three control structures • Sequence (one after another) • Selection (making decisions/choices) • Repetition (doing things over and over)
Sequence • The straightforward execution of one processing step after another • pseudocode statements statement a statement b statement c
Example of Sequence in VB Private Sub CmdTotlasales_Click() Numbercontracts = CInt(TxtTotalContracts.Text) Pricecontracts = CCur(TxtContractPrice.Text) Totalrevenue = Numbercontracts * Pricecontracts TxtTotalSales.Text = FormatCurrency(Totalrevenue, 2) End Sub
Selection • Pseudocode statements: IF condition p is true THEN statement(s) in true case ELSE statement(s) in false case ENDIF • If condition p is true then the statement or statements in the true case will be executed, and the statements in the false case will be skipped
Example of Selection in VB If strGrade = A then sngGradePoint = 4.0 Elseif strGrade = B then sngGradePoint = 3.0 Elseif strGrade = C then sngGradePoint = 2.0 Elseif strGrade = D then sngGradePoint = 1.0 Else sngGradePoint = 0.0 End If
Repetition • A set of instructions to be performed repeatedly, as long as a condition is true • Pseudocode statements: DOWHILE condition p is true statement block ENDDO
Example of Repetition • Add up all odds between 1 to 20 • 1 + 3 + 5 + 7 + 9 + … + 19 = ? • Human’s way • 1 + 3 =4 • 4 + 5 = 9 • 9 + 7 = 16 • … … … • 81 + 19 = 100
Example of Repetition (Cont) • Computer’s way Private Sub CmdCalcSum_Click() intSum = 0 intCounter = 1 Do while intCounter <= 20 intSum = intSum + intCounter intCounter = intCounter + 2 Loop End sub
Data • Variable • the name given to a collection of memory cells, designed to store a particular data item • the value stored in that variable may change or vary as the program executes • Constant • a data item with a name and a value that remain the same during the execution of the program (e.g. 8, 10) • Literal • a constant whose name is the written representation of its value (e.g. “8”, “10”)
Data type • Integers • whole numbers • Real • Numbers with a decimal point • Two choices in VB: single and double • Character • Boolean • True or False