Upload
marian-mathews
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
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
Control Flow Summary
Control flow. Sequence of statements that are
actually executed in a program. Conditionals and loops: enables us to
choreograph the control flow. Manipulate small amounts of data.
Straight-lineprograms
All statements areexecuted in the order given.
ConditionalsCertain statements are
executed depending on thevalues of certain variables.
ifif-else
LoopsCertain statements are
executed repeatedly untilcertain conditions are met.
whilefor
do-while
Control Flow Description Examples
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;a9 = 0.0;
double x = a4 + a8;
1.4 Arrays
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. 52 playing cards in a deck. 14 thousand undergrads at UVa. 1 million characters in a book. 10 million audio samples in an MP3 file. 4 billion nucleotides in a DNA strand. 1 trillion webpages crawled by Google!
horton0
gurumurthi1
humphrey2
knuth3
billg4
rms5
mst3k6
alvin7
index value
Ele
ments
6
Arrays in Java
Java has special language support for arrays. 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 arraya = new double[N]; // create the arrayfor (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
Arrays in Java
Java has special language support for arrays. 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. Declare, create, and initialize in one statement. Default initialization: all numbers automatically set to zero.
int N = 10; double[] a; // declare the arraya = new double[N]; // create the arrayfor (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
Initializing and Printing Arrays
• Alternative way of initializing an array:double[] a = {0.1, 0.2, 0.3};
• Printing an array:• INCORRECTSystem.out.print(a); //random looking junk• CORRECTSystem.out.print(a[3]); //print element a[3]• CORRECTfor(int i=0;i < N;i++) System.out.println(a[i]); //print the entire array
WHY DOES ARRAY MANIPULATION INVOLVE MORE WORK?
8
Basic Computer Hardware
CentralProcessing
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
Role of Memory
Provides storage for the data used by a program
Variables – One storage location per variable
Size of the location depends on the datatype of the variable
Arrays A sequence of variables of the same
type Need to store all the “variables” of the
array in memory as a single logical unit
10
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
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.
1 2
1 2
1 1 2 2
1
[ , ,..., ]
[ , ,.., ]
. ...
n
n
n
i i n n
i
x x x
y y y
x y x y x y x y
x
y
x y
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];}
1 2
1 2
1 1 2 2
1
[ , ,..., ]
[ , ,.., ]
. ...
n
n
n
i i n n
i
x x x
y y y
x y x y x y x y
x
y
x y
+=-=/=*=
14
Array Processing Code
NOT i <= N
What DoesThis Code Do?
Dr. Java Demo of Array Reversal
15
An array’s length is fixed
Each array keeps track of how many items it contains In length field. Example:
int[] list = { 3, -1, 0, 7, 5 } list.length == 5 // is true
Note use of “.” notation. More on this later, but for now think of it as “something inside of” the thing on the left
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);
Auto-increment and arrays
Auto-increment and auto-decrement operators used with arrays. ++i means “add one to i’s value and use the updated value” i++ means “use i’s value now and then add one to the old
value” Can use on line by itself as a single statement
– In which case they do exactly the same thing! 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;
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) );