EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Chapter 10 – Arrays and ArrayLists
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Chapter 7: Arrays In this chapter, you will learn about
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Chapter 17 Recursion.
Data Structures ADT List
John Hurley Cal State LA
1 Lecture 16/3/11: Contents Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user- defined object.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java two neighbors 1. Initialization: 2.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CS110 Programming Language I
1 Various Methods of Populating Arrays Randomly generated integers.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Computer Programming Lab 8.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Java Unit 9: Arrays Declaring and Processing Arrays.
CS1101X: Programming Methodology Recitation 7 Arrays II.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
This week in CS 5 HW 9 (2 problems) M/T sections W/Th sections due Sunday, 11/4 at midnight due Monday, 11/5 at midnight Recitation for HW9 -- Friday 11/2.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
A: A: double “4” A: “34” 4.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
CSC111 Quick Revision.
Computing Adjusted Quiz Total Score
An Introduction to Java – Part I, language basics
Java Lesson 36 Mr. Kalmes.
Repetition Statements
Question 1a) What is printed by the following Java program? int s;
Presentation transcript:

EXAMPLES (Arrays)

Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in a video image. A common algorithm on these grids is to smooth the data by updating the value of each point to be the average of its old value and the points immediately surrounding it. –If the point were on the edge of the grid, the average would include only that point and its immediate neighbors that are on the grid. Write a Java method that smoothes the values in a 2-D grid.

private static void smoothImage(double [][] A){ double [][]temp = new double[A.length][A[0].length]; int i, j, x, y, count; for (i = 0; i < A.length; i++) for (j = 0; j < A[0].length; j++) temp[i][j] = A[i][j]; for (i = 0; i < A.length; i++) for (j = 0; j < A[0].length; j++){ count = 0; A[i][j] = 0; for (x = -1; x <= 1; x++) for (y = -1; y <= 1; y++) if ((i+x >= 0) && (i+x < A.length) && (j+y >= 0) && (j+y < A[0].length)){ A[i][j] += temp[i + x][j + y]; count++; } A[i][j] /= count; }

Example Suppose that you are given a 2-D array with integers and an integer value. Write a Java method that determines whether or not the integer is written as the summation of the successive values in the 2-D array.

private static boolean subsequenceSum(int [][]A, int value){ for (int i = 0; i < A.length; i++) for (int j = 0; j < A[0].length; j++){ if (value == A[i][j]) return true; int total = A[i][j]; for (int k = i + 1; k < A.length; k++){ total += A[k][j]; if (value == total) return true; } total = A[i][j]; for (int k = j + 1; k < A[0].length; k++){ total += A[i][k]; if (value == total) return true; } return false; }

Example The Bell triangle is the number triangle obtained by beginning the first row with the number one, and beginning subsequent rows with last number of the previous row. Rows are filled out by adding the number in the preceding column to the number above it Write a complete Java program to compute the values in the bell triangle when the row number is specified.

import java.util.*; public class BellTriangle{ public static void main(String [] args){ int [][] A; Scanner s = new Scanner(System.in); System.out.print("Enter a row number: "); int size = s.nextInt(); if (size <= 0) System.out.println("Enter a positive integer"); else A = computeBellTriangle(size); }

private static int [][] computeBellTriangle(int size){ int [][] T; T = new int[size][]; for (int i = 0; i < size; i++) T[i] = new int[i+1]; T[0][0] = 1; for (int i = 1; i < T.length; i++){ T[i][0] = T[i-1][i-1]; for (int j = 1; j < T[i].length; j++) T[i][j] = T[i][j-1] + T[i-1][j-1]; } return T; }