The goal in this assignment is to perform various matrix
multiplications. Input matrices A and B are read from a file.
Output matrices A*A, A*B, B*A and B*B are written to file.
Code Requirements:
-Include the problem statement in the top of the code (copy and
paste this document and enclose in comments)
-Include comments in the code
-Ask the user to enter the input and output file names.
-Read the input data matrices A and B from the input file. (A
sample input file is shown in Appendix A)
-Read the number of rows n.
-Read matrix A, which has n rows and n columns.
-Read string “A”.(this data is not used)
-Read matrix B, which has n rows and n columns.
-Read string “B”. (this data is not used)
- Calculate matrices AA, AB, BA and BB, where
-AA=A*A
-AB=A*B
-BA=B*A
-BB=B*B
-Write the following to the output file (A sample output file is
shown in Appendix B. Use type double for the matrix elements, and
include one decimal place in the output of each number)
-String “First matrix A”
- line space
-Matrix A, which has n rows and n columns (requires n lines)
-line space
-String “Second matrix B”
- line space
-Matrix B, which has n rows and n columns (requires n lines)
-line space
-String “Matrix multiplication A*A”
- line space
-Matrix AA, which has n rows and n columns (requires n lines)
-line space
-String “Matrix multiplication A*B”
- line space
-Matrix AB, which has n rows and n columns (requires n lines)
-line space
-String “Matrix multiplication B*A”
- line space
-Matrix BA, which has n rows and n columns (requires n lines)
-line space
-String “Matrix multiplication B*B”
- line space
-Matrix BB, which has n rows and n columns (requires n lines)
-line space
-Testing the code
-Run the code using the provided file inputFileTest.txt
(shown in Appendix A, and
included in the Canvas file directory for Homework 4). The output
should match the file
outputFileTest.txt
-Run the code using the provided file inputFile.txt.
Include the output file as part of the
files that you are submitting in
thezipper folder.
List of required deliverables
-A flowchart or pseudocode must show the code logic
-A java code or codes
-All the generated output files(in this case, file
outputFile.txt)
-Compress all these files in a folder and attach it in the Canvas
assignment corresponding to
homework # 4.
How it will be graded:
-All the required files and documents must be provided.
-The Java code must produce the correct results.
-The assignment must be submitted on time for full credit.
-Code style: Include comments in your code, use spaces to improve
code readability.
-Efficiency: Make sure your code follows the logic shown in your
flowchart or pseudocode.
Additional notes:
Any code that is not yours must be referenced. For instance,
provide the web link, author and so
forth.
Academic integrity will be enforced as needed. For instance, it is
not acceptable students
submitting identical codes, or copying code from the web without
referencing them.
inputFile.txt
5
1.0 2.0 1.0 3.0 4.0
3.0 1.0 2.0 5.0 1.0
2.0 4.0 3.0 6.0 1.0
4.0 1.0 2.0 3.0 2.0
1.0 2.0 1.0 3.0 1.0
A
4.0 2.0 3.0 1.0 2.0
1.0 3.0 1.0 2.0 3.0
4.0 5.0 2.0 3.0 4.0
3.0 1.0 7.0 5.0 2.0
1.0 2.0 3.0 4.0 1.0
B
inputFileTest.txt
2
1.0 2.0
3.0 4.0
A
3.0 4.0
1.0 2.0
B
outputFile.txt
First matrix A
1.0 2.0 1.0 3.0 4.0
3.0 1.0 2.0 5.0 1.0
2.0 4.0 3.0 6.0 1.0
4.0 1.0 2.0 3.0 2.0
1.0 2.0 1.0 3.0 1.0
Second matrix B
4.0 2.0 3.0 1.0 2.0
1.0 3.0 1.0 2.0 3.0
4.0 5.0 2.0 3.0 4.0
3.0 1.0 7.0 5.0 2.0
1.0 2.0 3.0 4.0 1.0
Matrix multiplication A*A
25.0 19.0 18.0 40.0 17.0
31.0 22.0 22.0 44.0 26.0
45.0 28.0 32.0 65.0 28.0
25.0 24.0 20.0 44.0 27.0
22.0 13.0 15.0 31.0 14.0
Matrix Multiplication A*B
23.0 24.0 40.0 39.0 22.0
37.0 26.0 52.0 40.0 28.0
43.0 39.0 61.0 53.0 41.0
36.0 28.0 44.0 35.0 27.0
20.0 18.0 31.0 27.0 19.0
Matrix Multiplication B*A
22.0 27.0 21.0 49.0 25.0
23.0 17.0 17.0 39.0 15.0
39.0 32.0 30.0 70.0 33.0
42.0 44.0 38.0 77.0 32.0
30.0 22.0 23.0 46.0 18.0
Matrix Multiplication B*B
35.0 34.0 33.0 30.0 30.0
20.0 24.0 31.0 32.0 22.0
42.0 44.0 54.0 51.0 41.0
58.0 53.0 65.0 59.0 49.0
31.0 29.0 42.0 38.0 29.0
outputFileTest.txt
First matrix A
1.0 2.0
3.0 4.0
Second matrix B
3.0 4.0
1.0 2.0
Matrix multiplication A*A
7.0 10.0
15.0 22.0
Matrix Multiplication A*B
5.0 8.0
13.0 20.0
Matrix Multiplication B*A
15.0 22.0
7.0 10.0
Matrix Multiplication B*B
13.0 20.0
5.0 8.0
Answer :
import java.util.*;
import java.io.*;
public class Matrix {
//a function to multiply 2 matrices a and b of dimension nxn and store then in c
public static void multiplyMatrix(double[][] a, double[][] b, double[][] c, int n )
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
}
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
System.out.println("Enter input file name: ");//prompt for input file
String inputfile = input.nextLine();//take input filename
System.out.println("Enter output file name: ");//prompt for output file
String outputfile = input.nextLine();//take output file name
input = new Scanner(new File(inputfile));//take input from inputfile
int n = input.nextInt();//take number of rows and cols
double[][] a = new double[n][n];//array to store A
double[][] b = new double[n][n];//array to store B
double[][] aa = new double[n][n];//matrix to hold product of AA
double[][] ab = new double[n][n];//matrix to hold product of AB
double[][] ba = new double[n][n];//matrix to hold product of BA
double[][] bb = new double[n][n];//matrix to hold product of BB
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = input.nextDouble();//take input array A from file
String A = input.next();//take letter A as input
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
b[i][j] = input.nextDouble();//take input array B from file
String B = input.next();//take letter B as input
input.close();//close the scanner
//to write into the output file
BufferedWriter bw = new BufferedWriter(new FileWriter(outputfile));
bw.write("First Matrix A");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(a[i][j] + " ");//write matrix A to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
bw.write("Second Matrix B");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(b[i][j] + " ");//write matrix B to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
multiplyMatrix(a,a,aa,n);//aa now has product of A*A.
bw.write("Matrix multiplication A*A");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(aa[i][j] + " ");//write matrix AA to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
multiplyMatrix(a,b,ab,n);//ab now has product of A*B
bw.write("Matrix multiplication A*B");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(ab[i][j] + " ");//write matrix AB to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
multiplyMatrix(b,a,ba,n);//ba now has product of B*A
bw.write("Matrix multiplication B*A");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(ba[i][j] + " ");//write matrix BA to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
multiplyMatrix(b,b,bb,n);//bb now has product of B*B
bw.write("Matrix multiplication B*B");
bw.newLine();//go to next line
bw.newLine();//go to next line
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
bw.write(bb[i][j] + " ");//write matrix BB to the file
}
bw.newLine();//go to next line
}
bw.newLine();//go to next line
bw.close();
}
}
Sample output on running the above code:
inputFileTest.txt:
Output on giving inputFileTest.txt as input:
outputFileTest.txt:
inputFile.txt:
Output on giving inputFile.txt as input:
outputFile.txt:
The goal in this assignment is to perform various matrix multiplications. Input matrices A and B...
The goal in this assignment is to perform various matrix multiplications. Input matrices A and B are read from a file. Output matrices A*A, A*B, B*A and B*B are written to file. -Ask the user to enter the input and output file names. -Read the input data matrices A and B from the input file. (A sample input file is shown in Appendix A) -Read the number of rows n. -Read matrix A, which has n rows and...
In C++
Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...
Hi it's my code for python
I almost finished my project but only one thing left which is
most confusing part please help me
I have to find most occurring ending character from a to z
For instance, output should be like this I have to find a to
z.
Words starting with ‘a’ end mostly with ‘O’
Words starting with ‘b’ end mostly with ‘O’
......
No words start with ‘O’(If there's no word in the character from a...
Please help me out with this assignment. Please read the
requirements carefully. And in the main function please cout the
matrix done by different operations! Thanks a lot!
For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...
SnowGeese File:
Trial Diet WtChange
DigEff ADFiber
1 Plants -6.0 0.0
28.5
2 Plants -5.0 2.5
27.5
3 Plants -4.5 5.0
27.5
4 Plants 0.0 0.0
32.5
5 Plants 2.0 0.0
32.0
6 Plants 3.5 1.0
30.0
7 Plants -2.0 2.5
34.0
8 Plants -2.5 10.0
36.5
9 Plants -3.5 20.0
28.5
10 Plants -2.5 12.5
29.0
11 Plants -3.0 28.0
28.0
12 Plants -8.5 30.0
28.0
13 Plants -3.5 18.0
30.0
14 Plants -3.0 15.0
31.0
15 Plants -2.5 ...
Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...
package hw2; public class SandBox { private final double length; // doesn't change after SandBox is constructed private final double width; // doesn't change after SandBox is constructed private final double height;// doesn't change after SandBox is constructed // current height of the sand within the box private double sandHeight; /* * Constructor * Initially there is no sand in this SandBox */ public SandBox(double length, double width, double...
This is my assignment prompt
This is an example of the
input and output
This is what I have so far
What is the 3rd ToDo in the main.cpp for
open the files and read the encrypted message? Does the rest of the
code look accurate?
Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...
Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...
/***************************************************
Name:
Date:
Homework #7
Program name: HexUtilitySOLUTION
Program description: Accepts hexadecimal numbers as input.
Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE
Enter BYE (case insensitive) to exit the program.
****************************************************/
import java.util.Scanner;
public class HexUtilitySOLUTION {
public static void main(String[] args) {
// Maximum length of input string
final byte INPUT_LENGTH = 4;
String userInput = ""; // Initialize to null string
Scanner input = new Scanner(System.in);
// Process the inputs until BYE is entered
do {...