Skip to content

Commit cd717a9

Browse files
committed
hint 2-2 inline code reference check
1 parent 980ab48 commit cd717a9

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

Project#01/hints/hint1-3.md

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,15 @@ double x[50], y[50], z[50];
66

77
A more elegant solution is to allocate the memory dynamically for each array once you know the number of atoms:
88
```c++
9-
#include <iostream>
10-
#include <fstream>
11-
#include <iomanip>
9+
int natom;
10+
input >> natom;
1211

13-
...
12+
int *zval = new int[natom];
13+
double *x = new double[natom];
14+
double *y = new double[natom];
15+
double *z = new double[natom];
1416

15-
int main()
16-
{
17-
ifstream input("geom.dat");
18-
19-
int natom;
20-
input >> natom;
21-
22-
input.close();
23-
24-
int *zval = new int[natom];
25-
double *x = new double[natom];
26-
double *y = new double[natom];
27-
double *z = new double[natom];
28-
29-
...
30-
31-
delete[] zval; delete[] x; delete[] y; delete[] z;
32-
33-
return 0;
34-
}
17+
delete[] zval; delete[] x; delete[] y; delete[] z;
3518
```
3619

3720
Don't forget to delete[] the memory after you're finished!

Project#01/hints/hint2-1.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
If we choose to store the matrix of bond distances, we need to allocate the necessary memory, either as a static two-dimensional array:
2+
```c++
3+
double R[50][50];
4+
```
5+
6+
or via dynamic allocation using the [Molecule class](https://github.com/CrawfordGroup/ProgrammingProjects/wiki/Classes-and-Objects):
7+
```c++
8+
double **R = new double* [mol.natom];
9+
for(int i=0; i < mol.natom; i++)
10+
R[i] = new double[mol.natom];
11+
```
12+
13+
Don't forget to delete[] the memory at the end of the program:
14+
```c++
15+
for(int i=0; i < mol.natom; i++)
16+
delete[] R[i];
17+
delete[] R;
18+
```

Project#01/hints/hint2-2.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
To build the distance matrix, we need a loop for each index:
2+
```c++
3+
...
4+
#include <cmath>
5+
...
6+
7+
for(int i=0; i < mol.natom; i++) {
8+
for(int j=0; j < mol.natom; j++) {
9+
R[i][j] = sqrt(
10+
(mol.geom[i][0]-mol.geom[j][0])*(mol.geom[i][0]-mol.geom[j][0])
11+
+ (mol.geom[i][1]-mol.geom[j][1])*(mol.geom[i][1]-mol.geom[j][1])
12+
+ (mol.geom[i][2]-mol.geom[j][2])*(mol.geom[i][2]-mol.geom[j][2])
13+
);
14+
}
15+
}
16+
```
17+
Note also that the `sqrt()` function is part of the C math library; thus we need the `#include <cmath>` directive.

0 commit comments

Comments
 (0)