Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with C++ code 2 - Problem This program will add two sparse matrices in an efficient manner. Sparse matrices have many zero entries, but

Help with C++ code

2 - Problem This program will add two sparse matrices in an efficient manner. Sparse matrices have many zero entries, but such entries are omitted in the input file and corresponding data structures to save memory and reduce processing time. You will read in 2 files and output 1 file; you must ask the user for the names of the files in the console.

2.1 Matrices and Matrix Addition: Subscripts in input and output files are matrices that are based on a standard mathematical definition, indexed from 1. Matrices of size m n start on 1,1 and go up to m,n. Therefore, the input/output matrices should always have subscripts starting at 1,1. 2.2 Matrix Addition: The addition of 2 matrices is done by adding each element of the matrices that are the same size. Assuming a matrix M1 and M2 are of size m x n, the sum would be M3 = M1rc + M2rc r,c where 1 r m, 1 c n. Mrc represents the value of the matrix at row r and column c, and M1 and M2 are the input matrices.

2.2 Storage You must store each input matrix as a single 1-dimensional dynamic array of structs. struct element { int r; int c; double value; } element* m1=new element[size1]; // you must determine the size dynamically element* m2=new element[size2]; // you must determine the size dynamically

3.1 Input files specifics The input are two text files, with one matrix per file. Prior to any data, a line containing the dimensions of the matrix will be present. There will be ONE matrix entry per line in the file and each line will have a triplet of numbers r, c, v. Subscripts r and c are positive integers and the value v is a real number (possibly negative). Comments have a # at the beginning of the line. Your program must ignore lines that begin with the # symbol. These can be placed anywhere in the input file. Your program should not get stuck due to this format in the input file. Values v can be positive or negative, where negatives start with - and one digit (numbers like -.2, or -0.31416e+10 will not be provided). Your program must reject matrices whose sizes are incompatible for addition (i.e., having different dimensions) and produce an empty file as result with an error printed on the console. Incompatible matrices is the only case where your program should stop with something printed on the console; otherwise, nothing is printed on the console. Note that matrices are not necessarily squared and they can even have only one non-zero entry

4.1- Output File Specifics The output matrix should be written in sorted order by row and column in sparse format (one entry per line) as well. The first line must have the dimensions of the matrix. You should write output numbers only with 2 decimals.

image text in transcribed

Sparse Matrix Addition 2013 00-2 3+0 020 2034 Hints for the HW: 0003 struct element( 40 2 1 int r; int c; double value; M1 nput file1 3 4 11 4 132 141 23-2 2 43 M2 Input file 2 3 4 11-2 1 31 143 232 M3 element* m1-new element[size1]; 3 4 11 2 1 33 144 2 43 element*m2-new element[size2]; Sparse Matrix Addition 2013 00-2 3+0 020 2034 Hints for the HW: 0003 struct element( 40 2 1 int r; int c; double value; M1 nput file1 3 4 11 4 132 141 23-2 2 43 M2 Input file 2 3 4 11-2 1 31 143 232 M3 element* m1-new element[size1]; 3 4 11 2 1 33 144 2 43 element*m2-new element[size2]

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

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions

Question

2. Use the Wilcoxon test to analyze the data given in Example 9.9.

Answered: 1 week ago

Question

CL I P COL Astro- L(1-cas0) Lsing *A=2 L sin(0/2)

Answered: 1 week ago

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago