#include <iostream>
using namespace std;
int main()
{
int j = 1;
cout << j << endl;
// assigning the address of outer j to jptr
int* jptr = &j;
// creating j2ptr to hold the address of inner j
int* j2ptr;
cout << jptr
<< "____________________" << endl;
int i = 5;
for (int j = 1; j<i ; j++)
{
// this for loop only operates on the
// inner j
j2ptr = &j;
cout << "Rock and Roll!" << j << endl;
}
// the outer j is not affected by the
// above for loop
cout << j2ptr
<< "____________________" << endl;
cout << j << endl;
return 0;
}
Thursday, October 27, 2011
Tesing Array
#include <iostream>
using namespace std;
void enterArray(int* a, int size)
{
// read and save in the array according the the size
int i;
for (i = 0; i < size; i++)
{
cout << "element " << i+1 << ": ";
cin >> a[i];
} // for
// print out the content
for (i = 0; i < size; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int size;
while (size)
{
int* intPtr; // a pointer to the integer type
cout << endl
<< "What is the size of the array: (0 to quit) ";
cin >> size;
int* score = new int[size];
intPtr = score;
cout << "array address: "<< *score << endl;
enterArray(score, size);
cout << "the pointed items: ";
for (int i=0; i<size+4; i++)
cout << *intPtr++ << ' ';
delete [] score;
}
return 0;
} // main
using namespace std;
void enterArray(int* a, int size)
{
// read and save in the array according the the size
int i;
for (i = 0; i < size; i++)
{
cout << "element " << i+1 << ": ";
cin >> a[i];
} // for
// print out the content
for (i = 0; i < size; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int size;
while (size)
{
int* intPtr; // a pointer to the integer type
cout << endl
<< "What is the size of the array: (0 to quit) ";
cin >> size;
int* score = new int[size];
intPtr = score;
cout << "array address: "<< *score << endl;
enterArray(score, size);
cout << "the pointed items: ";
for (int i=0; i<size+4; i++)
cout << *intPtr++ << ' ';
delete [] score;
}
return 0;
} // main
Tuesday, October 25, 2011
Thursday, October 20, 2011
floating point data representation
//
// This program is an illustration for potential floating point boundary error
// only the floating point number > 1 case is implemented.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ofstream fout;
fout.open("data.txt");
if (!fout.good()) throw "I/O error";
double x, mant;
int exp = 0;
cout << "Enter a postive x > 1: ";
//Now find exponent and mantissa
cin >> x;
cin.ignore(1000,10);
fout << x << endl;
//
// shifting the decimal point left
// until the number is smaller than 1
// to obtain the exponent
mant = x;
while (mant >= 1)
{
mant /= 2;
exp++; // At this point x = mant * 2^(exp)
}
while (mant < 0.5)
{
mant *= 2;
exp--;
}
cout << x <<" is stored in exponent and mantisa, \n"
<< "as 2^(eee) *.mmm\n"
<< x <<" = ";
cout << "2^(" <<exp<<") *.";
fout << x <<" is stored as mmm.mmm\n"
<< x <<" = ";
//
// shifting the decimal point right
// to convert decimal fraction to binary mantisa
int i=0;
string s = ""; // mantisa
string ss = ""; // binary float
float bfrac = 0; // binary fraction
float weight = 1; // fraction bit weight
while (mant > 0)
{
mant *= 2;
if (mant >= 1)
{
s = s + '1';
ss = ss + '1';
mant -= 1;
if(i >= exp) bfrac += weight = weight/2;
}
else
{
s = s + '0';
ss = ss + '0';
if(i >= exp) weight = weight/2;
}
if(++i == exp) ss = ss + '.';
if(i >= 23) break;
}
cout << s << endl;
cout << "or, presented as: " << ss << endl;
fout << ss << endl;
cout << "regenerated decimal fraction from binary fraction: \n"
<< bfrac << endl;
fout << "regenerated decimal fraction from binary fraction: \n"
<< bfrac << endl;
return 0;
}
// This program is an illustration for potential floating point boundary error
// only the floating point number > 1 case is implemented.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ofstream fout;
fout.open("data.txt");
if (!fout.good()) throw "I/O error";
double x, mant;
int exp = 0;
cout << "Enter a postive x > 1: ";
//Now find exponent and mantissa
cin >> x;
cin.ignore(1000,10);
fout << x << endl;
//
// shifting the decimal point left
// until the number is smaller than 1
// to obtain the exponent
mant = x;
while (mant >= 1)
{
mant /= 2;
exp++; // At this point x = mant * 2^(exp)
}
while (mant < 0.5)
{
mant *= 2;
exp--;
}
cout << x <<" is stored in exponent and mantisa, \n"
<< "as 2^(eee) *.mmm\n"
<< x <<" = ";
cout << "2^(" <<exp<<") *.";
fout << x <<" is stored as mmm.mmm\n"
<< x <<" = ";
//
// shifting the decimal point right
// to convert decimal fraction to binary mantisa
int i=0;
string s = ""; // mantisa
string ss = ""; // binary float
float bfrac = 0; // binary fraction
float weight = 1; // fraction bit weight
while (mant > 0)
{
mant *= 2;
if (mant >= 1)
{
s = s + '1';
ss = ss + '1';
mant -= 1;
if(i >= exp) bfrac += weight = weight/2;
}
else
{
s = s + '0';
ss = ss + '0';
if(i >= exp) weight = weight/2;
}
if(++i == exp) ss = ss + '.';
if(i >= 23) break;
}
cout << s << endl;
cout << "or, presented as: " << ss << endl;
fout << ss << endl;
cout << "regenerated decimal fraction from binary fraction: \n"
<< bfrac << endl;
fout << "regenerated decimal fraction from binary fraction: \n"
<< bfrac << endl;
return 0;
}
floating point data range and precision
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << fixed << setprecision(25) << endl;
cout << "--------- round-off error------------" << endl;
double x = 3;
cout << x << endl;
cout << (x + 0.0000000000000001) << endl;
cout << (x + 0.000000000000001) << endl << endl;
cout << "--------- more round-off -------------" << endl;
cout << scientific << setprecision(25) << endl;
double d;
for (d = 0; d != 2.0; d += 0.4)
cout << d << endl;
cout << d << endl << endl;
float f;
for (f = 0; f != 2.0; f += 0.4)
cout << f << endl;
cout << f << endl << endl;
cout << "--------- boundary test -------------" << endl;
f = 3.402823866e37;
cout << f << endl;
f = 3.402824036e37;
cout << f << endl;
f = 3.402824136e37;
cout << f << endl << endl;
f = 3.402823567e38;
cout << f << endl;
f = 3.402823568e38;
cout << f << endl;
f = 3.402823866e38;
cout << f << endl << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << fixed << setprecision(25) << endl;
cout << "--------- round-off error------------" << endl;
double x = 3;
cout << x << endl;
cout << (x + 0.0000000000000001) << endl;
cout << (x + 0.000000000000001) << endl << endl;
cout << "--------- more round-off -------------" << endl;
cout << scientific << setprecision(25) << endl;
double d;
for (d = 0; d != 2.0; d += 0.4)
cout << d << endl;
cout << d << endl << endl;
float f;
for (f = 0; f != 2.0; f += 0.4)
cout << f << endl;
cout << f << endl << endl;
cout << "--------- boundary test -------------" << endl;
f = 3.402823866e37;
cout << f << endl;
f = 3.402824036e37;
cout << f << endl;
f = 3.402824136e37;
cout << f << endl << endl;
f = 3.402823567e38;
cout << f << endl;
f = 3.402823568e38;
cout << f << endl;
f = 3.402823866e38;
cout << f << endl << endl;
return 0;
}
Subscribe to:
Comments (Atom)