CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 8: Arrays.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
One Dimensional Arrays
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
1 Various Methods of Populating Arrays Randomly generated integers.
CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
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.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Numbers and Arrays. Widening and narrowing Numeric types are arranged in a continuum: double float long int short byte, char You can easily assign a narrower.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Slides modified by Erin Chambers Problem Solving and Algorithm Design, part 2.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Recursion 1. Jump It  board of n squares, n >= 2  start at the first square on left  on each move you can move 1 or 2 squares to the right  each square.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Computer Science 210 Computer Organization Arrays.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Looping and Counting Lecture 3 Hartmut Kaiser
Introduction to Java Java Translation Program Structure
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
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];
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 CSE1301 Computer Programming: Where are we now in the CSE1301 syllabus?
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Java Programming Language Lecture27- An Introduction.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Lecture 14 Searching and Sorting Richard Gesick.
Basic notes on pointers in C
Lecture 9 Arrays Richard Gesick.
Computers & Programming Languages
Lecture 11 Searching and Sorting Richard Gesick.
Java Programming Review 1
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Dr. Sampath Jayarathna Cal Poly Pomona
Lecture 14 2D Arrays Richard Gesick.
1D Arrays and Lots of Brackets
Module 8 – Searching & Sorting Algorithms
1D Arrays and Lots of Brackets
Presentation transcript:

CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301 Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays – Initialization – Searching

CSE 1301 Arrays and Their Properties Hold several values of the same type (homogeneous) Based on a slot number (the index number) Instant access Linear (one after the other) Static – once their size is set, it’s set…

CSE 1301 What arrays look like Things to notice There are 7 slots, with index numbers 0 – 6 The name of the array is myArray Easy to be off by one myArray

CSE 1301 Creating Arrays [] = new [ ]; new brings complex variables to life in C# Notice that we can create an array of any data type, just by changing the data type!

CSE 1301 Examples An array of shorts: short[] someArray = new short[50]; An array of floats: float[] myArray = new float[25]; An array of booleans: bool[] list = new bool[65]; An array of chars: char[] characters = new char[255];

CSE 1301 Initializing Arrays with Values [] = new [ ] {VALUES}; Or [] = {VALUES};

CSE 1301 Modifying an Array You must specify which slot you are putting information in Example: int[] myArray = new int[50]; myArray[3] = 12; This won’t work: int[] myArray = new int[50]; myArray = 12; – Data type on the left of = is an array – Data type on right of = is an int … 49 12

CSE 1301 Accessing Information Copying information out of a particular slot Example: int clientAge; clientAge = myArray[4]; This copies information from the fifth slot (slot four) into the variable clientAge

CSE 1301 Initializing Arrays (large arrays) For most arrays, you will use a loop to initialize Problem: create an array of 50 bytes and fill each slot with the number 42 Solution: byte[] myList = new byte[50]; for (int counter=0; counter < 50; counter++) { myList[counter] = 42; }

CSE 1301 Line by Line (a smaller example) byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; }

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter

CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter false

CSE 1301 Finding the Smallest Element If you were given an array of numbers, how would YOU do it? Computers can only compare two at a time Go through entire list Keep track of smallest so far Let’s assume – We have an array of 5 integers – The array contains random unknown numbers – The name of the array is called randomArray

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 1 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 1 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 1 counter Is 42 > 17?

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 1 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 1 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 2 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 2 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 2 counter Is 17 > 42?

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 2 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 3 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 3 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 3 counter Is 17 > -8?

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 3 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 3 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 4 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 4 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 4 counter Is -8 > 4?

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 4 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 5 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 5 counter

CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for smallestSoFar 5 counter

CSE 1301 Summary Arrays hold values that are all the same type Arrays are static in size Creating arrays always follows a format You can create an array of any type To initialize or search arrays, you’ll almost always use a loop