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

// mulmatvec1.cpp - Program for testing dynamic memory allocation, version 1. 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
// 14 April 2006

#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] << " ";
        }// End for j
        cout << "\n";
    } // End for i
    cout << "\n";
    return;
} // End Echo

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

cout << " mulmatvec1 (14 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 ar_count = 0, 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;
}

try { // Beginning of try block

    A = new double*[mDim]; //Allocate space for pointers to rows of A matrix
    ar_count++;
    for (i = 0; i < mDim; i++) {
        A[i] = new double[mDim]; //Allocate space for columns of A matrix
    }//End for i
    b1 = new double[mDim];
    ar_count++;
    b2 = new double[mDim];

} // End of try block

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

    switch (ar_count){
        case 2: delete [] b1;
        case 1: for (i = i - 1; i >= 0; i--){ //Count down from the highest value of i that was reached before the exception
                              delete[] A[i];
                        } // End for i
                        delete [] A;
        default: cerr << "In catch block, so an exception occurred: " << xa.what() << "\n";
                       cout << "\nEnter any key to continue. \n";
                       cin >> rflag;
    } // End switch

    in.close();
    out.close();
    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

// 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";
}//End for i

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
}//End for i
delete [] A;
delete [] b1; //Release the memory allocated to b1 before ending program
delete [] b2; //Release the memory allocated to b2 before ending program
} //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