CS120 Lab 9 Array (Chapter 7)

Total Page:16

File Type:pdf, Size:1020Kb

CS120 Lab 9 Array (Chapter 7)

CS120 Lab 10 Part 1 - Nested Loops

Nested Loops: Sometimes, the body of a loop is again a loop. We say that the inner loop is nested inside an outer loop. This happens often when you process two-dimensional structures, such as tables. Let’s look at an example that looks a bit more interesting than a table of numbers. We want to generate the following right triangular shape:

* ** *** **** ***** ****** ******* ******** *********

Step 1: launching the Eclipse program from your desktop, click on “File” button on the toolbar, select “New Java Project”, and give Lab10 as the project name. Click Finish button.

Step 2: right click on Lab10 folder on the left column, select “New” and then “class” with a main() method (mark public static void main), name the class as “TriangularShape”. Then click on “Finish”. You need to follow up with Dr. Zhang’s instruction to complete this task.

CS120 Lab 10 Part 2– array (Chapter 7)

An array is our first data structure – a collection of related data items. An array is also like a container object that holds a fixed number of values of a single type (all int, all double, all string, but no mixing). The length of an array is established when the array is created. After creation, its length is fixed.

To declare and set aside memory box: double [ ] data = new double [10]; Using an initializer list: double [ ] data={76, 45, 67, 90, 98, 88, 65, 67, 88, 93}; Your Lab10 Part 2 requirements: Step 1: right click on Lab10 folder on the left column, select “New” and then “class” with a main() method (mark public static void main), name the class as “ArrayRandomNumb”. Then click on “Finish”. (1). Write a loop that fills an array called data with 20 random integer numbers between 1 and 200; (2). Display the 20 random numbers from the array called data like below (attention: you should see the different results each time when you run your program due to the random numbers): The 20 random numbers between 1 and 200 are: 24 106 144 47 79 32 171 131 131 25 37 128 12 185 102 185 152 132 193 123 (3). By using “Passing arrays to methods”, you need to create a method called findAverage( ) to calculate the average of these 20 numbers (please see the hint below): public static void main(String[] args) { … }//close main method public static double findAverage(int [] iniData){ //Start findAverage method …

}// close findAverage method

(4). By using “Passing arrays to methods”, you need to create a method called findMaxNumber() to find out the maximum number of these 20 numbers;

(5). By using “Passing arrays to methods”, you need to create a method called findMinNumber() to find out the Minimum number of these 20 numbers;

Step 2: when you complete your Lab10 coding and click run button to test your program, you should see the “similar” the output (attention: you should see the different results each time when you run your program due to the random numbers).

The 20 random numbers between 1 and 200 are: 103 186 42 197 146 54 103 8 63 180 63 156 2 10 165 51 165 130 164 173 The average of 20 numbers is 108.0 The maximum number is 197 The minimum number is 2

Step 3: Show your Lab10 result to your Lab instructor to get your credit.

Thanks!

Dr. Zhang

Recommended publications