Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS0007: Introduction to Computer Programming Array Algorithms.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
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
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 9: Advanced Array Concepts
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
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 
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.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6: Arrays: Lists and Tables
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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];
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Objectives You should be able to describe: One-Dimensional Arrays
Chapter 9: Sorting and Searching Arrays
Chapter 7 Arrays.
Arrays An Array is an ordered collection of variables
Chapter 8 Arrays Objectives
Review of Arrays and Pointers
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Chapter 8 Arrays Objectives
Presentation transcript:

Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with linear or binary search

Why arrays Consider a program to find the average of a set of numbers. How could we do this. We could read each number one at a time Sum all of the values together and keep a count to compute the average How do we then find the median value. The actual value closes to the average?

Consider a list of 3 numbers. Number1=5 Number2=10 Number3=7 Which is the median. How did we find that value? What would the code look like?

The median sum = n1+n2+n3 ave=sum/3 If (Math.abs(ave-n1) < Math.abs(ave-n2) && Math.abs(ave-n1) < Math.abs(ave- n3) ) median = n1 Else …

Complex for three number even more complex for more Arrays to the rescue. Arrays can store a list of values under the same name. Each value can be access individually using a subscript or index value or variable.

Array storage The array named Data stores 5 values. Each element in a array can be used in out program just like an other variable of that type. Data[0]=5; Sum=Data[0]+Data[1] Data[0]5 Data[1]10 Data[2]7 Data[3]2 Data[4]13

Making an array Declaring an array int[] Data; int Data[]; Allocating space for an arrays elements Arrays are reference type variable in Java so space must be allocated for them int[] Data =new int[100]; Or ---- int[] Data; Data =new int[100]; The new key word is required when making space for reference type data

Properties of arrays Indexing begins at zero The.length property returns the number of elements in the array. So Data.length would be 100 It is an error to access a data element in a array that is not defined. This will result in a logic error that can be difficult to debug.

Processing an array A loop is used to process an array Display value Sum values Initialize values Load values For (int x=0;x< Data.length;x++) Data[x]=0;

Processing arrays When working with arrays it is common practice to create a variable to keep track of the actual valid data items in the array. Arrays may be declared at an arbitrarily large side the actual count of valid data elements in the array may be less. If this is the case then using the arrays length property is not valid. For (int x=0;x< Data_cnt;x++) System.out.println(Data[x]);

Initialization lists String[] Month ={ “”, ” Jan ”, ” Feb ”, …” Dec ” }; System.out.println(Month[3]); int[] Data={ 5,10,7,12,2 }; Can only be done when the array is declared or allocated So this can be done later in the program Data = new int[] { 5,2,5,3,1 };

Lets make an example Write a program to collect grade scores from the user and store into an array. Write a loop to total the scores Write a loop to find the largest value Write a loop to count the number of time the largest value is entered.

Sample import javax.swing.*; public class grades { public static void main (String arg[]) { String snum; int num1=0; int i,sum; int largest; int largest_cnt; int[] data = new int[100]; int data_cnt=0; // read in the numbers while (( num1>=0 ) && (data_cnt<100)) { snum = JOptionPane.showInputDialog("Enter a Number?"); num1=Integer.parseInt(snum); data[data_cnt] = num1; data_cnt++; } data_cnt--; // print the list for(i=0; i<data_cnt;i++) { System.out.print(" " + data[i] + " "); } sum=0; // compute the total for(i=0; i<data_cnt;i++) sum=sum+data[i]; JOptionPane.showMessageDialog(null,"The sum is"+sum,"Results", JOptionPane.PLAIN_MESSAGE); sum=0; // compute the total for(i=0; i<data_cnt;i++) sum=sum+data[i]; JOptionPane.showMessageDialog(null,"The sum is"+sum,"Results", JOptionPane.PLAIN_MESSAGE); largest = 0; // find the largest value for(i=0; i<data_cnt;i++) { if (data[i] > largest) largest = data[i]; } JOptionPane.showMessageDialog(null,"The largest is"+largest,"Results", JOptionPane.PLAIN_MESSAGE); largest_cnt=0; // Count the number of times the largest value appears in the list for(i=0; i<data_cnt;i++) { if (data[i] == largest) largest_cnt ++; } JOptionPane.showMessageDialog(null,"The count of largest is"+largest_cnt,"Results", JOptionPane.PLAIN_MESSAGE); System.exit(0); }

Using arrays in an assignment newlist = list This assignment would seem like it should copy the values from list to newlist. It does not. Remember that an array is a reference type data. So this actually causes newlist and list to reference that same data. List elements list newlist

To copy the data in an array We must create a loop Then in the loop we must copy each element from one array to the destination. List elements list newlist List elements

Passing arrays as arguments Arrays are passed by reference to a method. So changes to the arrays elements in the method are shared by the calling method.

Multidimensional arrays Arrays can have more then one index. These are called multidimensional. So Int[][] data; Or Int data[][]; Declare a two-dimensional array Grid = new int[5][5]; will define the memory for the array 5 by 5 in size

Searching an array A form of array processing Linear search or sequential search Check each element looking for the data until it is found. Very simple algorithm. Binary search Only works for ordered lists. Divide and conquer. Check the middle of the list is it high or low. Then divide the search range in half and continue this process. Much faster then a sequential search

Sorting arrays Given an array of data order the data in some way. Maybe ascending or descending order. Maybe numeric or Alphabetic order The most basic form or sorting involved nested loops with comparisons and swamps

So a simple exchange sort ElementInitPass1Pass2Pass3Pass 4 Data[0]52222 Data[1]25444 Data[2]10 55 Data[3] Data[4]888810

The code for(out=0; o<datasize;out++) { for(in=out; in<datasize;in++) { If (data[out]<data[in]) { // swap data[out] and data[in] temp=data[out]; data[out]=data[in]; data[in]=temp; }

Let look at the same code It is actually a little different then the process we described. It does work it is not optimal What could be different???

An intuitive sort algorithm Try to find the lowest value in a list then swap it with the value in the first array slot. Then find the next lowest value in the list and try to swap it with the second array slot. Continue this process for each array slot up to the end of the list.

Homework TBD