0% found this document useful (0 votes)
3 views

Data_Structures_Assignment

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

Data_Structures_Assignment

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

(AI-2001) Data Structures and Algorithms

ASSESSMENT ACTIVITY: Assignment -01


Semester: Semester -III

Final Dated:

OBE Target: CLO-2, GA-3, C-3

Weight of Marks:

Student Name:

Student ID:

Teacher: Abdul Salam

Score:

Question 1:
Construct a Java Code that finds a given number from an array. The elements in the array
would be entered by the user.

Solution:

import java.util.Scanner;

public class FindNumber {


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

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();
int[] array = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}

System.out.print("Enter the number to find: ");


int target = scanner.nextInt();

boolean found = false;


for (int i = 0; i < n; i++) {
if (array[i] == target) {
found = true;
System.out.println("Number " + target + " found at index " + i);
break;
}
}

if (!found) {
System.out.println("Number " + target + " not found in the array.");
}

scanner.close();
}
}

Question 2:
Construct a Java Code that finds minimum and maximum number from an array and display
numbers and its memory address.

Solution:

import java.util.Scanner;

public class MinMaxFinder {


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

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();
int[] array = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}

int min = array[0], max = array[0];


int minIndex = 0, maxIndex = 0;

for (int i = 1; i < n; i++) {


if (array[i] < min) {
min = array[i];
minIndex = i;
}
if (array[i] > max) {
max = array[i];
maxIndex = i;
}
}

System.out.println("Minimum number: " + min + " at memory address (index): " +


minIndex);
System.out.println("Maximum number: " + max + " at memory address (index): " +
maxIndex);

scanner.close();
}
}

You might also like