Average histogram combining multiple files and vector of vectors (in c++) Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Directory's disk usage listPython files syncImprove my script to organize a video collectionSearch through .xml files for text that exists anywhere in the fileGet directory permissions for all directories in treeRecursively list files in provided directory in the style of `npm ls`Track changes inside a directoryCombine two .log files and write into a single log file with sorted list based on timestampQuickSearch for files and directoriesSimple server log backup script utilising AWS

Should man-made satellites feature an intelligent inverted "cow catcher"?

Proving that any solution to the differential equation of an oscillator can be written as a sum of sinusoids.

Marquee sign letters

Pointing to problems without suggesting solutions

Why do C and C++ allow the expression (int) + 4*5?

Dinosaur Word Search, Letter Solve, and Unscramble

How do you cope with tons of web fonts when copying and pasting from web pages?

How to achieve cat-like agility?

Short story about astronauts fertilizing soil with their own bodies

Keep at all times, the minus sign above aligned with minus sign below

Is honorific speech ever used in the first person?

Flight departed from the gate 5 min before scheduled departure time. Refund options

The test team as an enemy of development? And how can this be avoided?

Hide attachment record without code

Is there a spell that can create a permanent fire?

Noise in Eigenvalues plot

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

How to create a button that adds InputFields when clicked?

Maximum rotation made by a symmetric positive definite matrix?

Restricting the Object Type for the get method in java HashMap

Magento 2 - Add additional attributes in register

How to get a flat-head nail out of a piece of wood?

Why did Israel vote against lifting the American embargo on Cuba?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics



Average histogram combining multiple files and vector of vectors (in c++)



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Directory's disk usage listPython files syncImprove my script to organize a video collectionSearch through .xml files for text that exists anywhere in the fileGet directory permissions for all directories in treeRecursively list files in provided directory in the style of `npm ls`Track changes inside a directoryCombine two .log files and write into a single log file with sorted list based on timestampQuickSearch for files and directoriesSimple server log backup script utilising AWS



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


I run a program of simulation N times (I use each time a different random numbers to initialize it). The results of the program are stored in 2N directories: every run produces two approximations. The directories contain the same files and have the following structure:




def-risultati-i, i = 1, ... , N



exc-risultati-i, i = 1, ... , N




Where def and exc are test names of the approximations. In each one of the directories there are histograms files (file-a.dat, file-b.dat, ...)




bin_left bin_right weight error




Where each bin is identified by the values of beginning and end.



I've written a program that takes a file from every directory and produces the average of the corresponding histogram. So for instance I take the file risultati.dat from




def-risultati-1,
def-risultati-2, ... , def-risultati-N,




The code works fine but I'd like to improve the structure of the function that does all the job. There are some redundant passages (e.g. I store the values of the bins for the files taken from every directory, even though they are the same for every run of the program) and my c++ skills are rusty. I'm not sure about those nested for loops on a vector of vectors...



Here it is a "demo" of the program:



#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <cmath>

void merge(int N, std::string appr, std::string title)
double a;
std::string fin_dir = appr + "-totale";
std::string init_dir = appr + "-risultati-";
std::string num, dir;
std::ifstream ri;
std::ofstream out(fin_dir+title);
std::vector<std::vector<double>> v_ri;

for(int j=1; j<=N; j++)
num = std::to_string(j);
dir = init_dir+num+title;
ri.open(dir);
std::vector<double> v;

while(ri >> a)
v.push_back(a);


v_ri.push_back(v);
ri.close();


a=0;
std::vector<double> weight;
for(int k=2; k<v_ri[0].size(); k=k+4)
for(int j=0; j<v_ri.size(); j++)
a = a+v_ri[j][k]/double(N);

weight.push_back(a);
a=0;


a=0;
std::vector<double> err;
for(int k=3; k<v_ri[0].size(); k=k+4)
for(int j=0; j<v_ri.size(); j++)
a = a+pow(v_ri[j][k], 2)/double(N);

a = sqrt(a);
err.push_back(a);
a=0;


int cont = 0;
for(int i=0; i<v_ri[0].size(); i=i+4)
out << std::scientific << v_ri[0][i] << " " << v_ri[0][i+1] << " ";
out << std::scientific << weight[cont] << " " << err[cont] << std::endl;
cont++;



int main()
//merge(numeber_of_directories, "exc"/"def", title)
//title: /namefile.dat

int N=3;

merge(N, "exc", "/ris.dat");
merge(N, "def", "/ris.dat");

return 0;










share|improve this question











$endgroup$


















    1












    $begingroup$


    I run a program of simulation N times (I use each time a different random numbers to initialize it). The results of the program are stored in 2N directories: every run produces two approximations. The directories contain the same files and have the following structure:




    def-risultati-i, i = 1, ... , N



    exc-risultati-i, i = 1, ... , N




    Where def and exc are test names of the approximations. In each one of the directories there are histograms files (file-a.dat, file-b.dat, ...)




    bin_left bin_right weight error




    Where each bin is identified by the values of beginning and end.



    I've written a program that takes a file from every directory and produces the average of the corresponding histogram. So for instance I take the file risultati.dat from




    def-risultati-1,
    def-risultati-2, ... , def-risultati-N,




    The code works fine but I'd like to improve the structure of the function that does all the job. There are some redundant passages (e.g. I store the values of the bins for the files taken from every directory, even though they are the same for every run of the program) and my c++ skills are rusty. I'm not sure about those nested for loops on a vector of vectors...



    Here it is a "demo" of the program:



    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <vector>
    #include <cmath>

    void merge(int N, std::string appr, std::string title)
    double a;
    std::string fin_dir = appr + "-totale";
    std::string init_dir = appr + "-risultati-";
    std::string num, dir;
    std::ifstream ri;
    std::ofstream out(fin_dir+title);
    std::vector<std::vector<double>> v_ri;

    for(int j=1; j<=N; j++)
    num = std::to_string(j);
    dir = init_dir+num+title;
    ri.open(dir);
    std::vector<double> v;

    while(ri >> a)
    v.push_back(a);


    v_ri.push_back(v);
    ri.close();


    a=0;
    std::vector<double> weight;
    for(int k=2; k<v_ri[0].size(); k=k+4)
    for(int j=0; j<v_ri.size(); j++)
    a = a+v_ri[j][k]/double(N);

    weight.push_back(a);
    a=0;


    a=0;
    std::vector<double> err;
    for(int k=3; k<v_ri[0].size(); k=k+4)
    for(int j=0; j<v_ri.size(); j++)
    a = a+pow(v_ri[j][k], 2)/double(N);

    a = sqrt(a);
    err.push_back(a);
    a=0;


    int cont = 0;
    for(int i=0; i<v_ri[0].size(); i=i+4)
    out << std::scientific << v_ri[0][i] << " " << v_ri[0][i+1] << " ";
    out << std::scientific << weight[cont] << " " << err[cont] << std::endl;
    cont++;



    int main()
    //merge(numeber_of_directories, "exc"/"def", title)
    //title: /namefile.dat

    int N=3;

    merge(N, "exc", "/ris.dat");
    merge(N, "def", "/ris.dat");

    return 0;










    share|improve this question











    $endgroup$














      1












      1








      1





      $begingroup$


      I run a program of simulation N times (I use each time a different random numbers to initialize it). The results of the program are stored in 2N directories: every run produces two approximations. The directories contain the same files and have the following structure:




      def-risultati-i, i = 1, ... , N



      exc-risultati-i, i = 1, ... , N




      Where def and exc are test names of the approximations. In each one of the directories there are histograms files (file-a.dat, file-b.dat, ...)




      bin_left bin_right weight error




      Where each bin is identified by the values of beginning and end.



      I've written a program that takes a file from every directory and produces the average of the corresponding histogram. So for instance I take the file risultati.dat from




      def-risultati-1,
      def-risultati-2, ... , def-risultati-N,




      The code works fine but I'd like to improve the structure of the function that does all the job. There are some redundant passages (e.g. I store the values of the bins for the files taken from every directory, even though they are the same for every run of the program) and my c++ skills are rusty. I'm not sure about those nested for loops on a vector of vectors...



      Here it is a "demo" of the program:



      #include <iostream>
      #include <fstream>
      #include <stdio.h>
      #include <stdlib.h>
      #include <vector>
      #include <cmath>

      void merge(int N, std::string appr, std::string title)
      double a;
      std::string fin_dir = appr + "-totale";
      std::string init_dir = appr + "-risultati-";
      std::string num, dir;
      std::ifstream ri;
      std::ofstream out(fin_dir+title);
      std::vector<std::vector<double>> v_ri;

      for(int j=1; j<=N; j++)
      num = std::to_string(j);
      dir = init_dir+num+title;
      ri.open(dir);
      std::vector<double> v;

      while(ri >> a)
      v.push_back(a);


      v_ri.push_back(v);
      ri.close();


      a=0;
      std::vector<double> weight;
      for(int k=2; k<v_ri[0].size(); k=k+4)
      for(int j=0; j<v_ri.size(); j++)
      a = a+v_ri[j][k]/double(N);

      weight.push_back(a);
      a=0;


      a=0;
      std::vector<double> err;
      for(int k=3; k<v_ri[0].size(); k=k+4)
      for(int j=0; j<v_ri.size(); j++)
      a = a+pow(v_ri[j][k], 2)/double(N);

      a = sqrt(a);
      err.push_back(a);
      a=0;


      int cont = 0;
      for(int i=0; i<v_ri[0].size(); i=i+4)
      out << std::scientific << v_ri[0][i] << " " << v_ri[0][i+1] << " ";
      out << std::scientific << weight[cont] << " " << err[cont] << std::endl;
      cont++;



      int main()
      //merge(numeber_of_directories, "exc"/"def", title)
      //title: /namefile.dat

      int N=3;

      merge(N, "exc", "/ris.dat");
      merge(N, "def", "/ris.dat");

      return 0;










      share|improve this question











      $endgroup$




      I run a program of simulation N times (I use each time a different random numbers to initialize it). The results of the program are stored in 2N directories: every run produces two approximations. The directories contain the same files and have the following structure:




      def-risultati-i, i = 1, ... , N



      exc-risultati-i, i = 1, ... , N




      Where def and exc are test names of the approximations. In each one of the directories there are histograms files (file-a.dat, file-b.dat, ...)




      bin_left bin_right weight error




      Where each bin is identified by the values of beginning and end.



      I've written a program that takes a file from every directory and produces the average of the corresponding histogram. So for instance I take the file risultati.dat from




      def-risultati-1,
      def-risultati-2, ... , def-risultati-N,




      The code works fine but I'd like to improve the structure of the function that does all the job. There are some redundant passages (e.g. I store the values of the bins for the files taken from every directory, even though they are the same for every run of the program) and my c++ skills are rusty. I'm not sure about those nested for loops on a vector of vectors...



      Here it is a "demo" of the program:



      #include <iostream>
      #include <fstream>
      #include <stdio.h>
      #include <stdlib.h>
      #include <vector>
      #include <cmath>

      void merge(int N, std::string appr, std::string title)
      double a;
      std::string fin_dir = appr + "-totale";
      std::string init_dir = appr + "-risultati-";
      std::string num, dir;
      std::ifstream ri;
      std::ofstream out(fin_dir+title);
      std::vector<std::vector<double>> v_ri;

      for(int j=1; j<=N; j++)
      num = std::to_string(j);
      dir = init_dir+num+title;
      ri.open(dir);
      std::vector<double> v;

      while(ri >> a)
      v.push_back(a);


      v_ri.push_back(v);
      ri.close();


      a=0;
      std::vector<double> weight;
      for(int k=2; k<v_ri[0].size(); k=k+4)
      for(int j=0; j<v_ri.size(); j++)
      a = a+v_ri[j][k]/double(N);

      weight.push_back(a);
      a=0;


      a=0;
      std::vector<double> err;
      for(int k=3; k<v_ri[0].size(); k=k+4)
      for(int j=0; j<v_ri.size(); j++)
      a = a+pow(v_ri[j][k], 2)/double(N);

      a = sqrt(a);
      err.push_back(a);
      a=0;


      int cont = 0;
      for(int i=0; i<v_ri[0].size(); i=i+4)
      out << std::scientific << v_ri[0][i] << " " << v_ri[0][i+1] << " ";
      out << std::scientific << weight[cont] << " " << err[cont] << std::endl;
      cont++;



      int main()
      //merge(numeber_of_directories, "exc"/"def", title)
      //title: /namefile.dat

      int N=3;

      merge(N, "exc", "/ris.dat");
      merge(N, "def", "/ris.dat");

      return 0;







      c++ strings c++11 file-system vectors






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 52 secs ago







      Gitana

















      asked 13 hours ago









      GitanaGitana

      1149




      1149




















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217850%2faverage-histogram-combining-multiple-files-and-vector-of-vectors-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217850%2faverage-histogram-combining-multiple-files-and-vector-of-vectors-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar