Let us understand this code?

Hi all,
My teacher gave me a code and told me to try to understand it.
I have some questions would you please help me?

1- I know this code is for modelling but why he has written this part of code?

2- what are lines 20-23 for?
3-what does line 27 do? What is necessity of it?
4- what do lines 30-38 do?
5-what doe lines 42-43 do? what does tk:: do?
6- what is the purpose of lines 47-51-55-59?



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
  #include <iostream>
#include <fstream>
#include <math.h>
#include <cstdlib>
#include <sys/time.h>
#include <time.h>
#include "omp.h"


using namespace std;

//--------------------------------------------------------------------------
// global variables
double ds=0.25, dt=1, t=50000;
double scale_factor;
int nx, ny;
const int nq=9, nd=2;
const int np=5000;

#include "function1.h"
#include "function2.h"
#include "time_keeping.h"
#include "output.h"

//--------------------------------------------------------------------------
// main program
int main(int argc, char *argv[]){

//--------------------------------------------------------------------------
// initiate simulation

	function1_initiate(argc, argv);

	function2_initiate();

	output_initiate();

	time_initiate(argc, argv);

//--------------------------------------------------------------------------
// set program running
	tk::running=true;
	while(tk::running){

//--------------------------------------------------------------------------
// write output to file
		write_output();

//--------------------------------------------------------------------------
// 
		function1_method();

//--------------------------------------------------------------------------
// 
		function2_model();

//--------------------------------------------------------------------------
// increase time step and check if end time is reached
		time_increase();

//--------------------------------------------------------------------------
// end of main loop
	}

//--------------------------------------------------------------------------
// end program
	return 0;
}
Last edited on
2- what are lines 20-23 for?

Files that are created by yourself.

3-what does line 27 do? What is necessity of it?

Every program needs a main function, it is the function that is read first by compiler.
Your program consists of what is inside the Main ()


while(tk::running){ , runs while "tk::running" is true.
meaning: "tk::running" > 0.

Someone please correct me if im wrong about any of the above.
Last edited on
Thanks for your reply.
1- But what does "int argc, char *argv[])" in main statement do?
2- what would happen if did not write "tk::" in line 42?
3- what about lines 31-38? what do argc, argv do?
The first argument to main() is the number of
parameters passed to the program.


The second argument to main() is an
array of pointers to null-terminated character strings

2- what would happen if did not write "tk::" in line 42?

I don't know since i don't know what's inside the files that are included.
But since tk::running is set to true the while loop on next line is infinite.
if you didn't set tk::running to true on line 42, the loop would just run aslong tk::running is true. or >= 1
Last edited on
Topic archived. No new replies allowed.