looping function

Hi everyone. I'm writing a code which takes 4 files and do statistics stuff with the datas. I really would like to avoid this ugly copy-pasting, but I don't know exatly how I sould do it.
I was thinking about a struct with an extra data-member that store the amount of the other data-member, a pointer to the struct, then apply the funciton in a for loop and use the pointer to jump to the next data-member but I can't figure out how to do it.
If anyone has reference to leave here or tips to do this, I would really appreciate it.


here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int main( int argc, const char** argv ) 
{
	if( argc != 5 )
	{
		std::cout << "Usage <name_program>: " << argv[0] << "<file.dat>, <file.dat>, <file.dat>, <file.dat>  \n"  ;
		exit (-1) ;	
	}

	std::vector<double>  v100, v1k, v5k, v10k ;

	v100 = readAll <double> ( argv[1] ) ; //result_1.dat
	v1k = readAll <double> ( argv[2] ) ;  //result_2.dat
	v5k = readAll <double> ( argv[3] ) ;  //result_3.dat
	v10k = readAll <double> ( argv[4] ) ; //result_4.dat
	
        //...
	
}
	
You need a class, something like:

1
2
3
4
5
6
7
8
9
10
11
class Statistics
{
     public:
         void OpenData(const char* file_name);

         void DoThis();
         void DoThat();

     private:
         std::vector<double> mData;
};


Then your main becomes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(int argc, const char** argv)
{
      if( argc != 5 )
      {
	     std::cout << "Usage " << argv[0] << "file.dat1 ... file.dat6 \n"  ;
	     exit (-1) ;	
      }

      Statistics stat;

      for(int i = 1; i < 6; ++i)
           stat.OpenFile(argv[i])

      stat.DoThis()
      stat.DoThat();

      // ...
}
Last edited on
Hi,

What does the data look like?

One could have a struct to hold all the data points and their residuals. Then have a std::vector of that struct type. The struct could have static members (meaning there is only version of these values for all the objects of that struct type) for the totals, mean, standard deviations etc.

Not sure what you mean about pointers to get to the next value, just use a range based for loop to go through the whole vector.
Last edited on
The dada are the result of an Integral evaluated with Monte Carlo's techniques. Each file stores 10'000 numbers which are the result of the same integral evaluated with a different MC's technique (4 files --> 4 different methods).
Now, I want to store them in different conteiners because I want to highlight the differences between each MC technique used to evaluate the same integral, and see (with statistics and graphs) which MC method is the more accurate.
Ok, so you can have a struct which represents the data, but you won't be able to have static members for such things as the totals because you are using the same struct for 4 different sets of data.

So create the struct Data, then make a class which has a private std::vector<Data>. That class could have members which represent summary data such as totals or mean or Standard Deviation. Have functions which respectively read data, calculate, and show the results. Make objects out of this class, one for each data set.

This is just my take on how to do it, others might have better ideas :+)

If you need to do random numbers or distributions, use the builtin C++ found here:
https://en.cppreference.com/w/cpp/numeric/random

Don't use the C rand functions.

I hop it all goes well for you :+)
Topic archived. No new replies allowed.