Lab Example
Lab Example
//LABEXAMPLE1.cpp
/* Integer Type Ranges
This program prints the numeric ranges of the 6 integer types in C++:
*/
#include <iostream>
#include <climits> // defines the constants SHRT_MIN, etc.
using namespace std;
int main() {
// prints some of the constants stored in the <climits> header:
cout << "minimum short = " << SHRT_MIN << endl;
cout << "maximum short = " << SHRT_MAX << endl;
cout << "maximum unsigned short = 0" << endl;
cout << "maximum unsigned short = " << USHRT_MAX << endl;
cout << "minimum int = " << INT_MIN << endl;
cout << "maximum int = " << INT_MAX << endl;
cout << "minimum unsigned int = 0" << endl;
cout << "maximum unsigned int = " << UINT_MAX << endl;
cout << "minimum long = " << LONG_MIN << endl;
cout << "maximum long = " << LONG_MAX << endl;
cout << "minimum unsigned long = 0" << endl;
cout << "maximum unsigned long = " << ULONG_MAX << endl;
}
// LABEXAMPLE2.cpp
/*
Using the sizeof Operator
This program tells you how much space each of
the 12 fundamental types uses:
*/
#include <iostream> // defines the FLT constants
using namespace std;
int main()
{ // prints the storage sizes of the fundamental types:
cout << "Number of bytes used:\n";
cout << "\t char : " << sizeof(char) << endl;
cout << "\t short : " << sizeof(short) << endl;
cout << "\t int : " << sizeof(int) << endl;
cout << "\t long : " << sizeof(long) << endl;
cout << "\t unsigned char : " << sizeof(unsigned char) << endl;
cout << "\t unsigned short : " << sizeof(unsigned short) << endl;
cout << "\t unsigned int : " << sizeof(unsigned int) << endl;
cout << "\t unsigned long : " << sizeof(unsigned long) << endl;
cout << "\t signed char : " << sizeof(signed char) << endl;
cout << "\t float : " << sizeof(float) << endl;
cout << "\t double : " << sizeof(double) << endl;
cout << "\t long double : " << sizeof(long double) << endl;
}
// LABEXAMPLE3.cpp
/*
Reading from the <cfloat> Header File
This program tells you the precision and
magnitude range that the float type has on your system:
*/
#include <cfloat> // defines the FLT constants
#include <iostream> // defines the FLT constants
using namespace std;
int main() {
// prints the storage sizes of the fundamental types:
int fbits = 8*sizeof(float); // each byte contains 8 bits
cout << "float uses " << fbits << " bits:\n\t"
<< FLT_MANT_DIG - 1 << " bits for its mantissa,\n\t "
<< fbits - FLT_MANT_DIG << " bits for its exponent,\n\t "
<< 1 << " bit for its sign\n"
<< " to obtain : " << FLT_DIG << " sig. digits\n"
<< " with minimum value : " << FLT_MIN << endl
<< " and maximum value : " << FLT_MAX << endl;
}
// LABEXAMPLE4.cpp
/*
Integer Overflow
This program repeatedly multiplies n by 1000
until it overflows.
*/
#include <iostream>
using namespace std;
int main() {
// prints n until it overflows:
int n=1000;
cout << "n = " << n << endl;
n *= 1000; // multiplies n by 1000
cout << "n = " << n << endl;
n *= 1000; // multiplies n by 1000
cout << "n = " << n << endl;
n *= 1000; // multiplies n by 1000
cout << "n = " << n << endl;
}
// LABEXAMPLE5.cpp
/*
Floating-point Overflow
This program is similar to the one in Example 4.
It repeatedly squares x until it overflows.
*/
#include <iostream>
using namespace std;
int main() {
// prints x until it overflows:
float x=1000.0;
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e., it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e., it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e., it squares x
cout << "x = " << x << endl;
x *= x; // multiplies n by itself; i.e., it squares x
cout << "x = " << x << endl;
}
/*
Note the difference between integer overflow and floating-point overflow.
// LABEXAMPLE6.cpp
/*
Round-off Error
This program does some simple arithmetic to
illustrate roundoff error:
*/
#include <iostream>
using namespace std;
int main() {
// illustrates round-off error::
double x = 1000/3.0;
cout << "x = " << x << endl; // x = 1000/3
double y = x - 333.0;
cout << "y = " << y << endl; // y = 1/3
double z = 3*y - 1.0;
cout << "z = " << z << endl; // z = 3(1/3) - 1
if (z == 0)
cout << "z == 0.\n";
else
cout << "z does not equal 0.\n"; // z != 0
}
// LABEXAMPLE7.cpp
/*
Hidden Round-off Error
This program implements the quadratic
formula to solve quadratic equations.
*/
#include <cmath> // defines the sqrt() function
#include <iostream>
using namespace std;
int main() {
// implements the quadratic formula
float a, b, c;
cout << "Enter the coefficients of a quadratic equation:" << endl;
cout << "\ta: ";
cin >> a;
cout << "\tb: ";
cin >> b;
cout << "\tc: ";
cin >> c;
cout << "The equation is: " << a << "*x*x + " << b
<< "*x + " << c << " = 0" << endl;
float d = b*b - 4*a*c; // discriminant
float sqrtd = sqrt(d);
float x1 = (-b + sqrtd)/(2*a);
float x2 = (-b - sqrtd)/(2*a);
cout << "The solutions are:" << endl;
cout << "\tx1 = " << x1 << endl;
cout << "\tx2 = " << x2 << endl;
cout << "Check:" << endl;
cout << "\ta*x1*x1 + b*x1 + c = " << a*x1*x1 + b*x1 + c << endl;
cout << "\ta*x2*x2 + b*x2 + c = " << a*x2*x2 + b*x2 + c << endl;
}
/*
use following data to test the programm:
1. Enter the coefficients of a quadratic equation:
a: 2
b: 1
c: -3
*/
All program Must be complete and run in lab also imp for
mst-1