Friday, January 5, 2018

Multidimensional Arrays

Challenge 1: Write a Java program that fills a 3x4 matrix with random numbers in [0, 9] and calculates the sum of the elements in each row. For example:

Use a two-dimensional array to store the matrix elements.





class lab9_01 {

    public static void main(String[] args) {
        Random rand = new Random();
        int[][] randomIntegers = new int[3][4];
        int counter = 0;
        int firstRow = 0;
        int secondRow = 0;
        int thirdRow = 0;

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

            for (int j = 0; j < randomIntegers[0].length; j++) {

                randomIntegers[i][j] =rand.nextInt(10);
                System.out.print(randomIntegers[i][j] + "");
                if (i == 0) {

                    firstRow += randomIntegers[i][j];
                    
                } else if (i == 1) {
                    secondRow += randomIntegers[i][j];
                } else {
                    thirdRow += randomIntegers[i][j];
                }
            }
            
            System.out.print("");
            if (counter==0){
            System.out.println(" Sum first row: " + firstRow);
            counter++;}
            else if(counter ==1)
            {
            System.out.println(" Sum second row: " + secondRow);
            counter++;
            }
            else if(counter==2)
            {
            System.out.println(" Sum third row: " + thirdRow);
            }
        }
    }

}

Challenge 2: Write a Java program that adds two 4x4 matrices and prints the resulting matrix. For example: 


Use two-dimensional arrays to store the elements of the matrices. Your program should initialize the matrices in the declaration line.

public class lab9_02 {

    public static void main(String[] args) {

        int a[][] = {{0, 1,2,7},
                     {6,0,3,1},
                     {3,1,2,0},
                     {5,1,4,4},};
        

        int b[][] = {{1,1,0,2},
                     {3,4,5,0},
                     {3,2,6,7},
                     {4,0,3,2},};

        matrixAddition addMatrices = new matrixAddition(a, b);
    }
}
class matrixAddition {

    int add[][] = new int[4][4];

   public matrixAddition(int a[][], int b[][]) {
        for (int i = 0; i <= a.length - 1; i++) {
            for (int j = 0; j <= b[i].length - 1; j++) {
                add[i][j] = a[i][j] + b[i][j];
                System.out.print(add[i][j]+" ");
            }
            System.out.println();
        }
    }

}





Challenge 3: Write a program that populates a java.util.ArrayList variable with 10 random integers in [1, 20], asks the user to provide a value, and counts how many numbers are equal to the value entered by the user. 

class lab9_03 {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int randomNumbers, number, count = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        Random randomNumber = new Random();
        for (int i = 0; i < 10; i++) {
            randomNumbers = randomNumber.nextInt(20) + 1;
            list.add(randomNumbers);
        }
        System.out.print("Please enter a number: ");
        number = in.nextInt();
        for (int i = 0; i < 10; i++) {
            if (number == list.get(i)) {
                count++;
            }
        }
        System.out.println("You have" + count + " match/matches. ArrayList\n" + list);
    }

}

Challenge 4: Write a program that populates a java.util.ArrayList variable with 20 random integers in [1, 100] and prints the location of the smallest (the minimum) integer. 
import java.util.*;

class lab9_04 {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int randomNumbers, temp = 0, loc = 0, number, counter = 0;

        ArrayList<Integer> list = new ArrayList<Integer>();

        Random randomNumber = new Random();

        //randomNumbers = randomNumber.nextInt(100)+1;
        temp = randomNumber.nextInt(100) + 1;

        for (int i = 0; i < 20; i++) {

            randomNumbers = randomNumber.nextInt(100) + 1;

            list.add(randomNumbers);

            if (temp > list.get(i)) {

                temp = list.get(i);

                loc = i;
            }
            System.out.println(list.get(i) + "");

        }
        System.out.println("The minimun number is: " + temp + " and is at location: " + loc);




    }
}

No comments:

Post a Comment