Assalamu'alaikum and hello..
I'm going to share to all of you my Lab 9 on the topic array. From this assignment, I need to declare, initialize and understand the process of single-dimensional array.
Question 1
a)
double[
] value = new double[10];
b)
value[value.length
– 1] = 5.5;
c)
System.out.println(value[0]
+ value[1]);
d)
double
sum = 0;
for (int i = 0;
i < value.length; i++)
{
sum +=
value[i];
}
e)
double
min = 0;
for (int i = 0;
i < value.length; i++)
{
if ( min >
value[i] )
min = value[i];
}
f)
System.out.println(value[(int)(Math.random()
* value.length)] );
g)
double[
] value = {3.5, 5.5, 4.2, 5.6};
Question 2
Line 5: Declaration is wrong, double[100] should be double[ ] . The array needs to be
create before use it, e.g new double[100] .
Line 7: r.length() should be r.length
Line 9: r(i) should be r[i]
Line 9: Math.random should be Math.random()
Question 3
Write a program that reads in n integers and sort them in increasing order.
Question 4
Write a program that randomly generates n integers between 0 and 10 and counts the occurrence
of each.
Question 5
Write a program that reads an unspecified number of scores and determines how many scores are
above or equal to the average, and how many scores are below the average. Enter a negative
number to signify the end of the input. Assume maximum score is 100.
Question 6
Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
Write a test program that prompts the user to enter 10 double values, invokes this method, then
displays the average value.
Question 7
Write a method that finds the largest element in an array of double values using the following header:
public static double max(double[] array)
Write a test program that prompts the user to enter ten numbers, invokes this method to return the maximum value, and displays the maximum value.
Question 8
Write a program that prompts the user to enter the number of students, the students’ names, and their scores and prints student names in decreasing order of their scores. Assume the name is a string without spaces, use the Scanner’s next() method to read a name.
That’s all from me. Remain positive though it is impossible. Have a nice day ^_^
Nurazreen binti Mohd Nor (182433)
SSK3100 (Group 15)