Programming and Graphics

(Kiana) #1

4.6 Functions with array arguments 113


Vector projections


As an application, we read a matrix and a vector from the file ma-
trixv.dat, and then multiply the vector by the matrix numerous times. The
code consists of the main program and a function that performs the multiplica-
tion, both contained in the filemapping.cc. After each mapping, the vector is
optionally normalized so that its length becomes equal to one, and aesthetically
printed on the screen.


/*----------------------------------------------
Multiply a vector by a square matrix many times
------------------------------------------------*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

void matvec (int, double[][50], double[], double[]);

//--- main ---

int main()
{
int n, i, j, norm;
double b[50],c[50],a[50][50];

cout << endl;
cout << " Normalize the vector after each projection?" << endl;
cout << " Enter 1 for yes, 0 for no" << endl;
cout << " -------------------------" << endl;
cin >> norm;

//--- Read the matrix and the vector:

ifstream inputdata;
inputdata.open("matrixv.dat");
inputdata >> n;

for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
inputdata >> a[i][j];
}
}

for (i=1;i<=n;i++)
Free download pdf