?C++ and please do procedural code and add comments to all the functions/process
ID: 3688705 • Letter: #
Question
?C++ and please do procedural code and add comments to all the functions/processes please!
I will be using Visual Studios to run/test the program.
NO C header/functions please use only C++
write a C++program which reads an adjacency matrix, M, (which will be square) from a file, and computes powers of M: Mp, where p varies from 1 to 5. Leave the output in a file; prompt the user for the name of the output file or inform the user of the name of the output file before exiting. Note: Mp shows the number of paths of length p between the nodes of the graph. The power of a matrix is recursively defined: M1 = M Mn+1 = Mn ? M For testing purposes, form the adjacency matrix for the following sample graph, and put it in a file (in the appropriate format – these matrices have int elements, not double elements), and run your program with that input file. The input adjacency matrix will contain all the paths of length 0 and 1 (between 2 possible points). The sample graph attatched below:
Using this graph and its associated adjacency matrix you should get the following partial output:
. . .
Matrix to the power = 3
0 2 1 3 0
3 1 0 0 1
0 1 1 2 0
2 0 0 0 1
1 1 0 1 1
Explanation / Answer
#include <iostream> // input output stream header file
//#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
#include <string> // header file with string function
// #include <conio.h> // console input output header file
#include <fstream>
#include <iomanip>
using namespace std;
using namespace System;
using namespace System::IO;
const int numOfRows = 7;
const int numOfCols = 7;
/*
The adjacency Matrix:
A B C D E
A 0 1 0 1 0
B 1 0 1 0 1
C 0 1 0 0 1
D 1 0 0 0 1
E 0 1 1 1 0
*/
class MyMatrix {
public:
MyMatrix() {}
MyMatrix ( int , int, int );
MyMatrix& powerOp^=(const MyMatrix& );
int matrRows, matColumns;
int matArr[numOfRows][numOfCols];
friend ostream& operator^ (ostream&, const MyMatrix&);
}; // end class MyMatrix
MyMatrix& MyMatrix ::operator^=(const MyMatrix& mat1) {
// findPower()
// recursive call
//findPower();
for (int i = 0; i<= numOfRows; i++)
for (j = 0; j<= numOfCols; j++)
mat2[i][j] = mat1[i][j] * mat1[i+1][j+1];
} // end function operator^ // findPower()
int main() // start main
{
ifstream File("E:/Files/AdjMatrix.txt");
string ipLn;
String^ outFile = "E:/Files/PowerMatrix.txt"
if(File)
if getline(File, ipLn) // read the adjacency matrix from the file
matrix=atof(ipLn);
StreamWriter^ strmWrtr = gcnew StreamWriter(outFile);
strmWrtr->WriteLine("The output text file name is: E:/Files/PowerMatrix.txt ");
Console::WriteLine(" File ('{0}') used for outputting", outFile);
MyMatrix mat1, mat2;
ifstream inputFile("E:/Files/AdjMatrix.txt", ios::in);
inputFile >> mat1;
File.close();
return 0; // exit
} // end of main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.