Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
CSE 113 Introduction to Computer Programming Lecture slides for Week 10 Monday, October 31 th, 2011 Instructor: Scott Settembre.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
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)
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Loops – While, Do, For Repetition Statements Introduction to Arrays
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Computer Science II Exam I Review Monday, February 6, 2006.
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:
ME 221Lecture 141 ME 221 Statics Lecture #14 Section 4.3.
Please open your laptops and pull up Quiz Only the provided online calculator may be used on this quiz. You may use your yellow formula sheet for.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
CHEMISTRY 10123/10125 Spring 2007 Instructor: Professor Tracy Hanna Phone: Office: SWR 418
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Please open Daily Quiz 34. A scientific calculator may be used on this quiz. You can keep your yellow formula sheets out when you take the quiz. Remember.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
CS 115 QUIZ # 1 INFORMATION Scheduled for TUESDAY 9/22/15 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Iteration Statements CGS 3460, Lecture 18 Feb 20, 2006 Hen-I Yang.
Please CLOSE YOUR LAPTOPS, and turn off and put away your cell phones, and get out your note- taking materials.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Basic Types CGS 3460, Lecture 19 Feb 22, 2006 Hen-I Yang.
Summary CGS 3460, Lecture 43 April 26, 2006 Hen-I Yang.
REMINDER: If you haven’t yet passed the Gateway Quiz, make sure you take it this week! (You can find more practice quizzes online in the Gateway Info menu.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1. PLEASE HELP US OUT WITH THIS: When you go to the open lab next door in 203, please make sure you sign in on the log sheet and enter your instructor’s.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
REMINDER: If you haven’t yet passed the Gateway Quiz, make sure you take it this week! (You can find more practice quizzes online in the Gateway Info menu.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Please CLOSE YOUR LAPTOPS, and turn off and put away your cell phones, and get out your note- taking materials.
Due Today: #1 pg (12-35) #2 pg (1-4, 6-34) #3 pg (35-41 odd, 43-60) #4 pg (13-24, 27, 28, , odd) Monday.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Syllabus and Class Policies MATH 130: Summer 2016.
One-Dimensional Array Introduction Lesson xx
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Programming Control Structures with JavaScript Part 2
Content Creation Jiaqi Xue 12/10/2012.
Presentation transcript:

Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang

Quiz 2 Average: Std Dev: Max: 94 Min: %  100% of HW2 83.5%  90% or above

Announcement Textbook is now in library as a 2-hour course reserve The details of the Optional Final Exam will be announced next Wednesday after Quiz3 Hint on Homework 3 Guest lecturer next Monday

Hints on Homework 3 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home-chess club ) + StreetDist ( home- library ) + StreetDist ( home-soccer field ) That’s a total of 10 variables you need to keep for each housing DO I HAVE TO WRITE A PROGRAM WITH 70 VARIABLES?

Hints on Homework 3 What are the “necessary” data needed to keep around? 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home-chess club ) + StreetDist ( home- library ) + StreetDist ( home-soccer field ) That’s a total of 10 variables you need to keep for each housing

StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home- chess club ) + StreetDist ( home-library ) + StreetDist ( home-soccer field ) double sum_street_dist = 0; double street_dist; int x, y; int i; for (i = 1; i <= 5; i++) { switch(i) { case 1: x = school_x; y = school_y; break; case 2: x = clinic_x; y = clinic_y; break; …. } street_dist=((home_x > x)?(home_x – x):(x - home_x)) + ((home_y > y)?(home_y – y):(y - home_y)); sum_street_dist = sum_street_dist + street_dist; } A

There are five housing. Can I use the same trick again? Yes! double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { switch(j) { case 1: home_x = house1_x; home_y = house1_y; break; case 2: home_x = house2_x; home_y = house2_y; break; …. } A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

Only keep the variables you need for later use. Try reuse same variables for similar calculations, especially those in the loops. In case of “maximize” and “minimize” type of problem, you can always only keep the info of the winner. In most of the meaningful programs, loops are your best friend. We reduce from 85 to 47 statements by using for loops and switch. If using arrays, we can eliminate 30 more statements to a total of 17. (But you’re not allowed to do that in this homework.) Can you make this work for problem 2 with only minor modification? Hints

Previously… type conversion, casting Introduction to array

Agenda Arrays One-dimension array Multi-dimension arrays

Array Aggregate variable vs. Scalar variables C support 2 kinds of aggregate variable: Arrays and structures A data structure for multiple data values, all of the same type  Elements  Index (Subscript) Close usage between array and for loop

More about Arrays 0-based index Out of bound problem Traversing through elements Array initialization sizeof(a)/sizeof(a[0]) Supermarket problem in homework 2

When not using array. double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { switch(j) { case 1: home_x = house1_x; home_y = house1_y; break; case 2: home_x = house2_x; home_y = house2_y; break; case 3: home_x = house3_x; home_y = house3_y; break; case 4: home_x = house4_x; home_y = house4_y; break; case 5: home_x = house5_x; home_y = house5_y; break; } A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

Why use Array? double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { home_x = house_x[j]; home_y = house_y[j]; A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

Multidimension Array Row major vs. Column major Remember the notation: m[3][5] not m[3,5] Use of nested loop for multidimension array Initialization of multidimension array const modifier Can I assign an array to another variable?

Summary Hints on Homework 3 Array One-dimension array Multidimension array

Before you go Read Chapter 8. Exercise: 8.8, 8.9, 8.11 Homework 3 due Mar 7, Tuesday at 11:59 pm Quiz 3 will be held on Mar 8th (Wed). Quiz 3 includes  homework 3,  textbook up to Sec 7.1, and  slides up to last Friday (2/24)

Pickup your Quiz 2 Last name: K-ZLast name: A-J