2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies.

Slides:



Advertisements
Similar presentations
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2: Algorithm Discovery and Design
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Elementary Algebra Exam 4 Material Exponential Expressions & Polynomials.
Basic Elements of C++ Chapter 2.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Computer Science 101 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Chapter 1 Number Sense See page 8 for the vocabulary and key concepts of this chapter.
Skills for October Rounds
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
IT253: Computer Organization
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Data Representation in Computer Systems
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CSC 221 Computer Organization and Assembly Language
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
2/13/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 9 Dr. Richard J. Bonneau IONA Technologies.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Significant Figure Rules RulesExamples The following are always significant Non zero digits Zeros between non zero digits Zero to the right of a non zero.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Building Java Programs
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Presentation transcript:

2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies rbonneau: Still under construction – use at your own risk !!! rbonneau: Still under construction – use at your own risk !!!

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

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 ! ????

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 !!!

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? Good for tables of values AKA Output Formatting Also works for strings!!

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

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!

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, Now we introduce a new data type Now : real - represents fractions, big numbers, small numbers - uses scientific notation with an exponent and significant digits = X 10 2 significant digits exponent In Free Pascal, Real Numbers can be –as small as –and as big as –with up to significant digits ! Example real Constants: e e

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 := ; 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 := * y; avg := (x1 + x2 + x3)/3.0; or more complicated z := (1+k/n)*( sigma)/(.0004 * h); Another Data Type!! Another Data Type!!

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

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!

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: total chars, 2 decimal places +/- X X. y y 2 6 = field size

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!

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 = …. (infinite) But in the computer, only finite number of decimal places, e.g … 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 !!

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??

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

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 ???

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  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 ??

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

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!

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