DOC PREVIEW
UVA CS 101 - Multidimensional Arrays

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Exam 2 ReviewMultidimensional ArraysTwo Dimensional Arrays in JavaSetting 2D Array Values at Compile TimeMatrix AdditionFunctions/Static MethodsComponents of a FunctionThings to Remember About FunctionsScope2.2 Libraries and ClientsLibrariesKey Points to RememberRecursionSlide 14How-To’s on Writing Recursive FunctionsRecursion Exercise FactorialRecursion Exercise Fibonacci NumbersObjectsThings to Remember About ObjectsExam 2 ReviewMultidimensional Arrays3Two Dimensional Arrays in JavaArray access. Use a[i][j] to access element in row i and column j. Zero-based indexing. Row and column indices start at 0.double[][] a = new double[10][3];for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { a[i][j] = 0.0; }}Declaring and Initializing a 10-by-3 Array4Setting 2D Array Values at Compile TimeInitialize 2D array by listing values. double[][] p = { { .02, .92, .02, .02, .02 }, { .02, .02, .32, .32, .32 }, { .02, .02, .02, .92, .02 }, { .92, .02, .02, .02, .02 }, { .47, .02, .47, .02, .02 }, };5Matrix AdditionMatrix addition. Given two N-by-N matrices a and b, define cto be the N-by-N matrix where c[i][j] is the sum a[i][j] + b[i][j]. double[][] c = new double[N][N];for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) c[i][j] = a[i][j] + b[i][j];Functions/Static MethodsComponents of a FunctionThings to Remember About FunctionsHow many values can a function return?Zero or OneFunctions can be overloadedDifferent argument typesDifferent number of argumentsDifferent return value is NOT overloadingFunctions are usually called by valueArgument values are copied from function call-site to the function bodyThese arguments are local variables in the functionWhat is the other way of calling functions?Call by Reference9ScopeScope (of a name). The set of statements that can refer to that name.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) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) { double x = sqrt(a[i]); StdOut.println(x); } }}two differentvariables withthe same name iscope of cscope of epsilonscope of t2.2 Libraries and ClientsIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 1/14/19 1/14/1911LibrariesLibrary. A module whose methods are primarily intended for useby many other programs.Client. Program that calls a library.API. Contract between client andimplementation.Implementation. Program thatimplements the methods in an API.Key Points to Remember•Create a class file with all your functions•Use the standard Save -> Compile sequence•This class file does NOT have a main() function•Use the name of the class to invoke methods in that library•Can do this from any Java program12Recursion14RecursionWhat is recursion? When one function calls itself directly or indirectly.Gcd. Find largest integer d that evenly divides into p and q.base casereduction steppublic static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q);}base casereduction step,converges to base case15How-To’s on Writing Recursive FunctionsBase Case: You must check if we’ve reached the base case before doing another level of recursion! Make Progress Towards Base Case:Your recursive calls must be on a smaller or simpler input. Eventually this must reach the base case (and not miss it).Multiple recursive calls:Sometimes more than one recursive call.–H-Tree, Towers of HanoiAre their return values chosen, combined?Recursion ExerciseFactorialn! = n*(n-1)! (Reduction Step)0! = 1 (Base Case)public static int factorial( int n )J {JJJ if ( n <= 0 )J // base caseJJJJ return 1;JJJ elseJJJ // reduction stepJJJJ return ( n * factorial ( n - 1 ) );}Recursion ExerciseFibonacci NumbersF(N) = F(N-1) + F(N-2) (Reduction Step)F(0) = 0; F(1) = 1 (Base Case)public static long Fib(int n) {if (n == 0) return 0;if (n == 1) return 1;return Fib(n-1) + Fib(n-2);} What is the running time (computational cost) of this program?Draw the recursion treeObjectsThings to Remember About ObjectsFor this exam, you need to know only how to USE objectsClass APIConstructorInstance MethodsDifference between constructors and


View Full Document

UVA CS 101 - Multidimensional Arrays

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Multidimensional Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Multidimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Multidimensional Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?