Answer to Question #318451 in Java | JSP | JSF for lolo

Question #318451

1.      Write a Java program to find the maximum and minimum value of a 1-d array.

 

2.      Write a Java program without using a second array to reverse elements of each row of a 2-d array of integer values.

e.g.      1    2   3    4                                      4    3    2  1

               5    6   7    8          è                    8    7    6  5

             9   10  11 12                                    12  11  10  9

 

3.      Write a java program to print the Pascal’s Triangle using a 2-d array.

 

4.      Write a Java program to convert an array to an ArrayList, and an ArrayList to an array.

 

5.      Write a Java program to arrange the elements of a given array of integers where all negative integers appear before all the positive integers.



1
Expert's answer
2022-03-26T05:45:03-0400
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        // 1.
        int[] firstArray = {-26, 65, 41, 32, 98, 18};
        int maxNum = firstArray[0];
        for(int a: firstArray)
            if(maxNum < a)
                maxNum = a;

        System.out.println(maxNum);

        // 2.
        int[] secondArray = {5, 6, 7, 8};
        int buff;

        for (int i : secondArray) {
            System.out.print(i + " ");
        }
        System.out.print("  -   ");



        for(int i = 0; i < secondArray.length/2; i++){
            buff = secondArray[i];
            secondArray[i] = secondArray[secondArray.length-i-1];
            secondArray[secondArray.length-i-1] = buff;
        }
        for (int i : secondArray) {
            System.out.print(i + ", ");
        }
        System.out.println("");



        // 3.

        int n = 8;
        int[][] arr = new int[n][];
        for (int i = 0; i < n; i++) {
            arr[i] = new int[n - i];
            for (int j = 0; j < n - i; j++) {
                if (i == 0 || j == 0) {
                    arr[i][j] = 1;
                } else {
                    arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
                }
            }
        }

        for (int[] row : arr) {
            for (int el : row)
                System.out.printf(" %2d", el);
            System.out.println();
        }


        // 4.

        String[] names = {"Igor", "Dima", "Vanya", "Kiril", "Alex", "Roma"};
        String[] names2 = new String[names.length];
        List<String> list = new ArrayList<>();

        for (String s : names)
            list.add(s);

        for (int i = 0; i < list.size(); i++)
            names2[i]= list.get(i);


        // 5.

        int count = 0, buuf;

        int[] fiveArray = {12, 56, 42, -16, 1, -21, 23, -2};

        for(int i = 0; i < fiveArray.length; i++){
            if(fiveArray[i] < 0){
                buuf = fiveArray[count];
                fiveArray[count] = fiveArray[i];
                fiveArray[i] = buuf;
                count++;
            }
        }
        for (int i : fiveArray) {
            System.out.print(i + ", ");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS