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
178
179
180
181
182
183

// mulmatvec0.cpp - Program for testing dynamic memory allocation, version 0. 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).
// 7 April 2006
// Written in Microsoft Visual C++, Version 6

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

using namespace std;

void Echo(unsigned int N, double** matrix);

void Echo(unsigned int N, double** matrix) { //Routine to output a SQUARE Matrix
    cout << "\n";
    for (unsigned int i = 0; i < N; i++){
        for (unsigned int j = 0; j < N; j++){
            cout << matrix[i][j] << " ";
       }
       cout << "\n";
    } // End for i
    cout << "\n";
    return;
} // End Echo

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

cout << " mulmatvec0 (7 April 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') {
double** A = NULL; //Pointer to rows of A Matrix
double* b1 = NULL; // b1 vector
double* b2 = NULL; // b2 vector
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;
}

//Allocate space for pointers to rows of A matrix
if (!(A = new double*[mDim])){
    cout << "Allocation for A failed. \n";
    in.close(); //Close the input file before terminating
    out.close(); //Close the output file before terminating
    return 0;
} //End if

// Allocate space for entries of the columns of the A Matrix
for (i = 0; i < mDim; i++) {
    if (!(A[i] = new double[mDim])){//If failure, release memory allocated before ending program
        for (j = (i - 1); j >= 0; j--){
            delete [] A[j];
        }
        delete [] A;
        cout << "Allocation for columns of A failed.\n";
        in.close(); //Close the input file before terminating
        out.close(); //Close the output file before terminating
        return 0;
    } //End if !A
}//End for i

// Allocate space for b1 vector
if (!(b1 = new double[mDim])){
    for (i = 0; i < mDim; i++){ //Release all allocated memory before ending program
        delete [] A[i];
    }
    delete [] A;
    cout << "Allocation for b1 failed. \n";
    in.close(); //Close the input file before terminating
    out.close(); //Close the output file before terminating
    return 0;
} //End if

// Allocate space for b2 vector
if (!(b2 = new double[mDim])){
    for (i = 0; i < mDim; i++) {
        delete [] A[i];
    } // End for i
    delete [] A;
    delete [] b1; //Release the memory allocated to b1 before ending program
    cout << "Allocation for b2 failed. \n";
    in.close(); //Close the input file before terminating
    out.close(); //Close the output file before terminating
    return 0;
} //End if

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 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

// Echo(mDim, A);

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";
}

out << "\n";

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

for (i = 0; i < mDim; i++) {
    delete [] A[i]; //Free up memory used by A Matrix before ending program
}
delete [] A;
delete [] b1; //Release the memory allocated to b1 before ending program
delete [] b2;
} //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.