Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Computer Architecture CSCE 350
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and 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.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Sorts, CompareTo Method and Strings
Sorting Mr. Jacobs.
Arrays (review) CSE 2011 Winter May 2018.
Java Review: Reference Types
C Passing arrays to a Function
More About Data Types & Functions
Object Oriented Programming in java
Introduction to Computer Science
Presentation transcript:

Arrays

2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that can hold a series of values – Just a new name for matrix – Just like a matrix index, an array uses an index to access a particular value

Array: composite type Multiple elements/items can be stored and retrieved, easily (in constant time). Can store any kind of element – primitive types or objects themselves. Ex. int[] a; String[] s; [] ;

Initialization [] = new [ ]; [] ={item1,item2,item3, …,itemn}; Once constructed, size remains fixed.

5 Initializing an array int[] justAVector = {2, 3, 5, 7, 11, 13, 17, 19, 23}; String[] myFriendsNames = {“Ram”, “Rahim”, “Kabir”, “Gita”, “Sita”}; char firstFewDigitsAsChars[] = {‘0’, ‘1’, ‘2’, ‘3’}; boolean whichOfMyFriendsAreTall[] = {false, false, true, true, false};

Array: size, addressing int[] a=new int[10]; Size of an array: a.length Element in array accessed by: a[0], a[1], etc. a[ ] Array index starts at: 0 ends at: n-1 (assuming size/length of the array is n).

7 Array layout in memory Recall that every variable requires some space to be stored in memory – Often the compiler is responsible for allocating this space – So, every variable has an address (just like you and I have addresses) – The address is often called a reference of a variable in Java – If I try to print the value at this address, I will get the value of the variable How is an array stored in memory?

8 Array layout in memory Array elements stored contiguously in memory – Starting address of a[] (same as the address of a[0]), add 8 to get the address of a[1] and so on) – doubles are 64 bits in size and memory is always byte addressed (one byte is 8 bits) – Array variable ‘a’ is really a pointer to the starting location or address of the array and is called a reference. – When calling a method, for an array argument the reference is passed. So modifications to the array inside the method are reflected outside the method – passing individual array elements passes the value (private copies are made in the method to hold the value so changes are not reflected outside.)

9 Always pass by value Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method. – Modifying a value in a method does not have any effect on the caller – But if the value is a reference then the change affects ‘what is referred to’ and so all references will see the change. References are addresses and hence a modification of the contents at the address will have “global” visibility On the other hand non-referential argument values are just copied in the “local memory” of a method; so a modification to a value within a method always remains local (changes contents only of local memory)

10

11

The Sorting Problem Let S be a sequence of entities which can be ordered (that is for any 2 elements, say a, b in the sequence exactly one of the following is true a b or a=b). Sort(S) : arranges the sequence in ascending or descending order.

Sort algorithms The sorting problem is one of the best studied problems and many algorithms exist. It is also practically important – sorting is routinely required in almost all database centred applications.

Selection sort: example //green is min, | is separator // after first round, blue sorted //stop since only one left

Selection sort algorithm //S is the array to be sorted. sepIndex=0; loop (s.length-1) times { minI=findMinIndex(S,sepIndex); exchange(S,sepIndex,minI); sepIndex++; }

Selection sort - findMinIndex int findMinIndex(from) { minI=from; for(i=from+1;index<S.length;i++) { if (S[i]<S[minI]) minI=i; } return minIndex; }

Bubble sort: example //sort ascending, | is unsorted-sorted separator //interchange neighbours from 0 till end //after first pass //after second pass //after third pass //4th pass, no interchange Stop condition: no interchange or | is at start.

Bubble sort algorithm lim=S.length-1; while (bubblePass(S,lim) && lim>0) { lim--; }

Bubble sort - bubblePass bubblePass(to) interchange=false; for(i=0; i<to; i++) { if (S[i]>S[i+1]) { exchange contents of S[i],S[i+1] interchange=true; } return interchange;

Quicksort qsort(S) { p=choose_pivot(S);//can be first /* S into S1,S2 with S1 containing elements p.*/ partition(p,S,S1,S2); SS=append(qsort(S1),p,qsort(S2)); return SS; }

Mergesort msort(s) { /* Split S into two (almost) equal parts s1,s2*/ split(s,s1,s2); msort(s1); msort(s2); /* at this point s1, s2 are sorted */ merge(s1,s2,ss); return ss; }

Merge merge(s1, s2) { s=initialize to s1.length+s2.length i1=0;i2=0,i=0; while(i1<s1.length && i2<s2.length){ if(s1[i1]<s2[i2])s[i]=s1[i1++]; else s[i]=s2[i2++]; i++ } // Now copy rest of s1 or s2 to s return s;}