AIIQ4 - system("notepad...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; struct matrix { int rows; int cols; float* elements; }; int i; a.elements=new float[m*n]; a.rows=m; a.cols=n; for(i=0;i<m*n;i++) a.elements[i]=s; } { if( (i >= a.rows) || (j >= a.cols) ){ cerr << "subscript out of bounds\n"; exit(1); } return a.elements[i*a.cols + j]; } ostream& operator << (ostream& out, matrix& a); istream& operator >> (istream& in, matrix& a); matrix operator + (matrix& a, matrix& b); matrix operator * (matrix& a, matrix& b); int main(void) { ifstream fin(""); ofstream fout("matricesoperators.out"); matrix a, b, c, d; fin >> a >> b >> c; fout <<a << b<< c; d = a + b; fout << "The sum of \n" << a << "and\n" << b << "is\n" << d; d = a * c; fout << "\nThe product of \n" << a << "and\n" << c << "is\n" << d;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: system(&quot;notepad matricesoperators.out&quot;); return 0; } ostream&amp; operator &lt;&lt; (ostream&amp; out, matrix&amp; a) { int i,j; out&lt;&lt;&quot; &quot;&lt;&lt;a.rows&lt;&lt;&quot; &quot;&lt;&lt;a.cols&lt;&lt;endl&lt;&lt;endl; for(i=0;i&lt;a.rows;i++){ for(j=0;j&lt;a.cols;j++) out&lt;&lt;&quot; &quot;&lt;&lt;entry(a,i,j); out&lt;&lt;endl&lt;&lt;endl; } return out; } istream&amp; operator &gt;&gt;(istream&amp; in, matrix&amp; a){ int i,j; in&gt;&gt;i&gt;&gt;j; init(a,i,j); for(i=0;i&lt;a.rows;i++) for(j=0;j&lt;a.cols;j++) in&gt;&gt;entry(a,i,j); return in; } matrix operator + (matrix&amp; a, matrix&amp; b){ int i,j; matrix d; init(d,a.rows,a.cols); for(i=0;i&lt;d.rows;i++) for(j=0;j&lt;d.cols;j++) entry(d,i,j)=entry(a,i,j)+entry(b,i,j); return d; } matrix operator * (matrix&amp; a, matrix&amp; b){ int i,j,k; matrix d; init(d,a.rows,b.cols); for(i=0;i&lt;d.rows;i++) for(j=0;j&lt;d.cols;j++) for(k=0;k&lt;a.cols;k++) entry(d,i,j)=entry(d,i,j)+entry(a,i,k)*entry(b,k,j); return d; }...
View Full Document

Page1 / 2

AIIQ4 - system(&amp;amp;quot;notepad...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online