Assignment 07- Arrays.docx
Assignment 07- Arrays.docx
Arrays
14 140
Coding Tasks
1. Write a Java program that will take N integer numbers from the user and create an
array of length N.
a. Print the elements of the array with their indices.
b. Take another integer input from the user, resize the array by length 1, and add the
new integer value to the array. Print the resized array.
2. You are given an integer array with duplicate values. Write a Java program to update
the array by replacing the duplicate values of the array with zero. Then print the updated
array. [Your code should work for any given integer array]
3. Write a Java program that asks the user for the length of an array and then creates an
integer array of that length by taking inputs from the user. Then,
A. Reverse the array by creating a new array of the same length and print it. (Out-of
Place)
B. Reverse the array without creating any new arrays and print it. (In-Place)
4. Take an integer N input from the user and create an integer array of N numbers by
taking inputs from the user. Then, print the array. Next, modify the array by changing
the positive numbers by 1 and the negative numbers by 0. If the element is zero, then it
will be unchanged. Lastly, print the modified array.
5. Write a Java program that will take N integer numbers from the user and create an
array of length N. Take another number from the user and print the index of the number
where it is found first. If not found then print ‘Element not found’.
Note: Think about how to apply the concept of flag and break in this task.
6. Write a Java program that asks the user for the length of an array then creates a double
data-type array of that length by taking inputs from the user.
Then do the following:
a. Show the maximum element and its index from the array.
b. Show the minimum element and its index from the array.
c. Show the summation of all the elements from the array.
d. Show the average of all the elements from the array.
Enter the length of the array: 5 Maximum element 344.0 found at index 2
Enter a number: 7.5 Minimum element -31.2 found at index 1
Enter a number: -31.2 Summation: 517.8
Enter a number: 344.0 Average: 103.56
Enter a number: 97.1
Enter a number: 100.4
7. You are given an integer array. You need to create a new array that will contain only
the unique elements of the given array. Finally, print the new array.
10. You are given two arrays of the same length. The first array contains the marks of the
students and the second array contains the name of the students. You need to sort the
marks array in ascending order while maintaining the corresponding names of the
students in the names array aligned with their respective marks. Use the Bubble sort
technique to solve this problem. [Your code should work for any given arrays]
11. Trace the following code, create a tracing table, and write the outputs.
1 class TraceA{
2 public static void main(String args[]){
3 int [] myArray = new int[10];
4 int index1 = 0, index2 =0;
5 while (index1 < 10){
6 myArray[index1] = index1 + 3;
7 index2 = 1;
8 while (index2 < index1 ){
9 myArray[index1] = myArray[index1] + myArray[index2] - index1;
10 index2 = index2 + 1;
11 }
12 System.out.println(myArray[index1]);
13 index1 = index1 + 1;
14 }
15 }
16 }
12. Trace the following code, create a tracing table, and write the outputs.
1 class TraceB{
2 public static void main(String args[]) {
3 int [] myArray = new int[5];
4 int [] b;
5 int idx1 = 0, idx2 = 0;
6 b = myArray;
7 while (idx1 < 5){
8 myArray[idx1] = idx1 + 11;
9 idx2 = 1;
10 while (idx2 < idx1 ){
11 myArray[idx1] = b[idx2 - 1] + myArray[idx2] - idx1;
12 idx2 = idx2 + 1;
13 }
14 System.out.println(myArray[idx2]);
15 idx1 = idx1 + 1;
16 }
17 }
18 }
13. Trace the following code, create a tracing table, and write the outputs.
1 class TraceC {
2 public static void main(String args[]){
3 int [] myArray = new int[10];
4 int [] b;
5 int index1 = 0, index2 =0;
6 index1 = 0;
7 b = myArray;
8 while (index1 < 10){
9 myArray[index1] = index1 + 4;
10 index2 = 1;
11 while (index2 < index1 ){
12 myArray[index1] = b[index1] + myArray[index2] - index1;
13 index2 = index2 + 1;
14 }
15 System.out.println(myArray[index1]);
16 index1 = index1 + 1;
17 }
18 }
19 }
14. Trace the following code, create a tracing table, and write the outputs.
1 class TraceD{
2 public static void main(String args[]){
3 int[] arr1 = {3,2,0,1,5,6,7};
4 int[] arr2 = {30,20,40,11,55,-34,100};
5 int a1 = 0, a2 = 0;
6 while (a1<arr1.length-1){
7 arr2[a1] = arr1[a2]+ a1 - arr2[a2];
8 a2 = 1;
9 while (a2 < a1 ){
10 arr2[a1] = arr2[a1] + arr1[a2] - a2;
11 a2 = a2 + 1;
12 }
13 System.out.println(arr2[a1]);
14 a1 = a1 + 1;
15 }
16 System.out.println(arr2[arr1[a2]]);
17 }
18 }