Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.

Similar presentations


Presentation on theme: "CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2."— Presentation transcript:

1 CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2

2 Case Study 1 Converting Units of Measurements Problem Statement You work in a store that sells imported fabric, measured in square meters; The store customer wants to know the equivalent amount in square yards. You need to write a program that performs the conversion.

3 Case Study 1 Converting Units of Measurements Analysis: –Input: Fabric size in square meters (SqMeters). –Output: Equivalent amount in square yards (SqYards). –Relevant Formulas: The relationship between sq. meter & sq. yards (1 m 2 = 1.196 y 2 )

4 Case Study 1 Converting Units of Measurements Design: –Algorithm: 1. Read the fabric size in m 2. 2. Convert size to yd 2 using the formula (1 m 2 = 1.196 y 2 ). 3. Display fabric size in yd 2. –Refinement of step2: 2.1 Fabric size in yd 2 = 1.196 * fabric size in m 2

5 Case Study 1 Converting Units of Measurements Implementation –Program header Give a logical name for your program –Program Name:ConvertSqMtoSqYd; The name of the source code file of the program can be different from the logical name –File name:CONVMYSQ.PAS –TurboPascal limits file names to 8 characters –Declaration section Memory cells used in your program Constant declaration: use constants for unchanged values Variable declaration: use variables for values that change –Program body Data processing takes place here

6 Case Study 1 Converting Units of Measurements Implementation (Using Turbo Pascal) program ConvertSqMtoSqYd; {Convert square meters to square yards } constMetersToYards = 1.96;{conversion constant} varSqMeters,{input - fabric size in meters} SqYards: real;{output - fabric size in yards} begin {Read the fabric size in square meters.} WriteLn ('Enter the fabric size in square meters >'); ReadLn(SqMeters); {Convert the fabric size to square yards.} SqYards := MetersToYards * SqMeters; {Display the fabric size in square yards. } WriteLn ('The fabric size in square yards is ', SqYards) end.

7 Case Study 1 Converting Units of Measurements Testing (Output Window): –A program run Enter the fabric size in square meters > 2.00 The fabric size in square yards is 2.392000E+00

8 Case Study 2: Square & Cube Problem Statement –Write a program that calculates the square root, square, and cube of a number Is this statement clear & sufficient for analysis & design steps? –NO

9 Case Study 2: Square & Cube Modified Problem Statement Write a program that gets an integer number from the user, calculates the square root, square and cube of the number and then prints the number,the square root, the square, and the cube. If the number is negative, the square root should not be evaluated. An error message should be written instead. The output should be labeled with suitable headings.

10 Case Study 2: Square & Cube Analysis –Input Get a number from the user –Processing & formulas Calculate the square root, the square, & the cube –SquareRoot = sqrt(Number) –Square = Number x Number. –Cube = Number x Square. –Output Write labels for Number, Square root, Square, and Cube Write values of Number, Square root, Square, and Cube (This is one way of doing it…)

11 Case Study 2: Square & Cube Design (Algorithm) 1.Pseudocode for initialization Clear screen Write program purpose 2.Pseudocode for Input Read Number 3.Pseudocode for Processing SquareRoot = Sqrt(Number) Square = Number * Number Cube = Square * Number 4. Pseudocode for Output Write “Number”, “Sq. Root”,“Square”, “Cube” Write Number, SquareRoot, Square, Cube 5. Pseudocode for termination Write Goodbye message

12 Case Study 2: Square & Cube Implementation –Program header program SquareNCube; –Declaration section const HEAD = ‘Number Sq.Root Square Cube’; { Heading } var Number, { input- number being processed } Square, { output - square of the number } Cube : integer; { output- cube of the number } SquareRoot : real; {output- square root of the number} –Program body

13 Case Study 2: Square & Cube Implementation: First draft –Program Body Enclosed between begin & end. Write algorithm as comments begin{Program body} {1. Initialize} {2. Read Number } {3. Calculations / Evaluate formulas} {4. Output heading & values} {5. Terminate} end.

14 Case Study 2: Square & Cube Implementation: Second draft Add Pascal statements in corresponding places begin{Program body} {1. Initialize} Clrscr; WriteLn(‘This program calculates square rt., ‘); WriteLn(‘square & cube of a given number’); {2. Read Number } write (‘Enter a number ’); readln (Number); {3. Evaluate formulas} if (Number >= 0) then SquareRoot := sqrt(Number) else WriteLn(‘Square root can't accept negative values’); Square:= Number * Number; {evaluate square} Cube:= Square * Number; {evaluate cube} {4. Output heading & values} { writeln ( HEAD ); } writeln ( ‘Number Sq.Root Square Cube’); writeln ( Number, SquareRoot, Square, Cube ) {5. Terminate} WriteLn(‘Good bye’); ReadLn; end.

15 Case Study 2: Square & Cube Implementation: (Final Pascal Code) program SquareNCube; const{ Declaration part } HEAD = ‘N umber Sq.Root Square Cube’; { Heading } var Number, { input- number being processed } Square, { output - square of the number } Cube : integer; { output- cube of the number } SquareRoot : Real; {output- square root of the number} begin { Program body } { Initialize} Clrscr; WriteLn(‘This program calculates square rt., ‘); WriteLn(‘square & cube of a given number’); { Read Number } write (‘Enter a number’); readln (Number); { Evaluate formulas } if (Number >= 0) then SquareRoot := sqrt(Number) else WriteLn(‘Square root can't accept –ve values’); Square := Number * Number; { evaluate square } Cube := Square * Number; { evaluate cube } { Output heading & values } writeln ( HEAD ); writeln ( Number, SquareRoot:6:2,Square, Cube ); { Terminate} WriteLn(‘Good bye’); ReadLn; end. { end of program body }

16 Ideas from Students A program for keeping track of my grades in a course –Easy A program like those in graphic calculators that allows entering the data and will automatically display mean / average, the frequency, the median, the mode … –Easy, we’ll do a simple version of it A special dictionary that will enable me to write customized terms and the computer will automatically recognize the word I meant and transform it by itself. Ex: dvlp would stand for development. That would be very time saving while writing reports. –Not hard, but in this course we don’t learn everything we need A program for planning courses: what courses do I take next and what courses do I have left. –A planning and scheduling problem, can be arbitrarily hard A program for managing my time and schedule –If you know an algorithm for doing that, let me know….

17 A Limitation In this course, given the syllabus, we are limited to solving problems where you know beforehand how many inputs you will have or where you do not have to keep track of the values of individual inputs (unless you use external files to do so). Why? –Because we will look at only a limited set of ways of representing data in memory

18 Case Study 3: Keeping Track of Grades in a Course A program for keeping track of my grades in a course –Is this specified clearly enough?

19 Case Study 3: Revised Problem Statement A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will retrieve and display the scores it already knows and give me the option to enter the new data. When I exit the program, all the data (old and new) will be stored to a file so that it can be retrieved next time the program is run.

20 Case Study 3: Begin Analysis A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will retrieve and display the scores it already knows and give me the option to enter the new data. When I exit the program, all the data (old and new) will be stored to a file so that it can be retrieved next time the program is run.

21 Case Study 3: I/O Analysis Input: Existing data from file New data from keyboard Output: Existing data plus new data to file

22 Case Study 3: Processing Analysis Processing: –Display existing data Decide: about how to display the data. E.g. Table with headers: Assessment My Score Max Score HW#1 8 10 …. … …. –Prompt for new data: Data: Name of assessment, My score, Max score Decide: –prompt for data one by one or all 3 at a time? –prompt for data for a single assessment or multiple assessments? If multiple assessments, how do you indicate you are done? –Store old and new data: Decide: –Overwrite the old file? –Make a new version of the file? Decide: –Display all the data the program knows about before exiting?

23 Case Study 3: Revised Problem Statement A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will: –Retrieve and display the scores it already knows in a table with suitable headers –Give me the option to enter data for one or more additional assessments or to exit –If I choose to enter data for an assessment it will prompt me for all three values on one line –After I enter the values it will again give me the option to enter new data or to exit. When I exit the program –all the data (old and new) will be stored to a new file so that it can be retrieved next time the program is run. –all the data it has for me will be shown. Are you starting to think that you should use a spreadsheet instead?


Download ppt "CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2."

Similar presentations


Ads by Google