Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Language : C++ I need help on printing out this matrix from using a class i have everything else .below are the two cpp

Program Language :C++

I need help on printing out this matrix from using a class i have everything else .below are the two cpp files and header file. The problem is located Under Matrix Test.cpp with the function void printMatrix(const MatrixType &);(i made the text bold in the program code so you know where to look).

MatrixTest.cpp

#include // for input/output

#include // for output to console

#include // for formatted output

#include // for isnan (tests a non-valid number)

#include // for pausing the output in an .EXE file

#include "MatrixType.cpp" // for sparse matrix implementation

using namespace std; // for the standard template library

// prototypes for the main

void title();

void getMatrix(MatrixType &);

void printMatrix(const MatrixType &);

// global objects initialized

ifstream fin ("sparseInSimple.txt"); // opens file input fin

ofstream fout("sparseOut.txt"); // opens file output fout

int main()

{

title(); // outputs the program's title

MatrixType matrix; // instantiate a sparse matrix

getMatrix(matrix); // read the data of the matrix from a file

printMatrix(matrix); // output the 2D matrix formatted to console

fin.close(); // close the input file

fout.close(); // close the out put file

system("pause"); // pause the output to view using .exe

return 0; // signify all is ok to the operating system

}

void title()

{

cout << "S P A R S E M A T R I X I N P U T" << " ";

cout << setw(31) << "Written by x" << " ";

}

// reads in the sparse matrix and echo prints it to console and file

void getMatrix(MatrixType & matrix)

{

int sigRows, sigCols; // declare local variables to be read in

int row, col;

float value;

fin >> sigRows >> sigCols; // read first pair as significant # rows and # cols

cout << fixed << showpoint << setprecision(1); // format the output for one decimal fixed

fout << fixed << showpoint<< setprecision(1);

// echo print significant rows and significant cols to the screen and output file

cout << setw(11)<< sigRows << setw(9)<< sigCols << endl; // echo print sig. rows and cols.

matrix.setRowAndColumns(sigRows,sigCols);

// call the class function to set significant rows & cols

cout << matrix.numberOfRows() << endl;

// priming read 1st sparse data (prime the pump)

fin >> row >> col >> value;

// loop until data is used (end of the file)

while(!fin.eof())

{

cout << setw(11)<< row

<< setw(9)<< col

<< setw(9) << value << endl;

matrix.storeItem(row, col, value);

fin >> row >> col >> value;

}

// stores the file data into each node

// read in the remaining sparse data for each item

// end of loop

cout << " "; // triple space (vertically)

}

// echo outputs the sparse matrix to the console and file

void printMatrix(const MatrixType & matrix)

{

cout << "Printing the matrix" << endl;

for(int r= 5; r<5; r++ ){// nested for loop

if( )) // The problem is located around here

}

fout << "Printing the matrix" << endl;

cout << endl; // skip a line at the end

fout << endl; // skip a line at the end

}

Matrix Type.cpp

#include "MatrixType.h"

// include class specifications

MatrixType::MatrixType()

{

myRows = 0;

myCols = 0;

mySize = 0;

}

void MatrixType::setRowAndColumns (int r, int c)

{

myRows = r;

myCols = c;

}

int MatrixType::numberOfRows() const

{

return myRows;

}

int MatrixType::numberOfCols() const

{

return myCols;

}

void MatrixType::storeItem (int r, int c, float v)

{

oneItem item; // create local structure(node) of one item

item.row = r; // assign r to row

item.col = c; //assign c to col

item.value = v; // assign v to value

myMatrix[mySize]= item; //assign item to myMatrix

mySize++; // increment size of the Matrix by one

}

float MatrixType::valueAt (int r, int c) const

{

for(int index = 0; index < mySize; index++)

{

if(myMatrix[index].row == r && myMatrix[index].col == c)

return myMatrix[index].value;

}

}

MatrixType.h

#ifndef MATRIXTYPE_H

#define MATRIXTYPE_H

using namespace std;

struct oneItem // struct contains each array member

{

int row; // corresponds to the 2D row number

int col; // corresponds to the 2D column number

float value; // corresponds to the float value of member

};

const int MAXSIZE = 225; // maximum # of elements in the sparse array

class MatrixType

{

public:

MatrixType(); // default constructor initializes mySize, myRows, & myCols to 0

void setRowAndColumns (int r, int c); // sets rowsFilled & columnsFilled to r and c

float valueAt (int r, int c) const; // yields value at row and col of myMatrix

void storeItem (int r, int c, float value); // assigns value at row, col, & changes length

int numberOfRows () const; // yields rowsFilled

int numberOfCols () const; // yields columnsFilled

private:

oneItem myMatrix [MAXSIZE]; // complete assigned 1D array in sparse format

int myRows; // significant rows of full matrix

int myCols; // significant columns of full matrix

int mySize; // significant # of elements actually stored

};

#endif // MATRIXTYPE_H

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

Students also viewed these Databases questions

Question

What is loss of bone density and strength as ?

Answered: 1 week ago

Question

The paleolithic age human life, short write up ?

Answered: 1 week ago

Question

The Functions of Language Problems with Language

Answered: 1 week ago

Question

The Nature of Language

Answered: 1 week ago