Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heads-Up – Programming Quiz This Week Announcement will be made via E-mail You will be provided with a login to the password-protected website You will.

Similar presentations


Presentation on theme: "Heads-Up – Programming Quiz This Week Announcement will be made via E-mail You will be provided with a login to the password-protected website You will."— Presentation transcript:

1 Heads-Up – Programming Quiz This Week Announcement will be made via E-mail You will be provided with a login to the password-protected website You will have 75 minutes to complete the quiz. E-mail will state the deadline Work on the quiz by yourself individually and upload it via Collab. You are NOT allowed to post questions about the quiz on forums or approach the instructor(s) or TAs for help. (Quizzes are like “take- home” exams)

2 2 Control Flow Summary Control flow. n Sequence of statements that are actually executed in a program. n Conditionals and loops: enables us to choreograph the control flow. n Manipulate small amounts of data. Straight-line programs All statements are executed in the order given. Conditionals Certain statements are executed depending on the values of certain variables. if if-else Loops Certain statements are executed repeatedly until certain conditions are met. while for do-while Control FlowDescriptionExamples

3 3 Manipulating More Data Goal. 10 variables of the same type. double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0; a1 = 0.0; a2 = 0.0; a3 = 0.0; a4 = 0.0; a5 = 0.0; a6 = 0.0; a7 = 0.0; a9 = 0.0; double x = a4 + a8;

4 1.4 Arrays

5 5 Arrays Arrays allow you to store and manipulate huge quantities of data. Program code remains concise. Array. Indexed sequence of values of the same type. Examples. n 52 playing cards in a deck. n 14 thousand undergrads at UVa. n 1 million characters in a book. n 10 million audio samples in an MP3 file. n 4 billion nucleotides in a DNA strand. n 1 trillion webpages crawled by Google! horton0 gurumurthi1 humphrey2 knuth3 billg4 rms5 mst3k6 alvin7 indexvalue Elements

6 6 Arrays in Java Java has special language support for arrays. n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } HelloWorld.java

7 7 Arrays in Java Java has special language support for arrays. n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. Compact alternative. n Declare, create, and initialize in one statement. n Default initialization: all numbers automatically set to zero. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0 int N = 10; double[] a = new double[N]; // declare, create, init

8 Initializing and Printing Arrays Alternative way of initializing an array: double[] a = {0.1, 0.2, 0.3}; Printing an array: INCORRECT System.out.print(a); //random looking junk CORRECT System.out.print(a[3]); //print element a[3] CORRECT for(int i=0;i < N;i++) System.out.println(a[i]); //print the entire array WHY DOES ARRAY MANIPULATION INVOLVE MORE WORK? 8

9 Basic Computer Hardware Central Processing Unit (CPU) Executes the machine- language instructions of the compiled program Memory Provides storage for program data Stores the program and other large data as files Disk 9

10 Role of Memory Provides storage for the data used by a program n Variables – One storage location per variable n Size of the location depends on the datatype of the variable Arrays n A sequence of variables of the same type n Need to store all the “variables” of the array in memory as a single logical unit 10

11 Memory Memory is split into equal sized units known as “addresses”. (Address Space) Arrays occupy consecutive addresses in this linear space: int[] a = {5, 6, 10}; Memory Allocation: Java allocates addresses for the array when you call new Key Point: Name of the array indirectly references its starting address. int[] b = new int[3]; b=a; //b references the SAME data in memory. BEWARE! 11 0 1 2... N-2 N-1 0 1 2 3 4 5 N-2 N-1 a[0] a[1] a[2] 5 6 10 Addresses

12 12 Vector Dot Product Dot product. Given two vectors x and y of length n, their dot product is the sum of the products of their corresponding components.

13 13 Vector Dot Product double[] x = { 0.3, 0.6, 0.1 }; double[] y = { 0.5, 0.1, 0.4 }; double sum = 0.0; for (int i = 0; i < 3; i++) { sum += x[i]*y[i]; // same as sum = sum + x[i]*y[i]; } += -= /= *=

14 14 Array Processing Code NOT i <= N What Does This Code Do? Dr. Java Demo of Array Reversal

15 15 An array’s length is fixed Each array keeps track of how many items it contains n In length field. Example: int[] list = { 3, -1, 0, 7, 5 } list.length == 5 // is true n Note use of “.” notation. More on this later, but for now think of it as “something inside of” the thing on the left n Important: an array’s length cannot change (grow, shrink) – Must know how many items will be stored when it’s created! int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; // Find index of first occurrence of target in array int i = 0; while ( i < list.length && list[i] != target ) i = i + 1; if ( i != list.length ) System.out.println("Found at index " + i);

16 Auto-increment and arrays Auto-increment and auto-decrement operators used with arrays. n ++i means “add one to i’s value and use the updated value” n i++ means “use i’s value now and then add one to the old value” n Can use on line by itself as a single statement – In which case they do exactly the same thing! n Differences between the auto-increment operators are important when used in expressions: y = 0; x = y++; y = 0; z = ++y; 16 int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; int i = 0; while ( i < list.length && list[i++] != target ) ; // empty loop body! if ( i != list.length ) System.out.println("Found at index " + (i-1) ); Y=0; x = y; y = y+1; Y=0; y = y+1; z = y;

17 Which Version of the Code is Better? 17 int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; // Find index of first occurrence of target in array int i = 0; while ( i < list.length && list[i] != target ) i = i + 1; if ( i != list.length ) System.out.println("Found at index " + i); int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; int i = 0; while ( i < list.length && list[i++] != target ) ; // empty loop body! if ( i != list.length ) System.out.println("Found at index " + (i-1) );


Download ppt "Heads-Up – Programming Quiz This Week Announcement will be made via E-mail You will be provided with a login to the password-protected website You will."

Similar presentations


Ads by Google