Homework LAB8

 avatar
unknown
java
a year ago
7.3 kB
61
Indexable
//h1
import java.util.Scanner;

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

        System.out.print("Enter the length of the array: ");
        int N = sc.nextInt();
        double[] doubleArray = new double[N];

        double sum = 0;
        double average = 0;

        for (int i = 0; i < doubleArray.length; i++) {
            System.out.print("Enter a number: ");
            double num1 = sc.nextDouble();
            doubleArray[i] = num1;
            sum += num1;
        }

        double max = doubleArray[0];
        double min = doubleArray[0];
        int indexMax = 0;
        int indexMin = 0;
        for (int i = 0; i < doubleArray.length; i++) {
            if (max < doubleArray[i]) {
                max = doubleArray[i];
                indexMax = i;
            }
            else if (min > doubleArray[i]) {
                min = doubleArray[i];
                indexMin = i;
            }

        }



        System.out.println("Maximum element " + max + " found at index " + indexMax);
        System.out.println("Minimum element " + min + " found at index " + indexMin);
        System.out.println(sum);
        System.out.println(sum / doubleArray.length);
    }

}


//h2
public class HomeworkTwo {
    public static void main(String[] args) {
        int[] arr = {23, 100, 23, 56, 100};
        int[] uniqueArray = new int[arr.length];
        int count = 0;

        System.out.println("Input Array: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");

            boolean isFound = false;
            for (int j = 0; j < i; j++) {
                if (arr[i] == arr[j]) {
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                uniqueArray[count++] = arr[i];
            }
        }

        int[] finalArray = new int[count];

        System.out.println();
        System.out.println("New Array: ");
        for (int i = 0; i < finalArray.length; i++) {
            finalArray[i] = uniqueArray[i];
            System.out.print(finalArray[i] + " ");
        }
    }
}



//h3
import java.util.Scanner;

public class HomeworkThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter the length of the array 1: ");
        int N = sc.nextInt();
        int[] arr1 = new int[N];

        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = sc.nextInt();
        }

        System.out.print("Please enter the length of the array 2: ");
        int M = sc.nextInt();
        int[] arr2 = new int[M];

        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = sc.nextInt();
        }

        boolean isSubset = true;

        for (int i = 0; i < arr2.length; i++) {
            boolean found = false;
            for (int j = 0; j < arr1.length; j++) {
                if (arr2[i] == arr1[j]) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                isSubset = false;
                break;
            }
        }

        if (isSubset) {
            System.out.println("Array 2 is a subset of Array 1");
        } else {
            System.out.println("Array 2 is not a subset of Array 1");
        }
    }
}



//h4

import java.util.Scanner;

public class HomeworkFour {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter the length of the array: ");
        int N = sc.nextInt();
        int[] arr1 = new int[N];
        int firstIndex = 0;
        int secondIndex = 0;

        System.out.println("Please enter the elements of the array:");
        for (int i = 0; i < N; i++) {
            arr1[i] = sc.nextInt();
        }

        System.out.print("Please enter the target value: ");
        int target = sc.nextInt();

        boolean found = false;

        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                if (arr1[i] + arr1[j] == target) {
                    firstIndex = i;
                    secondIndex = j;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }

        if (found) {
            System.out.println("Elements need to be added " + arr1[firstIndex] + " " + arr1[secondIndex]);
            System.out.println("Indexes of the elements: " + firstIndex + " and " + secondIndex);
        } else {
            System.out.println("Target value not found.");
        }
    }
}



//h5

import java.util.Scanner;

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

        System.out.print("Enter the length of the array: ");
        int N = sc.nextInt();
        int[] arr = new int[N];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("Original array: ");
        for (int i = 0; i < arr.length;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();


        for (int i = 0; i < N - 1; i++) {
            int maxIndex = i;
            for (int j = i + 1; j < N; j++) {
                if (arr[j] > arr[maxIndex]) {
                    maxIndex = j;
                }
            }
            int temp = arr[maxIndex];
            arr[maxIndex] = arr[i];
            arr[i] = temp;
        }

        System.out.println("Sorted array: ");
        for (int i = 0; i < arr.length;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}




//h6
public class HomeworkSix {
    public static void main(String[] args) {
        int[] marks = {85, 90, 75, 44, 99};
        String[] names = {"Bob", "Alice", "Max", "Marry", "Rosy"};

        int n = marks.length;

        for (int i = 0; i < n - 1; i++) {
            boolean swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (marks[j] > marks[j + 1]) {

                    int tempMark = marks[j];
                    marks[j] = marks[j + 1];
                    marks[j + 1] = tempMark;

                    String tempName = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = tempName;
                    swapped = true;
                }
            }
            if (!swapped) {
                break;
            }
        }

        System.out.println("Sorted array: ");
        for (int i = 0; i < n; i++) {
            System.out.print(marks[i] + " ");
        }
        System.out.println();

        for (int i = 0; i < n; i++) {
            System.out.print(names[i] + " ");
        }
        System.out.println();
    }
}
Editor is loading...
Leave a Comment