user enters number between 1 and 10, the number they enter is the size of array,
ID: 3584355 • Letter: U
Question
user enters number between 1 and 10, the number they enter is the size of array, ex they enter 6 the array size becomes 6
- thenn program should print array of that size, so if they enter 6,the program prints array of size 6, where all integers should be zero.
-then the program should promt user to enter numbers to be places in array,
Proper outputs
Explanation / Answer
Consider the following interaction (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average. We need to be able to read each input value twice: once to compute the average (a cumulative sum) and again to count how many were above average. We could read each value into a variable, but we don't know how many days are needed until the program runs, so we don't know how many variables to declare. We need a way to declare many variables in one step and then be able to store and access their values. Arrays An array is an object that stores many values of the same type. An array element is one value in an array. An array index is an integer indicating a position in an array. Like Strings, arrays use zero-based indexing, that is, array indexes start with 0. The following displays the indexes and values in an array with 10 elements of type int. index 0 1 2 3 4 5 6 7 8 9 value 12 49 -2 26 5 17 -6 84 72 3 Array declaration The syntax for declaring an array is: type[ ] variable = new type[length]; Note that we need to specify a type (such as double or String), a variable name, and the length (the number of elements). For example: int[ ] numbers = new int[10]; // an array of 10 ints would assign the following array to the variable numbers. index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0 0 0 0 0 0 Each element in an array is initialized to zero, or whatever is considered "equivalent" to zero for the data type (false for booleans and null for Strings). Storing Values and Accessing Elements The syntax for storing a value in an array element is: variable[index] = expression; For example: numbers[0] = 27; numbers[3] = -6; would change the numbers array to: index 0 1 2 3 4 5 6 7 8 9 value 27 0 0 -6 0 0 0 0 0 0 The syntax for accessing an array element is: variable[index] where the index can be any expression that results in an int. For example: System.out.println(numbers[0]); if (numbers[3] > 0) { System.out.println(numbers[3] + " is positive"); } else { System.out.println(numbers[3] + " is not positive"); } Activity: Write a program that declares a double or String array of length 4, prints the values, assigns a value to each element, and prints the values again. Arrays and Loops We can use a variable as the index in an array expression. If we use a for loop to count from 0 to the highest index, then we can process each element of an array. For example, the following code would sum the elements in the numbers array. int sum = 0; for (int i = 0; i < 10; i++) { sum += numbers[i]; } We start at 0 because indexes start at 0. We end just before 10 because 10 is the length of our numbers array, and the last index is one less than the length of the array. [Arrays provide many opportunities for off-by-one errors because of the way indexes work.] If we changed the numbers array to have a different number of elements, this code would no longer work. Fortunately, Java provides a easy way to obtain the length of an array, by appending .length after the array variable, for example: int sum = 0; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.