Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to write a program that reads two matrices from files, multiples them3, and displays outputs both to the terminal and a result file.

You are to write a program that reads two matrices from files, multiples them3, and displays outputs both to the terminal and a result file. To begin, we have a very easy file format to represent a matrix. Consider the following example matrix: [ 1 2 3 4 5 6 ] For this program, the corresponding file would have the following contents: 2 3 1 2 3 4 5 6 That is, first the number of rows, then the number of columns, and then the contents in row-major order4. Inside the program, we will represent a matrix as an array of arrays of integers. So, the above example could be initialized as int[][] m = {{1, 2, 3}, {4, 5, 6}}; So the length of the outer array is the number of rows, and the length of each inner array is the number of columns. To get used to this representation, first complete the canMultiply method, which takes two matrices (in order) and checks if they can be multiplied via their dimensions. Once you are getting the hang of things, move on to readMatrix to create this matrix from a Scanner. With these complete, youll be ready for the matrixMultiply method. Your program will then prompt the user for file paths, attempt to open them (providing an error if they dont), check their dimensions using your method, and then output the results both to the terminal and to the file supplied by the user. Note: the screen and file representations will be different, and you have been supplied methods to perform this output correctly. For example Enter path for matrix 1: m1.txt Enter path for matrix 2: m2.txt Enter path for result: m3.txt 1 2 3 4 5 6 X 1 1 1 2 2 2 3 3 3 = 14 14 14 32 32 32 In this case assume m1.txt contained the contents described above, and m2.txt contained the following: 3 3 1 1 1 2 2 2 3 3 3 After the program runs, m3.txt will contain the following: 2 3 14 14 14 32 32 32 Your main method should provide appropriate input validation: Enter path for matrix 1: m2.txt Enter path for matrix 2: m1.txt Enter path for result: m3.txt 1 1 1 2 2 2 3 3 3 X 1 2 3 4 5 6 = Bad matrix dimensions Enter path for matrix 1: m_bad.txt Enter path for matrix 2: m2.txt Enter path for result: m3.txt Error opening file(s)

Starter code:

Learn Git and GitHub without any code!

Using the Hello World guide, youll start a branch, write comments, and open a pull request.

Read the guide

witcomp1050/pa2-richmondgatwitPrivate

  • Watch

    2
  • Star0

  • Fork0
  • Code
  • Issues
  • Pull requests
  • Actions
  • Projects
  • Wiki
  • Security
  • Insights

main

pa2-richmondgatwit/src/edu/wit/cs/comp1050/PA2c.java /

Jump to

Go to file

github-classroom Initial commit

Latest commit bd652c7 yesterday History

1 contributor

Executable File 119 lines (107 sloc) 2.71 KB

RawBlame

package edu.wit.cs.comp1050;
import java.io.PrintWriter;
import java.util.Scanner;
//TODO: document this class
public class PA2c {
/**
* Error to output if can't open any files
*/
public static final String ERR_FILE = "Error opening file(s)";
/**
* Error to output if files open but matrices
* are of incompatible dimensions for multiplication
*/
public static final String ERR_DIMS = "Bad matrix dimensions";
private static void _outputMatrix(PrintWriter out, int[][] m, boolean includeDimensions) {
for (int r=0; r
if (includeDimensions && r==0) {
out.printf("%d%n%d%n", m.length, m[0].length);
}
for (int c=0; c
out.printf("%d", m[r][c]);
out.printf((c
}
}
}
/**
* Prints a matrix to the terminal
* without dimensions
*
* @param m matrix to print
*/
public static void printMatrix(int[][] m) {
_outputMatrix(new PrintWriter(System.out, true), m, false);
}
/**
* Prints a matrix to a file
* with associated dimensions
*
* @param m matrix to print
* @param pw open file
*/
public static void printMatrix(int[][] m, PrintWriter pw) {
_outputMatrix(pw, m, true);
}
/**
* Checks if two matrices can be multiplied
* (i.e. the columns of the first match
* the rows of the second)
*
* @param m1 matrix 1
* @param m2 matrix 2
* @return true if m1 x m2 is legal
*/
public static boolean canMultiply(int[][] m1, int[][] m2) {
return false;
}
/**
* Reads and returns a matrix from a scanner
* Format:
* m (# rows)
* n (# #cols)
* r0c0 r0c1 ... r0cn (values in row 0, column-by-column)
* r1c0 r1c1 ... r1cn (values in row 1, column-by-column)
* ...
* rmc0 rmc1 ... rmcn (values in last row, column-by-column)
*
* Results in...
* int[][] {
* {r0c0, r0c1, ... r0cn},
* {r1c0, r1c1, ... r1cn},
* ...
* {rmc0, rmc1, ... rmcn}
* }
*
* @param s input source
* @return resulting matrix
*/
public static int[][] readMatrix(Scanner s) {
return null;
}
/**
* Multiply two matrices and
* return the result (assumes
* input matrices are valid
* for multiplication)
*
* @param m1 matrix 1
* @param m2 matrix 2
* @return result of m1 x m2
*/
public static int[][] matrixMultiply(int[][] m1, int[][] m2) {
return null;
}
/**
* Program to multiply matrices:
* 1. Ask for paths for 3 files (2 input, 1 output)
* 2. Check if inputs can be multiplied
* 3. If so, multiply!
* - Output the full problem to the console
* - Output only the result matrix to the file
*
* @param args command-line arguments, ignored
*/
public static void main(String[] args) {
// Hint: paths should be read as an entire line!
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

2. Why has the conflict escalated?

Answered: 1 week ago