Description
Tutorial 5
[Experimental Objective] Learn array initializer (Declare, create, and initialize).
l Learn how to copy and print array by for loop
l Learn how to using array to realize some simple algorithms
[Before Exercises]
(1) Type following code, try to create two arrays and use two different ways to print them.
int[] array1 = {1,2,3,4,5}; int[] array2 = new int[5]; array2[0] = 6; array2[1] = 7; array2[2] = 8; array2[3] = 9;
array2[4] = 10;
for(int i = 0; i<array1.length; i++){ System.out.print(array1[i] + ” “);
}
System.out.println(); for(int e:array2){ System.out.print(e + ” “);
}
System.out.println();
(2) Continue typing, create another array without giving it an address, and then finding what is the difference between two arrays.
int[] array3 = null; System.out.println(array3);
array3=array2;
System.out.println(array3);
(3) Why the first loop cannot change the value of array3? The second loop can change the value.
for(int e:array3){
e=1;
}
System.out.println(“array3: ” + Arrays.toString(array3)); for(int i = 0; i<array3.length; i++){ array3[i] = 1;
}
System.out.println(“array3: “+Arrays.toString(array3));
(4) We change the value of elements in array3, why are the elements in array2 changed accordingly?
System.out.println(“array2: ” + Arrays.toString(array2));
[Advanced]Try following code:
char[] Array4 = {‘a’, ‘b’, ‘c’}; System.out.println(Array4);
(5) Type following code, try to create two 2D arrays and initialize them.
int[][] array0 = new int[2][4]; int[][] array1 = {{1,2,3,4},{0,1,2,3}}; int[][] array2 = new int[2][]; array2[0] = new int[3]; array2[1] = new int[4];
for(int i = 0; i<array1.length; i++){
for(int j = 0; j<array1[0].length; j++){
System.out.print(array1[i][j] + ” “);
}
System.out.println(“”);
}
System.out.println(“———————”);
for(int i = 0; i<array2.length; i++){ for(int j = 0; j<array2[i].length; j++){ array2[i][j]=1;
}
}
for(int i = 0; i<array2.length; i++){ for(int j = 0; j<array2[i].length; j++){
System.out.print(array2[i][j] + ” “);
}
System.out.println();
}
[Exercises]
1. Suppose there are 10 students in a class, and we want the average score of these 10 students. Input 10 scores ([0, 100]) from the keyboard. Then after removing the highest score and the smallest score, please find the average score of the other 8 scores.
Please input 10 scores of these students:88.3 99 45 78 67.5 98.4 23.5 65.5
82 85.4
Average score is 76.26
2. Write a program to compare two arrays.
(1) Let user input the array size, (2) Let user input the two arrays,
(3) Output the result.
3. Given a one-dimensional array containing n positive integers (0<n<100), the number of the array represents the height of the column, and the depressed portion between the columns can store water. As the figure shown below, you can store 6 units of water. Write a program to calculate how many units of water an array can store.
4. Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program
Enter the integers between 1 and 100: 22 33 35 34 99 87 45 34 23 78 45 33
11 23 87 34 76 0
11 occurs 1 time
22 occurs 1 time
23 occurs 2 times
33 occurs 2 times
34 occurs 3 times
35 occurs 1 time
45 occurs 2 times
76 occurs 1 time
78 occurs 1 time
87 occurs 2 times
99 occurs 1 time
5. Write a program to get students’ grades of many courses and print scores and average scores in a grade table.
(1) Get the number of students sNum(less than 10) and the number of courses cNum(less than 10) from the console
(2) Get scores from console. The scores are inputted by cNum lines. In each line, there are sNum students’ scores of one course in order.
(3) Print a grade table of cNum+2 columns and sNum+2 rows , of which the first row are course names, the first column are student names, the last row are the average scores of each course and the last column are the average scores of each students.
Sample:
Reviews
There are no reviews yet.