AIIQ1 - cout<<" and\n\n" write(cout c...

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 Document Right 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 << " and\n\n"; write(cout, c); cout << " is\n\n"; write(cout, d); system("pause"); return 0; } void write(ostream& out, matrix& a) { int i,j; out<<" "<<a.rows<<" "<<a.cols<<endl<<" "; for(i=0;i<a.rows;i++){ for(j=0;j<a.cols;j++) out<<entry(a,i,j)<<" "; out<<endl<<" "; } } void read(istream& in, matrix& a){ int i,j; in>>i>>j; init(a,i,j); for(i=0;i<a.rows;i++) for(j=0;j<a.cols;j++) in>>entry(a,i,j); } matrix add(matrix& a, matrix& b){ int i,j; matrix d; init(d,a.rows,a.cols); for(i=0;i<d.rows;i++) for(j=0;j<d.cols;j++) entry(d,i,j)=entry(a,i,j)+entry(b,i,j); return d; } matrix mult(matrix& a, matrix& b){ int i,j,k; matrix d; init(d,a.rows,b.cols); for(i=0;i<d.rows;i++) for(j=0;j<d.cols;j++) for(k=0;k<a.cols;k++) entry(d,i,j)=entry(d,i,j)+entry(a,i,k)*entry(b,k,j); return d; }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

AIIQ1 - cout<<" and\n\n" write(cout c...

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