Tutorial 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Tutorial 5

Array and Array Processing I


Section A: Self-Test
1. What is an array?

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.

2. Write a Java statement to declare an array that can store 10 integers.

int[ ] arr=new int[10]

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={.....};

4. Print the students array (refer Q3)

5. out of index error


Exception ________________________ will be thrown, if accessing an index that is bigger
than the size of the array.

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};

Answer the following questions:


a. What is the size of the above array?

b. What does age[1] , age[3] and age[8] represent?

5 76 out of index error

c. What is the index of the oldest in the Ahmed’s family?

d. What is the index of the youngest in the Ahmed’s family?

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;
}

c. int [] arr = new int [8];


for (int i = 0; i < 8; i++) {
if (i > 5)
arr[i] = i * 2 + 1;

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.

int[ ]mylist=new int[50]

b. Write code segments that reads 50 integers and store in array myList.
for int i=0 i<50;i++
mylist[i]=sc.nextin()

c. Write code segments to sum all elements in the array myList.

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.

for int i=0 i<50;i++


secondlist[i]
2. Write a program that reads 10 integers between 1 and 10 and counts the occurrences of
each.

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.

You might also like