[go: up one dir, main page]

0% found this document useful (0 votes)
12 views9 pages

Assignment 07- Arrays.docx

The document outlines a lab assignment for CSE110: Programming Language I, consisting of 14 coding tasks related to arrays in Java. Each task requires students to implement specific functionalities such as creating arrays, modifying elements, reversing arrays, and handling user input. Sample inputs and outputs are provided for each task to guide students in their implementations.

Uploaded by

Abdur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Assignment 07- Arrays.docx

The document outlines a lab assignment for CSE110: Programming Language I, consisting of 14 coding tasks related to arrays in Java. Each task requires students to implement specific functionalities such as creating arrays, modifying elements, reversing arrays, and handling user input. Sample inputs and outputs are provided for each task to guide students in their implementations.

Uploaded by

Abdur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Assignment 7

Arrays

CSE110: Programming Language I

No of Tasks Points to Score

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.

Sample Input Sample Output

N=5 The elements of the array are:


Enter a number: 11 0: 11
Enter a number: 22 1: 22
Enter a number: 33 2: 33
Enter a number: 44 3: 44
Enter a number: 55 4: 55
Enter another number: 101 After resizing the array:
11 22 33 44 55 101

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]

Given Array Sample Output

int arr [] = {9, -5, 7, 9, -5, 5, 7}; Before removing duplicates:


9 -5 7 9 -5 5 7
After replacing duplicates with 0:
9 -5 7 0 0 5 0

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)

Sample Input Sample Output

Enter the length of the array: 5 Reversed using a new array:


Enter a number: 7 100 97 344 -31 7
Enter a number: -31
Enter a number: 344 Reversed the original array:
Enter a number: 97 100 97 344 -31 7
Enter a number: 100

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.

Sample Input Sample Output

N=4 Original array:


3 3 4 -2 1
4 After modifying:
-2 1101
1

N=3 Original array:


-4 -4 0 2
0 After modifying:
2 001

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.

Sample Input 1 Sample Input 2


N=7 N=5
Enter a number: 45 Enter a number: 4
Enter a number: 0 Enter a number: 99
Enter a number: 17 Enter a number: 23
Enter a number: 11 Enter a number: -67
Enter a number: -34 Enter a number: 34
Enter a number: -100 55
Enter a number: 17
17
Sample Output 1 Sample Output 2
17 is at index 2 Element not found

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.

Sample Input Sample Output

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.

Given Array Sample Output

int arr [] = {23,100,23,56,100}; Input array:


23 100 23 56 100
New array:
23 100 56

int arr [] = {-5,10,-7,-5}; Input array:


-5 10 -7 -5
New array:
-5 10 -7
8. Write a Java program that will take input of two arrays and elements from the user and
check whether the second array is a subset of the first array. A subset is a set that contains
only elements found in the original set.

Sample Input - 1 Sample Output - 1

Please enter the length of array 1: 5 Array 2 is a subset of Array 1.


Please enter the elements of the arr1:
5
3
2
72
8
Please enter the length of array 2: 3
Please enter the elements of the arr2:
5
3
72

Sample Input - 2 Sample Output - 2

Please enter the length of array 1: 5 Array 2 is not a subset of Array 1.


Please enter the elements of the arr1:
7
2
33
1
6
Please enter the length of array 2: 3
Please enter the elements of the arr2:
1
8
2
9. Take an integer N input from the user and create an array of length N by taking the
elements as user input. Then, print the array. Next, sort the array in descending order
using Selection Sort technique. Lastly, print the sorted array.

Sample Input Sample Output

N=6 Original Array:


Enter a number: 3 361285
Enter a number: 6 Sorted Array:
Enter a number: 1 865321
Enter a number: 2
Enter a number: 8
Enter a number: 5

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]

Given Array 1 Sample Output 1

int [] marks = {85, 90, 75, 44, 99}; Sorted Array:


String [] names = {"Bob", "Alice", "Max", 44 75 85 90 99
"Marry", "Rosy"}; Marry Max Bob Alice Rosy

Given Array 2 Sample Output 2

int [] marks = {100, 47, 85, 94, 5, 50}; Sorted Array:


String [] names = {"Henry", "Mari", 5 47 50 85 94 100
"Herry", "Jack", "Lily", "Oliver"}; Lily Mari Oliver Herry Jack Henry
Tracing Tasks

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 }

You might also like