Task: Write a C++ program !

Dear everybody

I am a newcomer and unfortunately, I have absolutely no idea in terms of the C++ programming language.
But I already know what the structure of an ordinary program is, but I still have a few basic questions (--->):

// on witch databases the computer takes a reference on
include --->what function has "include " ?
include
include

( --->MY INTERPRETATION: iostream, fstream and string are various databases and with the aid of those, the computer recognizes the commands and is able to execute them)

// Function declarations ---->why is that needed? void ...
int sensible
---->What do "void", "sensible" stand for? (MY INTERPRETATION: int stands for integer, so we only want an output of whole numbers)

//Global variables const double R=8.31441
(---->clear that here the constants like e.g. the Gas constant R is declared, but for what is "double" needed? Why isn't it here eagerly possible to use float? double is needed that the computer knows how much bytes he should offer to save the constant-right?)

int main ( ) {
//Parameters for input double V, n, Tmin, Tmax, step;
...

//Function definitions void readInput(double&V, double&n,double&Tmin,

double&Tmax, double&step) {

ifstream file("input.dat");

file >> V;
file>> n;
file>>Tmin;
file>>Tmax;
file>>step;

file.close( );
}

(with this function, the computer reads the definitions of the local variables (parameter) saved in the file "input.dat")

---->Question: Did I correctly describe the structure of an ordinary program? If not, what is missed?

Then, I still have the task to write a C++ program with the following task formulation:
Write a Program to compute the Helmholtz free energy of gaseous carbon dioxide using Monte Carlo integration

The van der Waals constants for gaseous carbon dioxide (CO2) are
a = 0.364 Nm4 mol-2 and b = 0.427 10-4 m3 mol-1.
The gas constant is R = 8.31441 JK-1 mol-1.

Write a C++ program that computes the integral (5.41) numerically employing the Monte Carlo technique of section 5.3, formula (5.16). The given function rando can be used to generate random numbers that are uniformly distributed on the interval (0, 1).
Test the rando function before integrating it into your new program. Make the program read the following data:
N = number of Monte Carlo sample points
V1 = lower limit of the integration interval
V2 = upper limit of the integration interval
a = van der Waals parameter a of the substance
b = van der Waals parameter b of the substance
n = number of moles of the substance
T = temperature
R = gas constant
H = parameter of the integration procedure

The program can be tested by setting a = b = 0 and comparing the result for very large N to the exact value obtained from (5.40). Use the program to calculate the change in Helmholtz free energy ΔA of one mole of carbon dioxide when it is compressed at T = 313.15 K from a volume V1 = 0.0257 m3 to a volume V 2 = 0.00247 m3. Compute ΔA using 3 different sequences of random numbers and use N = 1000, N = 10000 and N = 100000. Use also two different H- values, which are at least a factor 10 different. Make a table containing the 18 ΔA values and the exact value of ΔA calculated using (5.42), and the value of ΔA obtained using the ideal gas approximation (5.40). Analyze the differences and discuss the accuracy and convergence of the Monte Carlo simulations with respect to H and N.

The following sketch of a program may serve as a guide when writing the program
1. comments about purpose, contents, author, date, etc.
2. definition of variables as needed
3. function to compute f(x)=((an2)/V2)-(nRT)/(V-nb)) and Monte Carlo Integral
4. outline of the core of the program:
- print text
- read values of N, V1, V2, a, b, n, T, R and H, print these
- check for not-allowed input values
- set seed for the random number generator
- randomly choose x value in [V1, V2]
- calculate f(x)=((an2)/V2)-(nRT)/(V-nb))
- randomly choose y value in [0, H]
- accept or reject y value
- sum up ξn
- calculate the Monte Carlo integral
- print results: N, acceptance ration ξn/N, H, deltaA from the Monte Carlo integration, deltaA from the ideal gas approximation and the exact value of deltaA.

The beginning is given:

double rando (int &ig){
/* R. GEURTSEN, GRONINGEN, WFVG, JULY 1987 (Pascal VERSION) *
* REGULA WALSER, ZUERICH, JUNE 2000 (C++ VERSION) *
* *
* double rando (int &ig) *
* *
* RANDO GENERATES A RANDOM NUMBER RAND, USING A LINEAR *
* CONGRUENTIAL METHOD. THE RECURSION FORMULA *
* *
* IRAND = MOD(IRAND * B + 1, A) *
* *
* IS USED WITH B = 31415821 AND A = 100000000. THE LAST *
* DIGIT FROM THE RANDOM INTEGER IRAND IS CHOPPED OF, AND *
* THE NUMBER IS SCALED TO A REAL VALUE RAND BETWEEN 0 AND 1, *
* INCLUDING 0 BUT EXCLUDING 1. *
* *
* RETURN VALUE: DELIVERED WITH RANDOM NUMBER BETWEEN 0 AND 1 *
* IG: RANDOM NUMBER GENERATOR SEED, IS DELIVERED *
* WITH RANDOM INTEGER *
* */
const int m = 100000000;
const int m1 = 10000;
const int mult = 31415821;

int irand = abs(ig) % m;

// MULTIPLY IRAND BY MULT, BUT TAKE INTO ACCOUNT THAT OVERFLOW
// MUST BE DISCARDED, AND DO NOT GENERATE AN ERROR.

int irandh = int(irand / m1);
int irandl = irand % m1;
int multh = int(mult / m1);
int multl = mult % m1;

irand = ((irandh*multl+irandl*multh) % m1) * m1 + irandl*multl;
irand = (irand + 1) % m;

// CONVERT IRAND TO A REAL RANDOM NUMBER BETWEEN 0 AND 1.

double r = int(irand / 10) * 10.0;
r = r / m;
if ((r <= 0.0) || (r > 1.0))
r = 0.0;
ig = irand;
return r;
}


-
It would be great if you could help me with this difficult task, I absolutely have no idea what to do!
iostream, fstream and string are various databases and with the aid of those, the computer recognizes the commands and is able to execute them
Not quite. They're called "headers". They're text files that tell the compiler what functions are available for the program to use, what types of arguments they take, and what types they return.
They also often contain type definitions and variable declarations.

---->What do "void", "sensible" stand for? (MY INTERPRETATION: int stands for integer, so we only want an output of whole numbers)
The void part is not clear. Generally, it's used to mean that a function takes no arguments or returns nothing.
int is an integer type.
'sensible' is a variable name or, more properly, an identifier.
int sensible; is a declaration for a variable named 'sensible' of type int.

(---->clear that here the constants like e.g. the Gas constant R is declared, but for what is "double" needed? Why isn't it here eagerly possible to use float? double is needed that the computer knows how much bytes he should offer to save the constant-right?)
double is a floating point type like float, only able to hold more significant digits and much larger quantities. The upper limit for double is something like 10300, IIRC.

I can't read the rest. Please use wrap your code in [code]/* your code here */[/code] tags so the formatting is visible.
Thank you for you response, now the basic staff is clear!

I hope that now you can read my second question ;)

Then, I still have the task to write a C++ program with the following task formulation:

Write a Program to compute the Helmholtz free energy of gaseous carbon dioxide using Monte Carlo integration

---> for task formulations
see link
http://www.chemieonline.de/forum/attachment.php?attachmentid=24403&d=1322616647
and
http://www.chemieonline.de/forum/attachment.php?attachmentid=24404&d=1322616647

---> for given function rando
see link
http://www.chemieonline.de/forum/attachment.php?attachmentid=24405&d=1322616647

---> for references concerning equations (task formulation, in brackets)
see link
http://www.chemieonline.de/forum/attachment.php?attachmentid=24406&d=1322616647

It would be great if you could help me with this difficult task, I absolutely have no idea what to do!

code given:
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
/* double rando (int &ig){
/*     R. GEURTSEN, GRONINGEN, WFVG, JULY 1987 (Pascal VERSION)    
 *      REGULA WALSER, ZUERICH, JUNE 2000 (C++ VERSION)                 
*                                                                                                          
*       double rando (int &ig)                                                                    
*                                                                                                           
*       RANDO GENERATES A RANDOM NUMBER RAND, USING A LINEAR      
*       CONGRUENTIAL METHOD. THE RECURSION FORMULA                       
*                                                                                                            
*       IRAND = MOD(IRAND * B + 1, A)                                                    
*                                                                                                            
*       IS USED WITH B = 31415821 AND A = 100000000. THE LAST           
*       DIGIT FROM THE RANDOM INTEGER IRAND IS CHOPPED OF, AND      
*       THE NUMBER IS SCALED TO A REAL VALUE RAND BETWEEN 0 AND 1,
*       INCLUDING 0 BUT EXCLUDING 1.                                                      
*                                                                                                             
*      RETURN VALUE: DELIVERED WITH RANDOM NUMBER BETWEEN 0 AND 1 
*        IG: RANDOM NUMBER GENERATOR SEED, IS DELIVERED                    
*       WITH RANDOM INTEGER                                                                    
*                                                                                                               */
        const int m = 100000000;
        const int m1 = 10000;
        const int mult = 31415821;

        int irand = abs(ig) % m;

// MULTIPLY IRAND BY MULT, BUT TAKE INTO ACCOUNT THAT OVERFLOW
// MUST BE DISCARDED, AND DO NOT GENERATE AN ERROR.
  
        int irandh = int(irand / m1);
        int irandl = irand % m1;
        int multh = int(mult / m1);
        int multl = mult % m1;

        irand = ((irandh*multl+irandl*multh) % m1) * m1 + irandl*multl;
        irand = (irand + 1) % m;

// CONVERT IRAND TO A REAL RANDOM NUMBER BETWEEN 0 AND 1.

         double r = int(irand / 10) * 10.0;
         r = r / m;
         if ((r <= 0.0) || (r > 1.0))
               r = 0.0;
         ig = irand;
         return r;
}

 */



Last edited on
Topic archived. No new replies allowed.