David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
David Stotts Computer Science Department UNC Chapel Hill.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Kernighan/Ritchie: Kelley/Pohl:
David Stotts Computer Science Department UNC Chapel Hill.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
Computer Science 1620 Loops.
Computer Science 103 Chapter 3 Introduction to JavaScript.
VBA Modules, Functions, Variables, and Constants
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Pascal By: Liane Tom. Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
David Stotts Computer Science Department UNC Chapel Hill.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Java Unit 9: Arrays Declaring and Processing Arrays.
A First Book of ANSI C Fourth Edition
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
I Power Higher Computing Software Development High Level Language Constructs.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Functions Declarations, Function Expressions and IIFEs
Arrays Declare the Array of 100 elements Notes
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Topics Sequences Introduction to Lists List Slicing
Arrays Week 2.
Chapter 2 - Introduction to C Programming
Topics Sequences Introduction to Lists List Slicing
Introduction to C Programming
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

booyah

0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

Declare array as a variable var grades = [ ] ; Use each part individually grades[3] = 89.3 ; grades[ i ] = grades[ i+1] ; Use the entire array as a unit return grades ;

We select a single item from an array collection with an integer subscript grades[4] grades[ k+3 ] Collections are more general abstractions We allow any string “name” (or “tag” ) for a single elements in a collection not just integer

var stats = { } ; // similar to array declaration var stats = { “min”: 27.3, “max”: 97.7, “avg”: 78.2 } stats.min = 21.4; stats.max = 99.3; alert(stats.avg); return stats;

function calcStats ( nums ) { var sum=0, nAvg, nMax=-1, nMin=10000; for (var i=0; i<nums.length; i++) { sum = sum + nums[i]; if (nums[i]<nMin) { nMin=nums[i]; } if (nums[i]>nMax) { nMax=nums[i]; } } nAvg = Math.floor(100*(sum/nums.length))/100; return { “min”:nMin, “max”:nMax, “avg”:nAvg }; }

function calcStats ( nums ) { var sum=0; var stats = { } ; // an empty collection stats.min=10000; stats.max=-1; // add fields for (var i=0; i<nums.length; i++) { sum = sum + nums[i]; if (nums[i]<stats.min) { stats.min=nums[i]; } if (nums[i]>stats.max) { stats.max=nums[i]; } } stats.avg = Math.floor(100*(sum/nums.length))/100; return stats; }

 Object is a collection of data and functions that operate on that data  Object is like an abstraction of a program  Before we go deeper into this, we need a deeper look at functions

 A value is something that can be assigned to a variable…  A value is something that an expression produces when it is evaluated  Simple: 5, “tarheels”, false, x = 2+3; y = “tarheels”; done = false;  Structured: [ 2, 5, 14, 19, 27 ] grades = [ 2, 5, 14, 19, 27 ] ;

Functions are values too So functions can be assigned to variables function cube (x) { return x*x*x; } function myMain() { var cf = cube; alert( cf(4) ); // prints 64, or 4^3 alert( cube(4)); // same thing }

Functions are values too So functions can be passed as arguments function myMain() { var cf = cube; runnit(cf); runnit(cube); // passing the “name” } function cube (x) { return x*x*x; } function runnit ( fun ) { alert ( fun(5) ); }

Functions are values too So functions can be explicitly initialized locally function myMain() { var newFn = function ( x ) { return x*x*x; } alert(newFn(6)); // execute the new function runnit(newFn); // pass the new function “name” } function runnit ( funarg ) { alert( funarg(5) ); }

Another way to do local (inner) functions function myMain() { function newFn ( x ) { return x*x*x; } // var newFn = function ( x ) { return x*x*x; } alert(newFn(6)); // execute the new function runnit(newFn); // pass the new function “name” } function runnit ( funarg ) { alert(funarg(5)); }

You can also return a local function as the result value produced by another function function myMain() { var sq = makeFun(); alert(sq(5)); } function makeFun ( ) { var fun = function (x) { return x*x; }; alert(“making a function: “ + fun(3)); return fun; }

Objects are data and functions wrapped up together var student ; // just a declaration var done=false; while (!done) { student = { }; // make a new object… “allocation” student.name = prompt(“name?”); student.grade = prompt(“grade?”); course[i] = student; }

Objects are data and functions wrapped up together Make global function that uses no data in the object (like cube) Show it can be called independently Install it in an object inside main Show it can be called as object method

Objects are data and functions wrapped up together Make global function that refers to data in the object, using “this” Install it in an object inside main Show it cannot be called directly Show it can be called as obj method and uses data in obj

Objects are data and functions wrapped up together Make function inside object declaration that refers to data in the object, using “this” Show it can be called as obj method and uses data in obj

Objects are data and functions wrapped up together  Make a function that creates a new object locally  Loads up some (or all) the data fields  Creates some functions (methods) in the object  Returns the object back to the calling function  This function is a constructor ◦ it creates perhaps many object and they all are similar… ◦ same data stored, same methods to work on that data