AIIQ1 - cout << "...

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]; } void write(ostream& out, matrix& a); void read(istream& in, matrix& a); matrix add(matrix& a, matrix& b); matrix mult(matrix& a, matrix& b); int main(void) { ifstream fin("matrices.in"); matrix a, b, c, d; read(fin, a); read(fin, b); read(fin, c); d = add(a,b); cout << " The sum of \n\n"; write(cout, a); cout << " and\n\n"; write(cout, b); cout << " is\n\n"; write(cout, d); d = mult(a,c); cout << "\n The product of \n\n"; write(cout, a);
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: cout &lt;&lt; &quot; and\n\n&quot;; write(cout, c); cout &lt;&lt; &quot; is\n\n&quot;; write(cout, d); system(&quot;pause&quot;); return 0; } void write(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;&quot; &quot;; for(i=0;i&lt;a.rows;i++){ for(j=0;j&lt;a.cols;j++) out&lt;&lt;entry(a,i,j)&lt;&lt;&quot; &quot;; out&lt;&lt;endl&lt;&lt;&quot; &quot;; } } void read(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); } matrix add(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 mult(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

AIIQ1 - cout &amp;amp;lt;&amp;amp;lt; &amp;amp;quot;...

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