Skip to content

Commit 980ab48

Browse files
committed
Project 1 Step 1 hints and solutions check
1 parent d1eb1ed commit 980ab48

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

Project#01/hints/hint1-1.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
## Hint 1-1: Opening and closing the file stream
21
To open a file named "geom.dat", you need a file stream object:
32

43
```c++
@@ -10,12 +9,12 @@ To open a file named "geom.dat", you need a file stream object:
109

1110
int main()
1211
{
13-
ifstream input("geom.dat");
12+
ifstream input("geom.dat");
1413

15-
...
14+
...
1615

17-
input.close();
16+
input.close();
1817

19-
return 0;
18+
return 0;
2019
}
2120
```

Project#01/hints/hint1-2.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Use the ">>" operator to read the data from the file:
2+
3+
```c++
4+
#include <iostream>
5+
#include <fstream>
6+
#include <iomanip>
7+
8+
...
9+
10+
int main()
11+
{
12+
ifstream input("geom.dat");
13+
14+
int natom;
15+
input >> natom;
16+
17+
input.close();
18+
19+
return 0;
20+
}
21+
```

Project#01/hints/hint1-3.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
It may be convenient to use arrays to store the z-values and Cartesian coordinates of the atoms:
2+
```c++
3+
int zval[50];
4+
double x[50], y[50], z[50];
5+
```
6+
7+
A more elegant solution is to allocate the memory dynamically for each array once you know the number of atoms:
8+
```c++
9+
#include <iostream>
10+
#include <fstream>
11+
#include <iomanip>
12+
13+
...
14+
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+
}
35+
```
36+
37+
Don't forget to delete[] the memory after you're finished!

Project#01/hints/step1-solution.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```c++
2+
#include <iostream>
3+
#include <fstream>
4+
#include <iomanip>
5+
#include <cstdio>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
ifstream input("geom.dat");
12+
13+
int natom;
14+
input >> natom;
15+
16+
int *zval = new int[natom];
17+
double *x = new double[natom];
18+
double *y = new double[natom];
19+
double *z = new double[natom];
20+
21+
for(int i=0; i < natom; i++)
22+
input >> zval[i] >> x[i] >> y[i] >> z[i];
23+
24+
input.close();
25+
26+
cout << "Number of atoms: " << natom << endl;
27+
cout << "Input Cartesian coordinates:\n";
28+
for(int i=0; i < natom; i++)
29+
printf("%d %20.12f %20.12f %20.12f\n", (int) zval[i], x[i], y[i], z[i]);
30+
31+
delete[] zval;
32+
delete[] x; delete[] y; delete[] z;
33+
34+
return 0;
35+
}
36+
```
37+
38+
An even more elegant solution would be to couple the above to the [Molecule class](https://github.com/CrawfordGroup/ProgrammingProjects/wiki/Classes-and-Objects) we defined earlier in the Fundamentals section:
39+
```c++
40+
#include "molecule.h"
41+
#include <iostream>
42+
#include <fstream>
43+
#include <iomanip>
44+
#include <cstdio>
45+
46+
using namespace std;
47+
48+
int main()
49+
{
50+
Molecule mol("geom.dat", 0);
51+
52+
cout << "Number of atoms: " << mol.natom << endl;
53+
cout << "Input Cartesian coordinates:\n";
54+
mol.print_geom();
55+
56+
return 0;
57+
}
58+
```
59+
60+

0 commit comments

Comments
 (0)