10/01/2015
Created a c++ file, "main.cpp", on emacs that would print "Hello World!" upon its execution with the following script:
#include <iostream>
using namespace std;
int main() {
cout <<"Hello World!" << endl;
return 0; //Exit the program
}
To execute the program, the following set of commands were used:
g++ main.cpp
./a.out
10/04/2015
"main2.cpp": The following script assigns a number to a variable and performs a basic function; however, the output only appears as a whole integer.
#include <iostream>
using namespace std;
int main() {
cout<<"hello world"<<endl;
int i=2;
cout<<"i="<<i<<endl;
double a=3.3;
cout<<"a="<<a<<endl;
int j=a*i;
cout<<"a*i="<<j<<endl;
return 0;
}
Output:
hello world
i=2
a=3.3
a*i=6
"main3.cpp": The following script assigns a value of "10" to variable "i", subtracts "1", then adds "1"
#include <iostream>
using namespace std;
int main() {
int n=10;
cout<<"n is "<<n<<endl;
n--;
cout<<"n is now "<<n<<endl;
n++;
cout<<"n is now "<<n<<endl;
return 0;
}
Output:
n is 10
n is now 9
n is now 10
"main4.cpp": The following script sets "prop" as a boolean variable, which if the assigned statement is true, then "1" will be the output; otherwise, "0" is the output.
#include <iostream>
using namespace std;
int main() {
bool prop;
prop=(5>1);
cout<<"prop is "<<prop<<endl;
prop=(1>5);
cout<<"prop is "<<prop<<endl;
prop=(1!=5); //"!=" means "not equal to"
cout<<"prop is "<<prop<<endl;
return 0;
}
Output:
prop is 1
prop is 0
prop is 1
"main5.cpp": The following script subtracts "1" from "n" while "n>0"
#include <iostream>
using namespace std;
int main() {
int n=10;
while(n>0) {
cout<<"n is "<<n<<endl;
n--;
}
return 0;
}
Output:
n is 10
n is 9
n is 8
n is 7
n is 6
n is 5
n is 4
n is 3
n is 2
n is 1
"main6.cpp": The following script does the same as the previous one, but using a for loop. IMPORTANT: "cout" command for displaying output needs to be added within the for loop.
#include <iostream>
using namespace std;
int main() {
for(int n=10; n>0; n--) {
cout<<"n is "<<n<<endl;
}
return 0;
}
"main7.cpp": The following script counts both "n" and "m" until "n" reaches "10"
#include <iostream>
using namespace std;
int main() {
int n=0, m=0;
while(n<10) {
cout<<"n is "<<n<<": ";
m=0;
while(m<n) {
cout<<m;
m++;
}
cout<<endl;
n++;
}
return 0;
}
Output:
n is 0:
n is 1: 0
n is 2: 01
n is 3: 012
n is 4: 0123
n is 5: 01234
n is 6: 012345
n is 7: 0123456
n is 8: 01234567
n is 9: 012345678
"main8.cpp": The following script does the same as the previous one, but using a for loop.
#include <iostream>
using namespace std;
int main() {
for(int n=0; n<10; n++) {
cout<<"n is "<<n<<": ";
for(int m=0; m<n; m++) {
cout<<m;
}
cout<<endl;
}
return 0;
}
10/06/2015
Friend's birthday today, so made a script that would print "HAPPY BIRTHDAY!" after a month and day user input.
#include <iostream>
#include <cmath>
#include <ctime>
#include <string>
#include <sstream>
using namespace std;
int main()
{
time_t t =time(NULL);
tm*timePtr=localtime(&t);
int day=(timePtr->tm_mday);
int month=(timePtr->tm_mon)+1;
cout<<"Enter day of birth(number): ";
string input1;
unsigned int bday;
while(getline(cin,input1))
{
istringstream(input1)>>bday;
cout<<"Enter month of birth(number): ";
string input2;
unsigned int bmonth;
while(getline(cin,input2))
{
istringstream(input2)>>bmonth;
if((bmonth==month)&&(bday==day))
{
cout<<"HAPPY BIRTHDAY!"<<endl;
}
}
}
return 0;
}
10/11/2015
The following script is a logic statement that prints the value of "n" which changes depending on a set of conditions.
#include <iostream>
using namespace std;
int main()
{
int n=10;
while(n>=10)
{
if(n>5)
{
cout<<"n is "<<n<<endl;
}
else
{
cout<<"n = "<<n<<endl;
n--;
}
return 0;
}
}
Output:
n is 10
The following script sets a point "p" that stores the memory address of variable "i"
#include <iostream>
using namespace std;
int main()
{
int i=10;
cout<<"The memory address of i is "<<&i<<"\n";
cout<<"The data stored at memory address "<<&i<<" is "<<i<<"\n";
int* p=&i;
cout<<"The value of p is "<<p<<"\n";
cout<<"We say that p 'point at' the memory location referenced by address "<<p<<"\n";
cout<<"The data stored at memory address "<<p<<" is "<<*p<<"\n";
return 0;
}
Output:
The memory address of i is 0x7fffec1eac94
The data stored at memory address 0x7fffec1eac94 is 10
The value of p is 0x7fffec1eac94
We say that p 'point at' the memory location referenced by address 0x7fffec1eac94
The data stored at memory address 0x7fffec1eac94 is 10
The following script creates two separate mailboxes, "i" and "j" that store an integer. Each mailbox is independent of the other and a change in one would not affect the other.
#include <iostream>
using namespace std;
int main()
{
int i=10;
int j=i;
cout<<"i = "<<i<<" and j = "<<j<<"\n";
i=5;
cout<<"i = "<<i<<" and j = "<<j<<"\n";
j=1;
cout<<"i = "<<i<<" and j = "<<j<<"\n";
return 0;
}
Output:
i = 10 and j = 10
i = 5 and j = 10
i = 5 and j = 1
The following script creates one mailbox, "i". Pointer "*p" is used to refer to the data in "i", thus a change in either "i" or "*p" will change the other.
#include <iostream>
using namespace std;
int main()
{
int i=10;
int* p=&i;
cout<<"i = "<<i<<" and *p = "<<*p<<"\n";
i=5;
cout<<"i = "<<i<<" and *p = "<<*p<<"\n";
*p=1;
cout<<"i = "<<i<<" and *p = "<<*p<<"\n";
return 0;
}
Output:
i = 10 and *p = 10
i = 5 and *p = 5
i = 1 and *p = 1
The following script stores an integer on an empty memory location without assigning it to a specific variable, but rather to a pointer "*p".
The only way to call back the address is through the pointer.
#include <iostream>
using namespace std;
int main()
{
int* p=new int(5);
cout<<"p points at address "<<p<<"\n";
cout<<"The data stored in address"<<p<<" is "<<*p<<"\n";
*p=10;
cout<<"Now the data stored in address "<<p<<" is "<<*p<<"\n";
return 0;
}
Output:
p points at address 0x212f010
The data stored in address0x212f010 is 5
Now the data stored in address 0x212f010 is 10
10/25/2015
The following script two arrays of different sizes and assigns values based on a loop.
#include <iostream>
using namespace std;
/*********************************\
*Arrays*
*This program demonstrates arrays*
\*********************************/
int main()
{
// a loop to demonstrate 1D arrays
int ii[3]={1,2,3};
int j=0;
while(j<3)
{
cout<<"ii of "<<j<<" is "<<ii[j]<<endl;
j++;
}
// a loop to demonstrate 2D arrays
int LL[2][3]={1,2,3,4,5,6};
j=0;
int k;
while (j<2)
{
k=0; // do not forget to initialize k here
while(k<3)
{
cout<<"LL of "<<j<<" "<<k<<" is "<<LL[j][k]<<endl;
k++;
}
j++;
}
return 0;
}
Output:
ii of 0 is 1
ii of 1 is 2
ii of 2 is 3
LL of 0 0 is 1
LL of 0 1 is 2
LL of 0 2 is 3
LL of 1 0 is 4
LL of 1 1 is 5
LL of 1 2 is 6
The following script calculates the dot product of two vectors by calling another .cpp script to do the calculate and a .txt file to provide the values of the 2 vectors.
/*HW6MAIN.CPP*/
#include <iostream>
#include <fstream>
#include "dotprod.cpp"
using namespace std;
int main()
{
//declare the vectors
double vector1[3];
double vector2[3];
//open the input file
ifstream infile;
infile.open("vectors.txt");
//store the input in the vectors and print the vectors for the user
infile>>vector1[0]>>vector1[1]>>vector1[2];
cout<<"Vector 1 is ("<<vector1[0]<<","<<vector1[1]<<","<<vector1[2]<<")"<<endl;
infile>>vector2[0]>>vector2[1]>>vector2[2];
cout<<"Vector 2 is ("<<vector2[0]<<","<<vector2[1]<<","<<vector2[2]<<")"<<endl;
//close the input file
infile.close();
//call the dot_prod function from dotprod.cpp
dot_prod(vector1,vector2);
return 0;
}
/*DOTPROD.CPP*/
#include <iostream>
using namespace std;
double dot_prod(double v1[3],double v2[3])
{
double dotdot;
dotdot=v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
cout<<"The dot product is "<<dotdot<<endl;
return 0;
}
vectors.txt
1 2 3
4 5 6
7
Output:
Vector 1 is (1,2,3)
Vector 2 is (4,5,6)
The dot product is 32
The following script works as a random number generator and creates a histogram based on number of iterations.
#include <iostream>
#include <math.h>
using namespace std;
// This code is a pseudo random number generator that relies on
// random generated numbers from overflow of memory locations
const int a=7141;
const int c=54773;
const int mmod=256200;
double getFlatRandom(int& inew)
{
double mranflat=0;
inew=inew%mmod;
double aa=double(inew)/double(mmod);
mranflat=aa;
inew=a*inew+c;
return mranflat;
}
// This code will call the pseudo-random number generator and learn
// some things about its properties by filling and then displaying
// a histogram
int main()
{
int num;
cout<<"Enter the number of loop iterations: ";
cin>>num;
int inew=2345; // This is the "seed" for the random number generator
int histo[10]={0,0,0,0,0,0,0,0,0,0};
double atmp;
for (int i=0;i<num;i++)
{
atmp=getFlatRandom(inew); // Call the random number generator
histo[int (atmp*10)]++; // Increment the histogram bin with the number it falls within
}
// Print the histogram to the screen
for (int i=0;i<10;i++)
{
cout<<i<<": ";
for (int j=0;j<int((double(100*histo[i])/double(num))+0.5);j++)
{
cout<<"=";
}
cout<<endl;
}
return 0;
}
Output:
Enter the number of loop iterations: 100
0: ============
1: ==========
2: =======
3: =============
4: =======
5: ==========
6: ===============
7: =========
8: ======
9: ===========
Enter the number of loop iterations: 1000
0: ==========
1: ==========
2: =========
3: ==========
4: ==========
5: ==========
6: ===========
7: ==========
8: ==========
9: ==========
10/31/2015
The following script is launched with ROOT commands and creates a histogram with Xming. The histogram has a Gaussian distribution, Gaus(mean,sigma), that is scaled by a random number 'r'. The histogram has 50 bins from 0-50 and 500 entries generated from the Gaussian distribution. The histogram is saved as a .gif file.
#include "TCanvas.h"
#include <TRandom.h>
void roottest() {
TCanvas *c1 = new TCanvas("c1","demo",200,10,700,500);
c1->SetFillColor(42);
TRandom r;
gRandom->SetSeed();
TH1F *histo1 = new TH1F("histo1","s",50,0.,50.);
histo1->SetMarkerStyle(21);
for (int i=0;i<500;i++) {
float p1=r.Gaus(10,2);
histo1->Fill(p1);
}
histo1->Draw("");
c1->SaveAs("c1.gif");
}