David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Programming and Data Structure
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.
David Stotts Computer Science Department UNC Chapel Hill.
CS0007: Introduction to Computer Programming Array Algorithms.
© red ©
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
Programming with Collections Collections in Java Using Arrays Week 9.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
©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.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 9: Arrays and Strings
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
David Stotts Computer Science Department UNC Chapel Hill.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Mrs. Smith’s 7th Grade Reading Blue Class Mrs. Smith’s 7th Grade Reading Blue Class Mrs. Smith’s 7th Grade Reading Blue Class.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Chapter 8 Arrays and Strings
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
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.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
David Stotts Computer Science Department UNC Chapel Hill.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
If you say 8 color the ones in your picture purple. If you say 9 color the ones in your picture blue.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Python Programing: An Introduction to Computer Science
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
David Stotts Computer Science Department UNC Chapel Hill.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CPS Today’s topics l Algorithms and pseudocode l Notes adapted from Marti Hearst at UC Berkeley and David Smith at Georgia Tech Upcoming ä Beginning.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
David Stotts Computer Science Department UNC Chapel Hill.
1-1 Logic and Syntax A computer program is a solution to a problem.
Computer Programming BCT 1113
Colors.
Chapter 8 Arrays Objectives
Average Number of Photons
Can I color yellow?. Can I color yellow?
What Color is it?.
Chapter 8 Arrays Objectives
CS150 Introduction to Computer Science 1
ICS103 Programming in C Lecture 12: Arrays I
Let’s Learn the Basic Colors
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

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

“Musical chairs”

Arrays  Sometimes we need to access a collection of data values as a group, or systematically  We create a name for the entire collection (this is the abstraction)  We access items in the collection by number (called an index ) along with the name  Call this collection an Array

4 SeasonsBora Hut #2 “The Smiths want some papaya juice” “Go to Hut #2 and toss in some papaya juice”

Think of an array as a contiguous set of memory slots with one name for the whole var bbHuts = new Array( ); bbHuts n Each slot can hold a data value Each slot sort of like a variable Smith

var bbHuts = new Array( ); var k = 3 ; bbHuts[0] = “Jackson” ; bbHuts[1] = “Miller” ; bbHuts[2] = “Smith” ; bbHuts[ k ] = “Olafdottir” ; k++ ; // increments k bbHuts[ k ] = “Evans” ; k++ ; bbHuts[ k++ ] = “Xerxes” ; bbHuts[ k ] = “Wilson” ; bbHuts Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson k

alert( bbHuts[ k ] ) ; // prints “Wilson” bbHuts[ k+1 ] = bbHuts[ 2 ] + “-Jones” ; // does not change k // k+1 is NOT k++ if (bbHuts[ k-1 ] > bbHuts[ k ] ) { // a swap var tmp = bbHuts[ k ] ; bbHuts[ k ] = bbHuts[ k-1 ] ; bbHuts[ k-1 ] = tmp ; } bbHuts Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson k Smith-Jones tmp Wilson Xerxes Wilson

var x = 4; // arrays can be initialized similarly var colors = [ “red”, ”blue”, ”green”, ”yellow”, “orange”, “purple”, “rose”, “umber”, “mauve”, “chartreuse” ]; alert(colors[1]); // prints blue alert(colors.length); // prints 10, indexes go 0 to 9 alert(colors.indexOf(“rose”); // prints 6

var words = [ ] ; // common alternative to new Array( ); var songs = new Array(n) ; // alternative also, says make songs have n slots // doesn’t do much, but other languages do this // JS arrays grow longer as needed words[143] = “splendiferous” ; alert(words[0]) ; // prints undefined alert(words[142]) ; // prints undefined alert(words[-1]) ; // prints undefined alert(words.length) ; // prints 144

 Can store data related to some common concept in one array, and then get at the component values systematically  for loop is commonly used to get to all array elements one-at-a-time  Example: keep all student names in one array, so the array represents the entire course  Example: keep all student grades in an array, so the array represents the entire course

names Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson Smith-Jones grades k = 2; // grades[k] and names[k] // select all the information // about person k 2 2 a “student”, student 2

function myProg ( ) { size = Number(prompt(“how many students?”)); var students = new Array( size ); for (var sn=0; sn<size; sn++) { students[ sn ] = prompt(“student name?”); } var grades = new Array(size); for (var i=0; i<size; i++) { grades[i] = getGoodGrade( ); // data validation in the function } for (var k=0; k<size; k++) { alert(student[ k ] + “ has grade “ + grades[ k ]); } }

function getGoodGrade ( ) { // get a grade and return it after validation var num; num = Number(prompt(“what is the grade?”)); // now validate the input while ( num 100) { alert(“grade must be between 0 and 100”); num = Number(prompt(“what is the grade?”)); } return num ; }

 Now compute the average grade from the stored array of grades  Then go through the array of grades and find the largest grade  Also print the name of the student who had the highest grade