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;
$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;
c++ strings c++11 file-system vectors
$endgroup$
add a comment |
$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;
c++ strings c++11 file-system vectors
$endgroup$
add a comment |
$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;
c++ strings c++11 file-system vectors
$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
c++ strings c++11 file-system vectors
edited 52 secs ago
Gitana
asked 13 hours ago
GitanaGitana
1149
1149
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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