Arrays quiz 1 int quiz 1 quiz 2

  • Slides: 14
Download presentation
Arrays

Arrays

quiz 1 int quiz 1, quiz 2, quiz 3; System. out. println("Enter a quiz

quiz 1 int quiz 1, quiz 2, quiz 3; System. out. println("Enter a quiz score: "); quiz 1 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 2 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 3 = keyboard. next. Int(); 18 quiz 2 15 quiz 3 19

quiz 1 System. out. println("The average score is " + avg(quiz 1, quiz 2,

quiz 1 System. out. println("The average score is " + avg(quiz 1, quiz 2, quiz 3)); 17 quiz 2 15 quiz 3. . . 19 public static double avg (int q 1, int q 2, int q 3) { double average = (q 1 + q 2 + q 3) / 3. 0; return average; } The average score is 17. 0

quiz 1 int quiz 1, quiz 2, quiz 3, quiz 4, quiz 5, quiz

quiz 1 int quiz 1, quiz 2, quiz 3, quiz 4, quiz 5, quiz 6, quiz 7, quiz 8, quiz 9, quiz 10; System. out. println("Enter a quiz score: "); quiz 1 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 2 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 3 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 4 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 5 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 6 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 7 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 8 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 9 = keyboard. next. Int(); System. out. println("Enter a quiz score: "); quiz 10 = keyboard. next. Int(); 18 quiz 2 15 quiz 3 19 quiz 4 16 quiz 5 20 quiz 8 18 quiz 6 12 quiz 9 15 quiz 7 20 quiz 10 16

quiz 1 18 System. out. println("The average score is " + avg(quiz 1, quiz

quiz 1 18 System. out. println("The average score is " + avg(quiz 1, quiz 2, quiz 3, quiz 4, quiz 5, quiz 6, quiz 7, quiz 8, quiz 9, quiz 10)); 15 quiz 3 19 . . . public static double avg(int q 1, int q 2, int q 3, int q 4, int q 5, int q 6, int q 7, int q 8, int q 9, int q 10) { double average = (q 1 + q 2 + q 3 + q 4 + q 5 + q 6 + q 7 + q 8 + q 9 + q 10) / 10. 0; return average; } quiz 2 quiz 4 16 quiz 5 20 quiz 8 18 quiz 6 12 quiz 9 15 How about 20 scores? 100 scores? quiz 7 20 quiz 10 16

Arrays

Arrays

quiz int [ ] quiz; declared using square brackets reference variable

quiz int [ ] quiz; declared using square brackets reference variable

quiz int [ ] quiz; quiz = new int [ 7 ]; instantiate using

quiz int [ ] quiz; quiz = new int [ 7 ]; instantiate using new specify size in brackets

quiz int [ ] quiz; declaration quiz = new int [ 7 ]; instantiation

quiz int [ ] quiz; declaration quiz = new int [ 7 ]; instantiation quiz[0] = 18; 18 [0] quiz[5] = quiz[0] + 2; quiz[7] = 14; Array. Index. Out. Of. Bounds. Exception 20 [1] [2] [3] index values [4] [5] [6]

quiz int [ ] quiz; quiz = new int [ 7 ]; for (int

quiz int [ ] quiz; quiz = new int [ 7 ]; for (int i = 0; i < quiz. length; i++) { returns the number of array elements System. out. println("Enter a score: "); quiz[i] = keyboard. next. Int(); } accesses the ith element of the array 20 14 17 0 15 12 19 [0] [1] [2] [3] [4] [5] [6]

System. out. println("The average score is " + average( quiz ) ); . .

System. out. println("The average score is " + average( quiz ) ); . . . q quiz pass the entire array as an argument to the method parameter is an array of the same type public static double average ( int[ ] q ) { int sum = 0; for (int i = 0; i < q. length; i++) sum += q[i]; return (double) sum / q. length; } 20 14 17 0 15 12 19 [0] [1] [2] [3] [4] [5] [6] The average score is 13. 86

Practice • Declare and instantiate an array to hold 100 random integers. • Write

Practice • Declare and instantiate an array to hold 100 random integers. • Write a loop to assign the array to random values between 1 and 1000.

Practice • Declare an array to hold rainfall (in inches) for a week. •

Practice • Declare an array to hold rainfall (in inches) for a week. • Write a loop to read the rainfall values from the keyboard. • Write a method to return the largest value in the array.