1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

// mulmatvec2.cpp - Program for testing dynamic memory allocation, version 2. For this purpose,
// this program prompts the user to input the size, N, of an N X N array, [A]. The array is then
// created, as well as two one-dimensional arrays of size N: b1 and b2. The entries for all
// three arrays are input from a text file. This program then multiplies b1 by a scalar, a, to
// create a new vector b1a. It then computes [A](b1a) + (b2).
// Written in Visual C++ 2005 Express Edition
// 9 May 2006

#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <vector>
#include <float.h>

using namespace std;

typedef vector<double> C1DArray;
typedef vector<vector<double> > C2DArray;

void Echo1DArray(const C1DArray a1);
void Echo2DArray(const C1DArray a2);

//Routine to output a 1D Array of type double to the console
void Echo1DArray(const C1DArray a1) {
    cout << "\n";
    for (unsigned int i = 0; i < a1.size(); i++) {
        cout << a1[i] << "\n";
    } // End for i
    cout << "\n";
    return;
} // End Echo1DArray

//Routine to output a 2D, Square Array of type double to the console
void Echo2DArray(const C2DArray a2) {
    cout << "\n";
    for (unsigned int i = 0; i < a2.size(); i++) {
        for (unsigned int j = 0; j < a2[0].size(); j++) {
        cout << a2[i][j] << " ";
        } // End for j
        cout << "\n";
    } // End for i
    cout << "\n";
    return;
} // End Echo2DArray

int main()
{char rflag = 0; //Readiness flag

cout << " mulmatvec2 (9 May 2006)\n";
cout << "=========================================================================== \n";
cout << "This program does some matrix calculations.\n";
cout << "First, it calculates the result of multiplying vector b1 by scalar a.\n";
cout << "The result is multiplied by matrix [A] and the result added to vector b2.\n";
cout << "The dimension, N, of the [A] matrix, and of the b1 and b2 vectors should \n";
cout << "have been saved beforehand in a file named mulmatvecdat.\n";
cout << "mulmatvecdat should be in the same folder as the mulmatvec executable.\n";
cout << "The next values of mulmatvecdat should be the entries for matrix [A],\n";
cout << "with data for row 1 first, then row 2, then row 3, etc.\n";
cout << "The next N entries should be the values for the b1 vector.\n";
cout << "The next N entries should be the values for the b2 vector.\n";
cout << "The last entry should be the value of the scalar a.\n";
cout << "\nThe data is assumed to be of type double. Variables used within this program\n";
cout << "are type double.\n";
cout << "\nThe output is written to the file mulmatvecout.txt.\n";

cout << "\nIs everything ready (are you ready to continue?)? If yes, Enter y. \n";
cout << "Otherwise Enter any other key. \n";
cin >> rflag;

if (toupper(rflag) == 'Y') {
C1DArray b1, b2; // b1 and b2 vectors
C2DArray A; // A Matrix
int i, j, mDim;
double a, temp1;

ifstream in("mulmatvecdat.txt", ios::in);

if (!in) {
    cout << "Cannot open the input file.\n";
    return 0;
}

in >> mDim; //Input the Matrix dimension from the file
if (mDim < 1) {
    cout << "Invalid dimension entered. Program terminated. \n";
    in.close(); //Close the input file before terminating
    return 0;
}

ofstream out("mulmatvecout.txt", ios::out);
if (!out) {
    cout << "Cannot open the output file. Program terminated.\n";
    in.close(); //Close the input file before terminating
    return 0;
}

// Beginning of try block, just in case vector re-sizing unsuccessful
try {

    // Resize A to accommodate the mDim x mDim Square array
    A.resize(mDim); // mDim rows
    for (i = 0; i < mDim; i++){
        A[i].resize(mDim); // mDim columns
    } // End for i

    b1.resize(mDim); // Resize b1 to accommodate the mDim entries
    b2.resize(mDim); // Resize b2 to accommodate the mDim entries

} // End of try block

catch (bad_alloc& xa) { // Catch block, for exceptions

    in.close();
    out.close();
    cerr << "In catch block, so an exception occurred: " << xa.what() << "\n";
    cout << "\nEnter any key to continue. \n";
    cin >> rflag;
    return 0;

} // End of catch block

for (i = 0; i < mDim; i++){ //Input the A Matrix from the file
    for (j = 0; j < mDim; j++){
        in >> A[i][j];
    }//End for j
}//End for i

for (i = 0; i < mDim; i++){ //Input the b1 vector from the file
    in >> b1[i];
}//End for i

for (i = 0; i < mDim; i++){ //Input the b2 vector from the file
    in >> b2[i];
}//End for i

in >> a; //Input the a scalar from the file

in.close(); //Close the input file

// cout << "\nEchoing the A matrix.\n";
// Echo2DArray(A);

// cout << "\nEchoing the b1 vector.\n";
// Echo1DArray(b1);

// cout << "\nEchoing the b2 vector.\n";
// Echo1DArray(b2);

// cout << "\nEchoing the a scalar.\n";
// cout << a << " \n";

for (i = 0; i < mDim; i++){
    temp1 = 0.0;
    for (j = 0; j < mDim; j++){
        temp1 += A[i][j]*a*b1[j];
    }//End for j
    b2[i] += temp1;
}//End for i

out.precision(DBL_DIG);

out << "The resulting vector entries follow:\n";
out << "\n";
for (i = 0; i < mDim; i++){
    out << b2[i] << " \n";
}//End for i

out.close();
cout << "\nDone! The solution is in the text file mulmatvecout.txt \n";

} //End if rflag = 'Y'
else cout << "\nNot ready. Try again when ready with information. \n";
cout << "\nEnter any key to continue. \n";
cin >> rflag;
return 0;
} // End main program.