Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 1 Overview of Programming and Problem Solving Dale/Weems/Headington.

Similar presentations


Presentation on theme: "1 Chapter 1 Overview of Programming and Problem Solving Dale/Weems/Headington."— Presentation transcript:

1 1 Chapter 1 Overview of Programming and Problem Solving Dale/Weems/Headington

2 2 l Types - programs, software n translation process - abstract to detail, primitive, machine language - satisfy a need n system software - operating system n application programs n utility programs n data vs. information l Application programs n programmer, analyst, systems analyst n read, process, produce information

3 3 n information system n packaged/customized software –purchase, outsource or in-house –horizontal market –vertical market –VAR - value added reseller –interactive vs. batch n machine language n symbolic programs n Compiled/interpreted and executed

4 4 What is Computer Programming? Program computers - efficiently, effectively, quickly, accurately It is the process of planning a sequence of logical steps (called instructions) for a computer to follow. STEP 1 STEP 2 STEP 3...

5 5 l OOP – object-oriented programming n Classes – analysis of data and functions l How do we write the program? n Computer cannot analyze – only human n analyze, instructions to solve l Algorithm – step by step procedure n Computer – fast and flexible tool for implementing algorithms l Steps to computing the solution, not solution itself

6 6 Programming Life Cycle Phases 1 Problem-Solving Analysis and specification – understand problem General solution (algorithm) Verify – really solve the problem 2 Implementation Concrete solution (program) Test - thoroughly 3 Maintenance Use the program Modify the program - changing requirements

7 7 Algorithm to Determine an Employee’s Weekly Wages 1. Get the employee’s hourly payRate 2. Get the hours worked this week 3. Calculate this week’s regular wages 4. Calculate this week’s overtime wages (if any) 5. Add the regular wages to overtime wages (if any) to determine total wages for the week

8 8 l Programming language – English like - rules l Translating algorithm into programming language – coding, debugging l Translating into machine language - compiler l Planning – crucial – shortcuts take more time in the long run l Life cycle of program – problem solving, implementation (coding and testing), maintenance l Documentation - notes l data vs. information vs. knowledge l Binary representation of data

9 9 Memory Organization l two circuit states correspond to 0 and 1 l bit (short for binary digit) refers to a single 0 or 1. Bit patterns represent both the computer instructions and computer data l 1 byte = 8 bits l 1 KB = 1024 bytes l 1 MB = 1024 x 1024 = 1,048,576 bytes

10 10 How Many Possible Digits? l binary (base 2) numbers use 2 digits: JUST 0 and 1 l decimal (base 10) numbers use 10 digits: 0 THROUGH 9

11 11 What is a Programming Language? It is a language with strict grammar rules, symbols, and special words used to construct a computer program. Machine/assembly/symbolic language Structural/object-oriented/natural Assembler/interpreter/compiler Source program/object program

12 12 A Tempting Shortcut? GOAL THINKING CODE REVISE DEBUG TEST CODE Shortcut?

13 13 High Level Languages l are portable l user writes program in language similar to natural language examples -- FORTRAN, COBOL, Pascal, Ada, Modula-2, C++, Java l most are standardized by ISO/ANSI to provide an official description of the language

14 14 Three C++ Program Stages other code from libraries, etc. other code from libraries, etc. written in machine language written in machine language written in machine language written in machine language written in C++ written in C++ via compilervia linker SOURCEOBJECT EXECUTABLE myprog.cppmyprog.objmyprog.exe

15 15 Basic Control Structures l a sequence is a series of statements that execute one after another l selection (branch) is used to execute different statements depending on certain conditions l Looping (repetition) is used to repeat statements while certain conditions are met. l a subprogram is used to break the program into smaller units

16 16 SEQUENCE Statement...

17 17 SELECTION (branch) IF Condition THEN Statement1 ELSE Statement2 Statement1 Statement Statement2 Condition... True False

18 18 LOOP (repetition) Statement Condition... False True WHILE Condition DO Statement1

19 19 SUBPROGRAM (function) SUBPROGRAM1... SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM

20 20 l Running a C++ Program n Text editor – enter the program n Compiler – translates n Linker – combine necessary files

21 21 Computer Components Arithmetic Logic Unit Control Unit Auxiliary Storage Device Memory Unit ( RAM & Registers ) Central Processing Unit ( CPU ) Input Device Output Device Peripherals

22 22 Memory Unit l is an ordered sequence of storage cells, each capable of holding a piece of information l each cell has its own unique address the information held can be input data, computed values, or your program instructions.

23 23 Central Processing Unit l has 2 components to execute program instructions n Arithmetic/Logic Unit performs arithmetic operations, and makes logical comparisons. n Control Unit controls the order in which your program instructions are executed.

24 24 Peripherals l are input, output, or auxiliary storage devices attached to a computer n Input Devices include keyboard and mouse. n Output Devices include printers, video display, LCD screens. n Auxiliary Storage Devices include disk drives, scanners, CD-ROM and DVD-ROM drives, modems, sound cards, speakers, and digital cameras.

25 25 l Hardware/software l User Interface l Interactive/batch system l Operating system l Advantages n Portability n Any assembly level

26 26 Computing Profession Ethics l copy software only with permission from the copyright holder l give credit to another programmer by name whenever using his/her code l use computer resources only with permission l guard the privacy of confidential data l use software engineering principles to develop software free from errors

27 27 Problem Solving Techniques l ASK QUESTIONS -- about the data, the process, the output, error conditions. l LOOK FOR FAMILIAR THINGS -- certain situations arise again and again. l SOLVE BY ANALOGY -- it may give you a place to start. l USE MEANS-ENDS ANALYSIS -- Determine the I/O and then work out the details.

28 28 More Problem Solving Techniques l DIVIDE AND CONQUER -- break up large problems into manageable units. l BUILDING-BLOCK APPPROACH -- can you solve small pieces of the problem? l MERGE SOLUTIONS -- instead of joining them end to end to avoid duplicate steps. l OVERCOME MENTAL BLOCK -- by rewriting the problem in your own words.

29 29 Company Payroll Case Study A small company needs an interactive program to figure its weekly payroll. The payroll clerk will input data for each employee, and each employee’s wages and data should be saved in a secondary file. Display the total wages for the week on the screen.

30 30 One Employee’s Wages In one week employee ID # 4587 works 52 hours at the hourly pay rate of $24.75. Assume a 40.0 hour normal work week and an overtime pay rate factor of 1.5. What are the employee’s wages? 40 x $ 24.75 = $ 990.00 12 x 1.5 x $ 24.75 = $ 445.50 ___________ $ 1435.50

31 31 Problem-Solving Phase What information will be used? INPUT DATA from outside the program FORMULA CONSTANTS used in program COMPUTED VALUE produced by program OUTPUT RESULTS written to file or screen by program

32 32 Problem-Solving Phase INPUT DATAFORMULA CONSTANTS OUTPUT RESULTS Employee ID Number Hourly payRate Hours worked Normal work hours ( 40.0 ) Overtime pay rate factor (1.5) Hourly payRate Hours worked Wages COMPUTED VALUE Wages

33 33 If hours are more than 40.0, then wages = (40.0 * payRate) + (hours - 40.0) * 1.5 *payRate otherwise, wages = hours * payRate Week’s Wages, in General RECALL EXAMPLE ( 40 x $ 24.75 ) + ( 12 x 1.5 x $ 24.75 ) = $1435.50

34 34 Algorithm for Company Payroll Program l initialize total company payroll to 0.0 l repeat this process for each employee: 1. Get the employee’s ID empNum 2. Get the employee’s hourly payRate 3. Get the hours worked this week 4. Calculate this week’s wages 5. Add wages to total company payroll 6. Write empNum, payRate, hours, wages to file l write total company payroll on screen

35 35 // *************************************************** // Payroll program // This program computes each employee’s wages and // the total company payroll // *************************************************** #include // for keyboard/screen I/O #include // for file I/O using namespace std; void CalcPay ( float, float, float& ) ; const float MAX_HOURS = 40.0; // Maximum normal hours const float OVERTIME = 1.5; // Overtime pay factor C++ Program

36 36 C++ Code Continued int main( ) { float payRate; // Employee’s pay rate float hours;// Hours worked float wages; // Wages earned float total;// Total company payroll int empNum;// Employee ID number ofstream payFile;// Company payroll file payFile.open( “payfile.dat” );// Open file total = 0.0;// Initialize total

37 37 cout << “Enter employee number: “; // Prompt cin >> empNum; // Read ID number while ( empNum != 0 ) // While not done { cout << “Enter pay rate: “; cin >> payRate ; // Read pay rate cout << “Enter hours worked: “; cin >> hours ; // and hours worked CalcPay(payRate, hours, wages); // Compute wages total = total + wages; // Add to total payFile << empNum << payRate << hours << wages << endl; cout << “Enter employee number: “; cin >> empNum; // Read ID number }

38 38 cout << “Total payroll is “ << total << endl; return 0 ;// Successful completion } // *************************************************** void CalcPay ( /* in */ float payRate, /* in */ float hours, /* out */ float& wages ) // CalcPay computes wages from the employee’s pay rate // and the hours worked, taking overtime into account { if ( hours > MAX_HOURS ) wages = (MAX_HOURS * payRate ) + (hours - MAX_HOURS) * payRate * OVER_TIME; else wages = hours * payRate; }

39 39 Assignment #1 l Read chapters 1 and 2 l Exam preparation exercises - pg. 62-65 - 1, 3, 5-10, 13-15.


Download ppt "1 Chapter 1 Overview of Programming and Problem Solving Dale/Weems/Headington."

Similar presentations


Ads by Google