1 / 33

Arrays

Arrays. grades for students in a large course. we need a lot of variables. double[] grade; grade=new double[numberOfStudents];. creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1]. Arrays. double[] grade; grade=new double[numberOfStudents];. creates “variables”

japplegate
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays grades for students in a large course we need a lot of variables double[] grade; grade=new double[numberOfStudents]; creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1]

  2. Arrays double[] grade; grade=new double[numberOfStudents]; creates “variables” grade[0],grade[1],...,grade[numberOfStudents-1] the variables have nice names double sum=0,average; for (int i=0;i<numberOfStudents;i++) sum+=grade[i]; average=sum/numberOfStudentes;

  3. Arrays – another example String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; int findPhoneNumber(String searchedName) { int i=0; while ((i<numberOfPeople)&& (!searchedName.equals(name[i]))) i++; if (i==numberOfPeople) return –1; else return phoneNumber[i]; }

  4. EXERCISE #1: String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; Do we need to search the phone book sequentially?

  5. EXERCISE #2 - OOP: String[] name; int[] phoneNumber; name=new double[numberOfPeople]; phoneNumber=new int[numberOfPeople]; not OOP! What class(es) should we use? Define the class(es).

  6. Arrays as parameters public class Test { public static double average(double[] a) { if (a.length>0) { double sum=0; for (int i=0;i<a.length;i++) sum+=a[i]; return sum/a.length; } } else return 0; } We can figure out the size of the array!

  7. Returning arrays public class Test { public static int[] firstNumbers(int x) { int[] a=new int[x]; for (int i=0;i<x;i++) a[i]=i; return a; } }

  8. EXERCISE #3a: Define a method void mirror(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 10 | 6 |

  9. EXERCISE #3b: Define a method int[] mirror(int[] a) { } The method should not modify the input array, but return a mirrored copy a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 10 | 6 |

  10. EXERCISE #4: Define a method int maximum(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | 10

  11. EXERCISE #5: Define a method int indexOfMaximum(int[] a) { } a = | 6 | 4 | 3 | 2 | 1 | 0 a = | 6 | 10 | 3 | 2 | 1 | 1 a = | 6 | 1 | 3 | 2 | 10 | 4

  12. EXERCISE #6a: Define a method void sort(int[] a) { } a = | 6 | 10 | 3 | 2 | 1 | a = | 1 | 2 | 3 | 6 | 10 |

  13. int indexOfMaximum(int[] a,int length) { int index=0; for (int i=1;i<length;i++) if (a[i]>a[index]) index=i; return index; } void sort(int [] a) { for (int length=a.length;length>0;length--) { int index=indexOfMaximum(a,length); int c=a[length-1]; a[length-1]=a[index]; a[index]=c; } }

  14. EXERCISE #7a: Define a method int split(int[] a,int x) { } 1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4 and 4 1,4,2,3,2,3,3,2,4,9,8,8,9,9,5 returned index smaller or equal larger

  15. int split(int[] a,int x) { int b=0,e=a.length-1; while (b<=e) { while ((b<=e)&&(a[b]<=x)) b++; while ((b<=e)&&(a[e]>x)) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; } } return b-1; } (in place – solution) INVARIANT = (i<b => a[i]<=x) && (i>e => a[i]>x)

  16. EXERCISE #7b: Define a method int split(int[] a,int x,int start,int end) { } 9,3,5,1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4,2,3 and 4 9,3,5,1,4,2,3,2,3,3,2,4,9,8,8,9,9,5,2,3 returned index smaller or equal larger

  17. int split(int[] a,int x,int start,int end) { int b=start,e=end; while (b<=e) { while ((b<=e)&&(a[b]<=x)) b++; while ((b<=e)&&(a[e]>x)) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; } } return b-1; } INVARIANT = (i<b => a[i]<=x) && (i>e => a[i]>x)

  18. Can we use split for sorting? void sort(int[] a,int start,int end) { int middle=split(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,stop); } This never finishes.

  19. Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,end); } } it can be that middle=end or middle+1=start This never finishes, too.

  20. Can we use split for sorting? Solution – modify split so that start<=middle<end. We need to modify the specification too... it can be that middle=end or middle+1=start This never finishes, too.

  21. EXERCISE #7c: + each part has at least one element!!! Define a method int split2(int[] a,int x) { } we are promised that x occurs in a 1,9,8,4,5,2,3,8,9,2,3,9,5,3,2,4 and 4 1,4,2,3,2,3,3,2,4,9,8,8,9,9,5 returned index smaller or equal larger or equal

  22. int split2(int[] a,int x) { int b=0,e=a.length-1; while (true) { while (a[b]>x) b++; while (a[e]<x) e--; if (b<e) { int c=a[b]; a[b]=a[e]; a[e]=c; b++; e--; } else return e; } } INVARIANT: (i<b => a[i]<=x) && (i>e => a[i]>=x) CLAIM: (e>=0)&&(e<a.length-1)

  23. Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split2(a,a[start],start,end); sort(a,start,middle); sort(a,middle+1,end); } } it cannot be that middle=end or middle+1=start Now it always finishes.

  24. Can we use split for sorting? void sort(int[] a,int start,int end) { if (start<end) { int middle=split(a,a[start],start,end); sort(start,middle); sort(middle+1,stop); } } a[(int)(Math.random()*(end-start)+start)] QUICKSORT – one of the fastest sorting algorithms!

  25. Array index out of bounds int a=new int[10],b=0; for (int i=0;i<=10;i++) { a[i]=i; b+=a[i]; } System.out.println(“”+b);

  26. Initializing arrays int[] smallPrimes={2,3,5,7,11,13,17,23,29}; int[] smallPrimes; smallPrimes =new int[10]; smallPrimes[0]=2; smallPrimes[1]=3; .... smallPrimes[9]=29;

  27. EXERCISE #8: int[] a = {1,2,3}; int[] b = {4,5}; int[] c; c=a; a=b; b=c; System.out.println("a[0] = "+a[0]); System.out.println("a.length = "+a.length);

  28. EXERCISE #9: void swap(int[] d,int []e) { int[] f=d; d=e; e=f; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a,b); System.out.println("a[0] = "+a[0]);

  29. EXERCISE #10: void swap(int[] d,int []e) { int f=d[0]; d[0]=e[0]; e[0]=f; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a,b); System.out.println("a[0] = "+a[0]); System.out.println("a[1] = "+a[1]);

  30. EXERCISE #11: void swap(int d,int e) { int f=d; d=e; e=d; } int[] a = {1,2,3}; int[] b = {4,5}; int[] c; swap(a[0],b[0]); System.out.println("a[0] = "+a[0]); System.out.println("a[1] = "+a[1]);

  31. Difference from C++ class Test { private int data; public Test(int data) { this.data=data; } public int GetValue() { return data; } } Test[] a; a=new Test[10]; a[0]=new Test(3); System.println(a[0].GetValue()); System.println(a[1].GetValue()); error!

  32. Multidimensional Arrays grades for students in a large course, want to keep grade for each assignment separately double[][] grade; grade=new double[numberOfStudents][numberOfHomeworks]; Compute the average from i-th homework? Compute the sum for each student?

  33. Multidimensional Arrays double[][] grade; grade=new double[numberOfStudents][numberOfHomeworks]; double[][] grade; grade=new double[numberOfStudents][]; for (int i=0;i<numberOfStudents;i++) grade[i]=new double[numberOfHomeworks];

More Related