Array is a data structure that represents a collection of the same types of data.
Let's look at some example to understand array in more detail.
The question 1 is asked to write statement for array.
a.
Create an array
to hold 10 double values.
double [ ] values = new double [10];
b.
Assign the value
5.5 to the last element in the array.
values [values.length – 1] = 5.5;
c.
Display the sum
of the first two elements.
System.out.println (values [ 0 ] + values[ 1 ]);
d.
Write a loop
that computes the sum of all elements in the array.
double sum = 0;
for (int i = 0; i < values.length; i++)
sum
+= values [ i ];
e.
Write a loop
that finds the minimum element in the array.
double min = values [ 0 ];
for (int i = 0; i < values.length; i++)
if (min > values [ i ] )
min= values [ i ];
f.
Randomly
generate an index and display the element of this index in the array.
System.out.println (values [ (int) (Math.random ( )*numbers.length)
] );
g.
Use an array
initializer to create another array with the initial values 3.5, 5.5, 4.52 and
5.6.
double
[ ] values = {3.5, 5.5, 4.52, 5.6};
*This is how array created !!!!
The question 2 is to identify and fix the errors in the following code that attempts to assign random numbers to an array of 100 integers: public class Test {
public static void main(String[] args) {
double[100] r;
for ( int i = 0; i < r.length(); i++ ) {
r(i) = Math.random * 100;
} } *The way declare array is WRONG .
The question 3 is asked to write a program that reads in n integers and sort them in increasing order.
* The numbers are displayed in increasing order.
The question 4 is asked to 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.
*Negative number is used to end the program by using the loops.
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.
*Two method average with different datatype which are int and average lead to different answers.
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.
* method max is invoked.
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.
*The score for students is displayed in decreasing order.
Question 9 : Re-write the FindingRoot program from your previous lab to find roots for the equation
y = (x-4)2 – 9
This program should now be able to process n binary strings instead of just 4. The modified FindingRoot program should:
i. Put the n binary strings in an array called population.
ii. Sort the elements in population by their fitness values. The binary strings with the larger fitness values will be put in the beginning of population.
iii. Perform the crossover operations on the elements in population.
iv. Replace every element in population with the new binary strings.
v. Find the average fitness value of the elements.
vi. Changing the termination criteria in step (b). The loop terminates when the average fitness value for 20 iterations differs by only 0.1 or less.
It quite complicated, we will try to investigate and get the solution for this question.
In short, array is important in programming because :
- It is better and convenient way of storing the data of same datatype with same size.
- It allows us to store known number of elements in it.
That's all for today. Thank you very much ^^
LIM XUE TING
183615
SSK3100
(Group15)
Next blog: hennyabigail.blogspot.com (Henny
Abigailwillyen Sinjus)
No comments:
Post a Comment