0% found this document useful (0 votes)
6 views3 pages

DSA - Lab Practical 1 - Arrays

The document outlines a lab practical assignment for a Data Structures & Algorithms course, requiring individual work on Java programming tasks. It includes implementing and manipulating different types of arrays: an integer array with random values, a string array of user-input names sorted in both alphabetical and reverse order, and a two-dimensional array with calculations for sums. The document emphasizes the importance of originality in submissions to avoid penalties for plagiarism.

Uploaded by

tvpp7mkj8t
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)
6 views3 pages

DSA - Lab Practical 1 - Arrays

The document outlines a lab practical assignment for a Data Structures & Algorithms course, requiring individual work on Java programming tasks. It includes implementing and manipulating different types of arrays: an integer array with random values, a string array of user-input names sorted in both alphabetical and reverse order, and a two-dimensional array with calculations for sums. The document emphasizes the importance of originality in submissions to avoid penalties for plagiarism.

Uploaded by

tvpp7mkj8t
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/ 3

145195 – Hassan, Zeyn ul Abideen

School of Computing and Engineering Sciences


BBT 1201: Data Structures & Algorithms Lab Practical 1 - Arrays

Instructions

1. This is individual work.


2. Plagiarized work will only get 50% of the marks scored.
3. Answer all the questions in this document.

Questions

1. Implement an int array with 15 elements, using java programming language:


a. Initialize all elements with random integers using a loop. Print the results.
b. Search through the array to check if the number 15 is contained in the array. Print the
results.
c. Copy-paste or screenshot your solution below.
import java.util.Random;

public class Main {


public static void main(String[] args) {
int[] integerArray = new int[15];
// this initializes an array with 15 elements
Random randomNumberGenerator = new Random();
//this statement generates random numbers

for (int i = 0; i < integerArray.length; i++) {


integerArray[i] = randomNumberGenerator.nextInt(100);
//generates random integers from 0-100
}

DSA – Lab Practical 1 Wednesday, May 3, 2023


145195 – Hassan, Zeyn ul Abideen

for (int i = 0; i < integerArray.length; i++) {


System.out.println("Element " + i + " is: " + integerArray[i]);
}

// checks if the number 15 is in the array


boolean contains15 = false;
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i] == 15) {
contains15 = true;
break;
}
}
if (contains15) {
System.out.println("The array contains number 15.");
} else {
System.out.println("The array does not contain number 15.");
}
}
}
2. Implement a String array with 10 elements, using java programming language.
a. Initialize all elements to Names of people that a user will input.
b. Sort the names in array in alphabetical order and print the results.
c. Sort the name in reverse alphabetical order and print the results.
d. Copy-paste or screenshot your solution below.
import java.util.Scanner;
import java.util.Arrays;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] namesArray = new String[10];

for (int i = 0; i < namesArray.length; i++) {


System.out.print("Please enter a name: ");
namesArray[i] = scanner.nextLine();
}

Arrays.sort(namesArray);

DSA – Lab Practical 1 Wednesday, May 3, 2023


145195 – Hassan, Zeyn ul Abideen

System.out.println("Sorted names in alphabetical order are:");


System.out.println(Arrays.toString(namesArray));

String[] reverseNamesArray = new String[namesArray.length];


for (int i = 0; i < namesArray.length; i++) {
reverseNamesArray[i] = namesArray[namesArray.length - i - 1];
}
System.out.println("Sorted names in reverse alphabetical order are:");
System.out.println(Arrays.toString(reverseNamesArray));

scanner.close();
}
}
3. Implement a 2-dimensional array as; int numbers [9] [3] using java programming language.
a. Implement another array that will store the sum of each element in the numbers array.
b. Print out the element with the largest sum.

public class Main {


public static void main(String[] args) {
int[][] numbers = new int[9][3];
int[] sums = new int[9];
for (int i = 0; i < 9; i++)

DSA – Lab Practical 1 Wednesday, May 3, 2023

You might also like