Tutorial 5
Tutorial 5
Tutorial 5
An array is a data structure that can hold more than one value at a time.
It stores a fixed-size sequential collection of elements of the same type.
Each element can be accessed by its index.
Arrays are commonly used in programming to store and organize data efficiently.
3. Declare and initialize an array of strings named students to store the following
names:Luth, Wafiy, Ahmed, Waiz, and Sofea Izzara.
string[ ]names=new string[5]
names={.....};
6. The following code assigns the value 1 to be the first element of the array. True or false.
int [] intArr;
intArr[1] = 1;
Section B: Hand Tracing
1. The following declaration represents the age of all members of Ahmed's family.
int [] age = {42, 5, 7, 76, 38, 2, 48, 9};
2. Trace the following code segment to determine the value stored in the arr.
a. int [] arr = new int [8]; 0-7的数组
for (int i = 0; i < 8; i++) {
arr[i] = i+1;
}
0 1 2 3 4 5 6 7
orr 1 2 3 4 5 6 7 8
b. int [] arr = new int [8];
for (int i = 0; i < 8; i++) {
arr[i] = i * i;
}
else
arr[i] = i - 5;
}
d. int N = 10;
int [] arr = new int [N];
arr[0] = 0;
arr[1] = 1;
for (int i = 2; i < N; i++)
arr[i] = arr[i-1] + arr[i-2];
0 1 2 3 4
5 6 7 8 9
arr
3. Given the following program.
1 public class AnalyzeNumbers {
2 public static void main(String[] args) {
3 java.util.Scanner input = new java.util.Scanner(System.in);
4 System.out.print("Enter the number of items: ");
5 int n = input.nextInt();
6 double[] numbers = new double[n];
7 double sum = 0;
8
9 System.out.print("Enter the numbers: ");for
10 (int i = 0; i < n; i++) {
11 numbers[i] = input.nextDouble();sum
12 += numbers[i];
13 }
14
15 double average = sum / n;
16
17 int count = 0; // The numbers of elements above averagefor
18 (int i = 0; i < n; i++)
19 if (numbers[i] > average)
20 count++;
21
22
23 System.out.println("Average is " + average);
24 System.out.println("Number of elements above the average is "
25 + count);
26 }
}
Briefly explain what the program does and what the out of the program if n = 5
and input for n are 1,2,3,4 and 5.
0 1 2 3 4
arr1.0 2.0 3.0 4.0 5.0
Section C: Write Code Segments
1. Write code segments for the following task. Assume that the task will be executed in
thegiven order.
a. Write Java statement that declare and create an array myList to hold 50 integers.
b. Write code segments that reads 50 integers and store in array myList.
for int i=0 i<50;i++
mylist[i]=sc.nextin()
d. Write code segments to find the largest element in the array myList.
e. Write code segments to find the index of the largest element in the array myList.
f. Write code segments to copy all elements of array myList to array otherList.
3. Write aprogram that reads 10 integers and displays them in reverse order. For example, if
the data are 3 4 2 8 5 1 9 12 10 7, then the output is 7 10 12 9 1 5 8 2 4 3.
4. Write a programn that reads 10 number of scores and determines how many scores are
above or equal to the average and how many scores are below the average.
5. Write a program that reads two lists of 10 integers and compare whether the two are
strictly identical.
6. Write a program that reads 10 integers, remove the duplicate values in the array and display
the result. For example, if the input is 1 2 3 2 1 6 3 4 5 2, then the output will be 1 23 6 4 5.