Computer Applications
Write Java statements for the following:
i. Create an array to hold 15 double values.
ii. Assign the value 10.5 to the last element in the array.
iii. Display the sum of the first and the last element.
iv. Write a loop that computes the sum of all elements in the array.
Java Arrays
15 Likes
Answer
i. Create an array to hold 15 double values.
double arr[] = new double[15];
ii. Assign the value 10.5 to the last element in the array.
arr[14] = 10.5;
iii. Display the sum of the first and the last element.
double r = arr[0] + arr[14];
System.out.println("Result = " + r);
iv. Write a loop that computes the sum of all elements in the array.
double sum = 0;
for (int i = 0; i < 15; i++) {
sum += arr[i];
}
System.out.println("Sum = " + sum);
Answered By
7 Likes
Related Questions
Given the following declarations:
final int SIZE = 20; char[] name = new char[SIZE];
i. Write an assignment statement that stores 'D' into the first element of the array name.
ii. Write an output statement that prints the value of the tenth element of the array name.
iii. Write a for statement that fills the array name with spaces.
Write a program that reads ten integers and displays them in the reverse order in which they were read.
What happens in Java if you try to access an element that is outside the bounds of the array?
Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".
Sample Input:n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 1982 1987 1993 1996 1999 2003 2006 2007 2009 2010