Download presentation
Presentation is loading. Please wait.
Published byIlene Hudson Modified over 8 years ago
1
2/13/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 9 Dr. Richard J. Bonneau IONA Technologies Rich.Bonneau@iona.com
2
2/13/2003CSCI 150: Introduction to Computer Science2 Today’s Outline Today’s ‘notices’ Tonight’s Lecture Topics –End of Chapter 3 - “Numerical Computation and a Study of Functions” »... »Searching for The Best Value »Storing Information in Arrays »Findings Sums, Minima, Maxima »... »Putting Things in a Row and a Special Characteristic of Functions –New concepts to be developed »Multiple pieces of data can be stored in arrays Next Class Topics - A Quiz!! Tonight’s Topics Tonight’s Topics
3
2/13/2003CSCI 150: Introduction to Computer Science3 Today’s Notices Office Swords 339 extension 2282 Office hours Tu-Th 4-6 Homework Assignment #2 – last chance for partial credit – if handed in at this session Homework Assignment #3 – still being graded Homework Assignment #4 - due next Tuesday Homework Assignment #5 - being distributed today – due week from Tuesday Quiz on Tuesday - covering Chapters 1-3
4
2/13/2003CSCI 150: Introduction to Computer Science4 Summary of Last Sessions Session 7 Evolution of an algorithm To compute the largest value of a function Volume of a cylinder with a given surface area From simple computing loop to a program which maintained previous values and compared each time through the loop Common algorithm to find maximum (or minimum) of a function using the computer Session 8 Lab 2 – PFE and Programming Techniques Note this one
5
2/13/2003CSCI 150: Introduction to Computer Science5 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
6
2/13/2003CSCI 150: Introduction to Computer Science6 Storing Information in Arrays Pascal programs use Variables to store data Sometimes you need to store a lot of data – usually very similar data, just a lot of it Example - the savings program - needed 40 years of 12 months of data if you wanted to keep a complete table of your savings plan. Could use: Total1, Total2, Total3, … Total480 480 individual variables, declare each separately (ouch!) There is a better way to work with lots of very similar data in most programming languages: ARRAYS !!!
7
2/13/2003CSCI 150: Introduction to Computer Science7 Just what is an Array?? “A set of values using the same name AND a way to access/modify any one specific element in the set” Let’s see how to declare an array Two step process in Pascal: 1.Define a new data type just for the array 2.Declare a variable of that type – this is the actual array AKA “Table” “List”
8
2/13/2003CSCI 150: Introduction to Computer Science8 Pascal Syntax For Arrays – 2 Steps Step 1: declare a new type type realarray480 = array[1..480] of real; Step 2: declare a variable of this type var myTable: realarray480; keyword Always an array of some existing type! Limits of the array This declares an array of 480 real numbers, with the name myTable You get to name the type!
9
2/13/2003CSCI 150: Introduction to Computer Science9 How to Access elements in an array In memory an array looks like: myTable[1] myTable[2] myTable[3] …... myTable[480] Reference a specific element using the array name then [ then an integer constant (or variable or expr) then ] In other words, use the bracket notation to enclose the index of the desired element within the overall array. Elements are numbered/indexed according to the original type declaration – in this case from 1 to 480. index
10
2/13/2003CSCI 150: Introduction to Computer Science10 Additional Array Info First: in the pascal program, the type declarations must come before var declarations Generalized (syntax) declaration: two parts type = array [.. ] of ; var : ; Can declare many arrays of the same type Can declare many different array types (e.g. arrays of integers, arrays of strings, arrays of reals, different sized arrays, different start/end index values, etc.) E.g. type SavingsType = array[2003.. 2043] of real;
11
2/13/2003CSCI 150: Introduction to Computer Science11 Examples of Arrays in Programs See program FirstArray on p. 104 – –to declare an array and –start to put values into the elements using individual index values Can be quite tedious especially if you wanted to fill a big array - so consider using a while loop to fill an array See program FirstArray on page. 105 (file FirstArray2.pas) Once data in an array, how to print it out?? See program SecondArray on p. 106 ‘Fancier version’ on Page 107
12
2/13/2003CSCI 150: Introduction to Computer Science12 Arrays and the Savings Program Consider using an array to hold the value of the savings account balance for each of the 480 months of the original problem Look at FillSavingsTable program on Page. 108 This program is OK - but - there is NO OUTPUT! Could make it better by having the user be able to check on the values at different months! How? - Let’s look at the algorithm first
13
2/13/2003CSCI 150: Introduction to Computer Science13 Modified Savings Table Program Algorithm –First fill in the table with all the data for all months - using a loop?? –Then loop asking the user for which month he/she is interested in (with 0 or less to stop!) »Within the loop, let the user’s desired month be the index into the table to get the specific data requested here month 6 asked for A ‘counting’ loop … I.e. 1 to 480 A ‘command control’ loop because the user supplies data until he/she wishes to stop “A tale of 2 loops!” 1 100 2 204 3 313 4 421 5 530 6 635 7 742..
14
2/13/2003CSCI 150: Introduction to Computer Science14 Code for Savings Table Program See savings table program on pp. 109-110 (FillSavingsTable2.pas) Two natural divisions in the program - each using a loop and a table Generate the data to put into the table Ask the user for which month(s) information of the table is requested So we now know how to create and fill tables/arrays of information and manage them on behalf of a user How else can we use arrays??
15
2/13/2003CSCI 150: Introduction to Computer Science15 Findings Sums, Minima, Maxima What if ??? A banking scenario … We wanted a simple bank account program which allows us to have up to 4 deposits a month? And what if we wanted to know the –total of all the deposits and –the maximum sized deposit and –the minimum sized deposit? How to do that with our arrays concept? Let’s first look at the basic program: –declare array type and then array and then loop to input data –program Deposit - p. 111
16
2/13/2003CSCI 150: Introduction to Computer Science16 Side Story: Using Loops to Calculate Need someplace to put the sum value as we are computing it. Need an extra variable! Sometimes called an accumulator variable. Used to accumulate a running result over a set of computations Let’s look at some programs that calculate by using loops and accumulator variables. –program SumN - sums a sequence of integers – accum variable = sum –program Factorial - multiplies a sequence of integers – accum variable = product –program Asequence - creates a string from a single characters – accum variable = asequence
17
2/13/2003CSCI 150: Introduction to Computer Science17 Finding the Maximum (or Min) !! Getting back to our bank account scenario Finding the maximum value from an array of values - general algorithm : 1) init a variable with the biggest found so far 2) loop through all the elements in the array 3) for each array element compare it to see if we now have a bigger value then update the ‘biggest found so far’ variable 4) after all elements tested, print out the result Let’s go see some code to do this!!
18
2/13/2003CSCI 150: Introduction to Computer Science18 Finding the maximum deposit Code fragment to implement the steps of the algorithm largestSoFar := deposit[1]; {right?} i := 2;{can start with 2 now} while i <= 4 do begin if deposit[i] > largestSoFar then begin largestSoFar := deposit[i]; end i := i + 1; end; writeln( largestSoFar :8:2); Consider Deposit2 program … Pattern: Can also be used to find minimum value or sum of values or concatenation of strings! INIT COMPARE UPDATE OUTPUT LOOP 2-4
19
2/13/2003CSCI 150: Introduction to Computer Science19 Putting Things in a Row and a Special Characteristic of Functions Countability!! or “Putting things in a row!” Very important aspect of computer science – deals with whether or not computer programs are able to solve difficult problems or not - is there non-computability?? Countability involves trying to allocate elements of a given set to a potentially infinite set of ‘bins’ - aka ‘mapping’ If we can assign them all successfully, the set is said to be countable - if not, then uncountable Examples: Integers, even integers, pos/neg, even fractions! See next slide...
20
2/13/2003CSCI 150: Introduction to Computer Science20 Mapping countable sets... 1 2 3 4 5 6 7 8... 2 4 6 8 10 12 14 16... 0 1 -1 2 -2 3 -3 4... 0 1 2 1/2 3 1/3 2/3 4 1/4... Even integers Pos/neg integers Fractions! Also the set of all strings – first all strings of length one, then length two, …
21
2/13/2003CSCI 150: Introduction to Computer Science21 Where does this lead?? Who cares? … Computer scientists !!! –Related to how far/how many things computer programs can do! Consider Noah’s Ark question: –How many animals were on the Ark??? –How many male animals, female animals? »The same amount of male and female animals!! So we can know ‘how many’ without knowing exactly ‘how many’!!! But is there some infinite set which is NOT countable? Yes - the set of functions which just maps integers to other integers! Why not countable?? Example of “diagonalization” method! –First assume that the set of functions IS countable …
22
2/13/2003CSCI 150: Introduction to Computer Science22 Diagonalization Method... 1 2 3 4 5 6 7 8... 2 4 6 8 10 12 14 16... 0 1 -1 2 -2 3 -3 4... f1 f2 f3 f4 f5 f6 f7 f8... n 1 2 3 4... 0 1 -1 2 -2 3 -3 4... Create a new/different function by taking a different value along the diagonal!! E.g. at 1 use 5, at 2 use 6, etc. This function is NOT in current list!
23
2/13/2003CSCI 150: Introduction to Computer Science23 Why is this even interesting to us?? Eventually we will show there are only a countable number of programs But from what we have seen there are an uncountable number of functions What does that tell us about the power of programming? Not quite as powerful as we might believe!! There are “non-computable” functions !!
24
2/13/2003CSCI 150: Introduction to Computer Science24 Summary of major topics Arrays - what are they and how to declare them –New data type in pascal –Actually create your own data type ! Index values - how to identify a specific element in an array Use of loops with arrays - lots of processing in a little space Computing totals, maxima, minima of information in arrays Countable and Uncountable sets and the “power” of programming
25
2/13/2003CSCI 150: Introduction to Computer Science25 Next Session Quiz - Covering Chapters 1-3 of the book –Multiple choice? –True/False? –Definitions? –Analyze a program or two? –Write an algorithm? –Write a quick program ? –Open book, open notes... –Bring a calculator – you may need to do a little computing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.