Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Matrix Arithmetic in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cosc550_lab1;

import java.util.Arrays;
import java.util.Random;

/**
 *
 * @author obobotette0 Obioku Obotette September 8, 2016
 *
 * Lab 1 Program 1: Write a program that randomly generate a one-dimensional
 * array, and implements quicksort using recursion. Display the original array,
 * and the new array. Program 2: Write a program that randomly generate two
 * two-dimensional arrays, and implements matrix multiplication using logical
 * loop – for example, for loop. Display the two original matrixes, and the new
 * matrix.
 */
public class COSC550_Lab1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //create variables
        Random num = new Random();
        int array[] = new int[num.nextInt(50)];
        int i = 0;

        //loop to populate array
        for (i = 0; i < array.length; i++) {
            array[i] = num.nextInt(100);
        }

        //Print contents of array
        System.out.println(Arrays.toString(array));

        //Perform sort
        quicksort(array, 0, i - 1);

        //Print contents of array
        System.out.println(Arrays.toString(array));

        //Create the dimensions
        int x1 = num.nextInt(10);
        int y1 = num.nextInt(10);
     
        //Swap row & column values
        int matrix1[][] = new int[x1][y1];
        int matrix2[][] = new int[y1][x1];

        //double for loops to create matrix
        for (int n = 0; n < x1; n++) {
            for (int m = 0; m < y1; m++) {
                matrix1[n][m] = num.nextInt(100);
                matrix2[m][n] = num.nextInt(100);
            }
        }
        //Print matricies
        System.out.println("Matrix 1 dimensions are  X " + x1 + " Y " + y1 + "\nMatrix 1 contents are " + Arrays.deepToString(matrix1));
        System.out.println("Matrix 2 dimensions are  X " + y1 + " Y " + x1 + "\nMatrix 2 contents are " + Arrays.deepToString(matrix2));

        //Create matrix for result
        int result_matrix[][] = new int[x1][1];
     
        //Create variables
        int temp = 0;
        int result = 0;
     
        //Loop to produce matrix multiplication
        for (int n = 0; n < x1; n++) {
            for (int m = 0; m < y1; m++) {
                //Multiply row index by column index
                temp = matrix1[n][m] * matrix2[m][n];
                //Add results together
                result += temp;
            }
            //Place product in new array
            result_matrix[n][0] = result;
            //reset result
            result = 0;
        }

        //Print contents
        System.out.println("Result Matrix dimensions are  X " + x1 + " Y " + '1' + "\nResult Matrix contents are " + Arrays.deepToString(result_matrix));

    }

    //quicksort method
    public static void quicksort(int[] point, int low, int high) {
        //temp variable for swap
        int temp;

        //range variables
        int l = low;
        int h = high;

        //create midpoint and middle node
        int mid = (low + high) / 2;
        int median = point[mid];
        while (l <= h) {
            while (point[l] < median) {
                l++;
            }

            while (point[h] > median) {
                h--;
            }

            if (l <= h) {
                temp = point[l];
                point[l] = point[h];
                point[h] = temp;
                l++;
                h--;

            }
        }
        if (low < h) {
            quicksort(point, low, h);
        }
        if (l < high) {
            quicksort(point, l, high);
        }

    }

}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!