Download presentation
Presentation is loading. Please wait.
Published byAlison Sims Modified over 9 years ago
1
2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies Rich.Bonneau@iona.com rbonneau: Still under construction – use at your own risk !!! rbonneau: Still under construction – use at your own risk !!!
2
2/4/2003CSCI 150: Introduction to Computer Science2 Today’s Outline Today’s ‘notices’ Review of Last Session Topics Tonight’s Lecture Topics –Start of Chapter 3 - “Numerical Computation and a Study of Functions” »Let Us Calculate Some Numbers »Simple Calculations »Functions »Looping and Study of Functions –New concepts to be developed »Another Pascal data type: real - for big and small numbers »How to read in and write out and compute with real variables »Multiple input of data with readln »Formatting output data – field widths Next Class Topics
3
2/4/2003CSCI 150: Introduction to Computer Science3 Today’s Notices Office Swords 339 extension 2282 Office hours Tu, Th 4-6:15 Don’t forget homework #2 due today! Graded Homework #1 – a few left over from last week … pick them up? Does anyone still need the Free Pascal compiler for your own computer?? –Check out the lecture notes page on the course web site or Let me know - by e-mail! ????
4
2/4/2003CSCI 150: Introduction to Computer Science4 Summary of Last Session Pluralization program –4 cases - two special, two general –needed variable task to differentiate between them –direct plural or generated - either replacement or add- on Mini Text Editor Example –Concatenation Operator (+) for strings –String expressions to insert/delete/replace: e.g. text := copy(text ….) + new + copy(text….); –Looping using the while do language construct –Text functions: length, copy, pos ‘Conversation Program’ Pascal Syntax Summary This is the end of Chapter 2 !!!
5
2/4/2003CSCI 150: Introduction to Computer Science5 Integers, write(ln) and spacing ! Default behavior of writeln – left justify and use as only as many characters as needed Can specify field width for displaying integers: writeln(MyIntegerVar:10); This will right justify the value of the variable MyIntegerVar in a field 10 characters wide. Need to include space for possible minus sign. Statement write(MyIntegerVar) will display the value and then let the next writeln or write continue on from that point! You can compose better prompts! Write(‘Hi !’); Writeln(‘ How are you?’); will display: Hi! How are you? 10 34768 -18906 65536098 10 34768 -18906 65536098 Good for tables of values AKA Output Formatting Also works for strings!!
6
2/4/2003CSCI 150: Introduction to Computer Science6 Outline of Chapter 3 Let Us Calculate Some Numbers Simple Calculations Functions Looping and Study of Functions Searching for The Best Value Storing Information in Arrays Findings Sums, Minima, Maxima Patterns in Programming Putting Things in a Row and a Special Characteristic of Functions Tonight’s Lecture Topics
7
2/4/2003CSCI 150: Introduction to Computer Science7 Let’s Calculate With Numbers Last chapter -used computers to do text manipulation using strings (and integers) Now we want to use the computer to … COMPUTE!!! With Numbers!! Two sample numerical problems to solve: A) Find how to save money for future years –Develop an algorithm –Write program to compute savings and refine it B) Compute largest cylinder’s volume from its surface area: - a packaging problem - and an optimization problem!
8
2/4/2003CSCI 150: Introduction to Computer Science8 Simple Calculations First there were Strings – characters, e.g. ‘Pascal’ Then there were Integers - whole numbers (positive, negative) - 0, 345, -4534 Now we introduce a new data type Now : real - represents fractions, big numbers, small numbers - uses scientific notation with an exponent and significant digits 234.56 = 2.3456 X 10 2 significant digits exponent In Free Pascal, Real Numbers can be –as small as 10 -324 –and as big as 10 +308 –with up to 12-13 significant digits ! Example real Constants: 1.2345 3.14159 1.e20 -4.56e-10 0.000002345
9
2/4/2003CSCI 150: Introduction to Computer Science9 Pascal and the real data type To declare real variables in Pascal use: var savings, rate, fraction : real; Can assign constants to reals as in savings := 12.0; rate := 1.0e-1; fraction := -0.0000456; Can now also compute with real expressions –similar to integer expressions –using the operators of +, -, * and now / (for divide) – –also parentheses –looks like algebra x := 1.3 + 2.34 * y; avg := (x1 + x2 + x3)/3.0; or more complicated z := (1+k/n)*(3.14159-sigma)/(.0004 * h); Another Data Type!! Another Data Type!!
10
2/4/2003CSCI 150: Introduction to Computer Science10 Example - volume of cylinder Algorithm: find radiusr find heighth calculate volumeV = r 2 h print V Now let’s look at the Pascal code for this: Program CylinderVolume (two versions … CylVol1 and CylVol2) – slight difference … Note - readln may now have two or more input arguments!!! As long as they are numeric! r h r h
11
2/4/2003CSCI 150: Introduction to Computer Science11 Another real variable example: Savings Account Growth How to compute values of savings after a period of time? Need to use compound interest: newsavings = old savings + (old savings * interest_rate) And repeat this over and over during a period of time … In Pascal - use the same variable to accumulate results savings := savings + (savings*interest_rate); See program FindSavings!
12
2/4/2003CSCI 150: Introduction to Computer Science12 New output twist for Reals !! As we saw in the last program, a new formatting notation to output reals : writeln( savings : 6 : 2 ); Format Specification!! –The first number (6) is the field size - the least number of characters to display for the whole result –The second number (2) is the decimal count - how many places to display after decimal point. In setting field size, always remember to include chars for the sign (+ or -) and the decimal point! A very common format specification - 10:2 - 10 total chars, 2 decimal places +/- X X. y y 2 6 = field size
13
2/4/2003CSCI 150: Introduction to Computer Science13 Two Hazards in Using Reals First Hazard: Need to understand order of operations - aka precedence: Consider a * b + c - what gets done first: + or *?? Rule 1: Operations * / have higher precedence than + - Thus a*b done first and then add c to result Rule 2: If operators have the same precedence, execute from left to right –e.g. a * b * c / d same as (((a*b) * c) / d) When in doubt - use parentheses!! Expressions in parentheses done before others!
14
2/4/2003CSCI 150: Introduction to Computer Science14 More on Numerical Precision Second Hazard with Reals numbers not exact in the computer !!! E.g. 1/3 =.33333333333333…. (infinite) But in the computer, only finite number of decimal places, e.g. 11-12… leads to ‘round off errors’ in computation Also problems in dealing with large numbers and small numbers in the same computation! See Program ErrorDemo to see evidence of when this can be a problem – simply adding and subtracting the same number from a given number –Use inputs of 100 & then 1e12, 1e13, 1e14 !!
15
2/4/2003CSCI 150: Introduction to Computer Science15 Functions A function takes an input, processes it and returns a result or output y = F( …. ) e.g. y = sin ( x ) Functions can take any kind of inputs (strings, integers, reals …) and produce any kind of outputs, but we here will concentrate on numerical functions. Functions which have numerical inputs and numerical outputs are very useful throughout computer applications - business, accounting, engineering, science,... Remember your math classes??
16
2/4/2003CSCI 150: Introduction to Computer Science16 Some ways to represent a function an English language description a formula - as in mathematical notation a table of function values a visual graph of function values a computer program!! - can produce many of the other representations! Area = r 2 the area of a circle is computed by multiplying the irrational number pi by the radius of the circle multiplied by itself
17
2/4/2003CSCI 150: Introduction to Computer Science17 Looping and Study of Functions To show values of a function we could use the Pascal while loop to –set the function’s input value –compute the function value based on the input –print it out –loop back for another input value This can give us a lot of data that represents the function Look at the Double Program – very common model –uses a while loop and real (loop) variables to compute a table of values for the ‘double’ function –how to modify to go from 0.0 to 1.0 in steps of.1 ???
18
2/4/2003CSCI 150: Introduction to Computer Science18 Solving Cylinder Volume Problem Recall again (from high school math!): Volume of cylinder V= r 2 h Area of cylinder A= 2 r 2 + 2 r h Let’s assume that we want to find the largest volume for a fixed surface area, e.g. tin –say at Fixed Area = 1000 = A Using algebra (!!) we can solve for V to get: V = 500 r - r 3 So now we have a formula that gives us the Volume as a function of the radius r Consider: CylVol3 Computes a table of function values of V as a function of radius r Another program does simple graph Cylgra r h ??
19
2/4/2003CSCI 150: Introduction to Computer Science19 Solving the Savings Problem General Problem: –How much would you need to put away monthly to save a million dollars over 40 years!! First Algorithm: Compute the final total given a monthly amount How? –Ask for the monthly amount to be saved –Assume a monthly interest rate of 1% = 12% yearly! –Loop over 40 years (480 months) doing »Compute new monthly balance from old monthly balance and the interest rate –Print final balance after 480 iterations Let’s look at Saving40 for the full program
20
2/4/2003CSCI 150: Introduction to Computer Science20 Summary of major topics Reals in Pascal programs –allow to compute with fractions, decimals and very large and small numbers Functions and how to work with them in Pascal Practical application of reals in two sample problems: –cylinder volume optimization –saving for retirement!
21
2/4/2003CSCI 150: Introduction to Computer Science21 Next Session Thursday - Feb. 6 Homework assignment 4 to be passed out Continuation of Chapter 3 –Finding the best or optimal value of a function!! Let the computer do the job? –Study the process of evolving a more complex algorithm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.