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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Advertisements

Python Programming: An Introduction to Computer Science
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Chapter 2: Algorithm Discovery and Design
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to Programming with Java, for Beginners Control Structures.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 2: Algorithm Discovery and Design
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Financial Information Management Managing Financial Information Critical Thinking Business Process Modeling WINIT Control Structures Homework.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS1101: Programming Methodology Aaron Tan.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
General Programming Introduction to Computing Science and Programming I.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Lecture 4 Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Chapter 7 Problem Solving with Loops
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CISC105 – General Computer Science Class 4 – 06/14/2006.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Learning Javascript From Mr Saem
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
EGR 2261 Unit 10 Two-dimensional Arrays
Siti Nurbaya Ismail Senior Lecturer
Programming Funamental slides
Java Programming Arrays
Algorithm Discovery and Design
For loops Taken from notes by Dr. Neil Moore
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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, … Total individual variables, declare each separately (ouch!) There is a better way to work with lots of very similar data in most programming languages: ARRAYS !!!

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”

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!

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

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[ ] of real;

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

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

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

2/13/2003CSCI 150: Introduction to Computer Science14 Code for Savings Table Program See savings table program on pp (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??

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

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

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

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

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

2/13/2003CSCI 150: Introduction to Computer Science20 Mapping countable sets /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, …

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 …

2/13/2003CSCI 150: Introduction to Computer Science22 Diagonalization Method f1 f2 f3 f4 f5 f6 f7 f8... n 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!

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

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

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