thedemon13666
January 5th, 2012, 07:39 PM
Hi,
I have noticed a problem when using c++ on my ubuntu laptop, as the title suggests, when I am using two arrays it seems that for some reason the first value of one array is being set to thesecond value of another array and I am not sure why, here is some example code of mine, please note this is root code however I have noticed this happen with normal c++ code:
A it of pseudo code, I am reading in an ASCII file containing two lists separated by a space in a file, these are read into a pair of vectors, I couldnt find a simple way to do this so I used a counter to keep track of which number is was looking at, so it could be put in the correct vector.
As root does not seem to like vectors I then have to move the vectors into simple arrays.
The arrays are then used to plot a graph
#include "Riostream.h"
#include<iostream>
#include<fstream>
#include"TVector.h"
#include"TGraph.h"
#include"TCanvas.h"
#include"TROOT.h"
#include"TPad.h"
#include"TAxis.h"
#include"TMath.h"
#include"TH1F.h"
#include"TFrame.h"
#include"TLegend.h"
int main()
{
Double_t x;
Double_t E[260], res[260];
vector<Double_t> en;
vector<Double_t> re;
Int_t i = 0;
ifstream file("q3data.txt");
while(file>>x)
{
if (i%2==0)
{
en.push_back(x);
}
if (i%2>0)
{
re.push_back(x);
}
i=i+1;
}
for(Int_t j=0;j<=260;j++)
{
E[j]=en[j];
res[j]=re[j];
cout<<E[j]<<" "<<res[j]<<endl;
}
res[0]=re[0]; // IF THIS LINE IS NOT PRESENT THEN FOR SOME REASON res[0]=E[261]
cout<<res[0]<<endl;
cout<<E[0]<<endl;
TCanvas *c1 = new TCanvas("c1","1",200,10,700,500);
c1->SetFillColor(0);
c1->SetGrid();
//TH1F *hr = c1->DrawFrame(20,0,100,150);
TGraph *gr = new TGraph(260,E,res);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("dsfdf");
gr->GetXaxis()->SetTitle("df");
gr->GetYaxis()->SetTitle("df");
gr->Draw("ACP");
c1->SaveAs("3.1.pdf");
return 0;
}
I have indicated using a comment string in my code the line that seems to sort this out, however this is by no means a proper solution.....
In regards to this particular code for some reason res[0] is set equal to E[261] or en[261] IF res[0]=re[0] isnt added to the code as shown above
Does anyone have any idea why this is happening?
Thanks a bunch
EDIT I should add I am running ubuntu 11.10 x64
I have noticed a problem when using c++ on my ubuntu laptop, as the title suggests, when I am using two arrays it seems that for some reason the first value of one array is being set to thesecond value of another array and I am not sure why, here is some example code of mine, please note this is root code however I have noticed this happen with normal c++ code:
A it of pseudo code, I am reading in an ASCII file containing two lists separated by a space in a file, these are read into a pair of vectors, I couldnt find a simple way to do this so I used a counter to keep track of which number is was looking at, so it could be put in the correct vector.
As root does not seem to like vectors I then have to move the vectors into simple arrays.
The arrays are then used to plot a graph
#include "Riostream.h"
#include<iostream>
#include<fstream>
#include"TVector.h"
#include"TGraph.h"
#include"TCanvas.h"
#include"TROOT.h"
#include"TPad.h"
#include"TAxis.h"
#include"TMath.h"
#include"TH1F.h"
#include"TFrame.h"
#include"TLegend.h"
int main()
{
Double_t x;
Double_t E[260], res[260];
vector<Double_t> en;
vector<Double_t> re;
Int_t i = 0;
ifstream file("q3data.txt");
while(file>>x)
{
if (i%2==0)
{
en.push_back(x);
}
if (i%2>0)
{
re.push_back(x);
}
i=i+1;
}
for(Int_t j=0;j<=260;j++)
{
E[j]=en[j];
res[j]=re[j];
cout<<E[j]<<" "<<res[j]<<endl;
}
res[0]=re[0]; // IF THIS LINE IS NOT PRESENT THEN FOR SOME REASON res[0]=E[261]
cout<<res[0]<<endl;
cout<<E[0]<<endl;
TCanvas *c1 = new TCanvas("c1","1",200,10,700,500);
c1->SetFillColor(0);
c1->SetGrid();
//TH1F *hr = c1->DrawFrame(20,0,100,150);
TGraph *gr = new TGraph(260,E,res);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("dsfdf");
gr->GetXaxis()->SetTitle("df");
gr->GetYaxis()->SetTitle("df");
gr->Draw("ACP");
c1->SaveAs("3.1.pdf");
return 0;
}
I have indicated using a comment string in my code the line that seems to sort this out, however this is by no means a proper solution.....
In regards to this particular code for some reason res[0] is set equal to E[261] or en[261] IF res[0]=re[0] isnt added to the code as shown above
Does anyone have any idea why this is happening?
Thanks a bunch
EDIT I should add I am running ubuntu 11.10 x64