Question

In: Computer Science

how do you add two matrices linked list in java? (am using linked list because 2D...

how do you add two matrices linked list in java?
(am using linked list because 2D arrays are not allowed.)
ex
[1st matrix]
1 3 2
4 2 1
3 2 4
+
[2nd matrix]
3 2 3
2 1 4
5 2 3
=
[3rd matrix]

4 5 5
6 3 5
8 4 7

Solutions

Expert Solution

Hi, Please find the solution and rate the answer:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

class Scratch {
    public static void main(String[] args) {
        List<Integer> matrix1 = new LinkedList<>();
        List<Integer> matrix2 = new LinkedList<>();
        Random x = new Random();
        for (int i = 0; i < 9; i++) {
            matrix1.add(x.nextInt(100));
            matrix2.add(x.nextInt(100));
        }

        for (int i = 0; i < 9; i++) {
            System.out.print(matrix1.get(i)+"\t");

        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < 9; i++) {
            System.out.print(matrix1.get(i)+" \t");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();

        }
        System.out.println();
        for (int i = 0; i < 9; i++) {
            System.out.print(matrix2.get(i) + " \t");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();
        }
        System.out.println();
        System.out.println("_________________________________");
        List<Integer> mat = add(matrix1,matrix2);
        for (int i = 0; i < mat.size(); i++) {
            System.out.print(mat.get(i)+", ");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();
        }

        System.out.println();
        System.out.println();

    }
    static List<Integer> add(List<Integer> mat1,List<Integer> mat2){
        int size=mat1.size();
        if(mat1.size()!=mat2.size())
            return null;
        List<Integer> mat = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            mat.add(mat1.get(i)+mat2.get(i));
        }
        return mat;

    }
}

Output: Addition of 2 arrays in last line


Related Solutions

I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
In JAVA How can you multiply two matrices that are different lengths. the answer should be...
In JAVA How can you multiply two matrices that are different lengths. the answer should be in 3d array so [][][] how to multiply a 3d array with a 2d array and the result is in 3d array for example: double[][][] nums1 = { { {0.0, 0.1, 2.0}, {3.0,4.0,5.0} }, { {6.0,7.0,8.0}, {9.0,10.0,11.0} } } int [][] nums2 = { {1,2,3}, {4,5,6}, {7,8,9}} and the results should be in [][][] <-- in this dimension int[][][] answer = { {  {18, 21,24},...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene...
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene within one of the following categories: Satire or humor Promote a cause You are free to create whatever you choose but it must conform to the following guidelines.   Show evidence of at least four colors. Have a textual composition on the finished product. Imagery or images Scene composition of at least six (6) elements One of the following 1) Shadows or Glows. May be...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
How do you write an append function for a linked list that is a that has...
How do you write an append function for a linked list that is a that has a O(1) Big O notation in Python? The one given in class is O(n). He recommended using a variable to track the end the list. This is the code I have written so far. I don't think I'm properly defining the tail in the add possibly class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT