Data Structures Array - Code.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Building Java Programs Chapter 13 Searching reading: 13.3.
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.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Java SE 8 for Programmers, Third Edition
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
 Pearson Education, Inc. All rights reserved Arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
 2005 Pearson Education, Inc. All rights reserved Arrays.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
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.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Searching Arrays Linear search Binary search small arrays
Arrays Chapter 7.
using System; namespace Demo01 { class Program
Lecture 3 Linear Search and Binary Search ArrayLists
More on Recursion.
Recitation 13 Searching and Sorting.
Chapter 7 Single-Dimensional Arrays
Counted Loops.
Case Study 2 – Marking a Multiple-choice Test
Advanced Programming Chapter 8: Arrays
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array.
Java How to Program, Late Objects Version, 10/e
מיונים וחיפושים קרן כליף.
Introduction to Programming
Exam 2 Review 1.
7 Arrays.
Building Java Programs
CNG 140 C Programming (Lecture set 8)
Chapter 8 Multi-Dimensional Arrays
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference.
Review of Arrays and Pointers
Multidimensional Arrays
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Data Structures Array - Code.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
class PrintOnetoTen { public static void main(String args[]) {
Arrays Week 2.
Single-Dimensional Arrays chapter6
Binary Search class binarysearch {
Object Oriented Programming
Linear Search (Area Code Example)
INC 161 , CPE 100 Computer Programming
Chapter 6 Arrays.
Module 8 – Searching & Sorting Algorithms
Arrays in Java Prepare 1 whole yellow paper.
2 Array of Objects.
Sorting and Searching -- Introduction
Outline Declaring and Using Arrays Arrays of Objects
22C:21 Problem 2.3 Solution Outline.
How do you do the following?
Searching.
Presentation transcript:

Data Structures Array - Code

Examples Using Arrays Initialize elements of 10-element array to even integers public class InitArray { public static void main( String[] args ) final int ARRAY_LENGTH = 10; // constant int[] array = new int[ ARRAY_LENGTH ]; // create array // calculate value for each array element for ( int counter = 0; counter < array.length; counter++ ) array[ counter ] = 2 + 2 * counter; System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings // output each array element's value System.out.printf( "%5d%8d\n", counter, array[ counter ] ); } // end main } // end class InitArray * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * Pearson Education, Inc. All Rights Reserved.

Examples Using Arrays (Cont.) Summing the elements of an array public class SumArray { public static void main(String[] args) int[] array = {87, 68, 94, 100, 83, 78, 85, 91, 76, 87}; int total = 0; // add each element's value to total for (int counter = 0; counter < array.length; counter++) total += array[counter]; System.out.printf("Total of array elements: %d%n", total); } } // end class SumArray * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * Pearson Education, Inc. All Rights Reserved.

Multidimensional arrays public class InitArray { // create and output two-dimensional arrays public static void main(String[] args) int[][] array1 = {{1, 2, 3}, {4, 5, 6}}; int[][] array2 = {{1, 2}, {3}, {4, 5, 6}}; System.out.println("Values in array1 by row are"); outputArray(array1); // displays array1 by row System.out.printf("%nValues in array2 by row are%n"); outputArray(array2); // displays array2 by row } // output rows and columns of a two-dimensional array public static void outputArray(int[][] array) // loop through array's rows for (int row = 0; row < array.length; row++) // loop through columns of current row for (int column = 0; column < array[row].length; column++) System.out.printf("%d ", array[row][column]); System.out.println(); } // end class InitArray

Linear Search public class LinearSearch_Loop { public static void main(String[] args) { int array[] = new int[100]; for (int counter = 0; counter < array.length; counter++) { array[counter] = 2 * counter; } System.out.println( linearSearch(array, 10)); public static int linearSearch(int A[], int key) { for (int counter = 0; counter < A.length; counter++) { if (A[counter] == key) { return counter; return -1;

Binary Search public class BinarySearch { public static void main(String[] args) { int array[] = new int[10]; for (int counter = 0; counter < array.length; counter++) { array[counter] = 2 * counter;} System.out.println(); System.out.println(" The number is foun in the index " + binarySearch(array, 2)); }// end of main public static int binarySearch(int A[], int key) { int low = 0; int high = A.length - 1; int middle; while (low <= high) { middle = (low + high) / 2; if (key == A[middle]) { return middle; } else if (key < A[middle]) { high = middle - 1; } else { low = middle + 1; } return -1; }// end of binarySearch }// end of class