#include <iostream>
using namespsce std;
void main()
{
char letter = 'a';
cout << letter;
letter=letter+1;
cout << letter;
}
a
b
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10
for ease 1 + 1 + 1 = 11
to add 7 + 4
7 = 111
4 = 100
----
11 = 1011
to subtract 7 - 6
7 = 111
6 = 110 first form the two's complement 001 + 1 = 010
then add
7 = 111
010
-----
1001
ignore the final carry and the answer is 001 = 1.
Bits Low High 16 -32,768 32,767 32 -2,147,483,648 2,147,483,647
| Vendor | Exponent bits | Low | High | Mantissa bits | Decimal Digits | |
|---|---|---|---|---|---|---|
| IEEE | 11 | -308 | 308 | 52 | 16 | |
| Cyber | 11 | -293 | 323 | 48 | 15 | |
| IBM 3081 | 7 | -79 | 75 | 56 | 16 | |
| VAX D | 7 | -39 | 38 | 56 | 17 | |
| VAX G | 11 | -309 | 307 | 52 | 16 | |
| Cray | 15 | -2466 | 2465 | 48 | 14 |
#include <iostream>
using namespace std;
int main()
{
float value,sum;
double dsum;
int count;
sum=dsum=0.0;
value=1.0e-3;
cout << "Adding 1/1,000 1,000 times " << "\t";
for(count=0;count<(int)1.0e3;count++) {
sum+=value;
dsum+=value;
}
cout << "float=" << sum << "\t" << "double=" << dsum <<endl;
sum=dsum=0.0;
value=1.0e-4;
cout << "Adding 1/10,000 10,000 times " << "\t";
for(count=0;count<(int)1.0e4;count++) {
sum+=value;
dsum+=value;
}
cout << "float=" << sum << "\t" << "double=" << dsum <<endl;
sum=dsum=0.0;
value=1.0e-5;
cout << "Adding 1/100,000 100,000 times " << "\t";
for(count=0;count<(int)1.0e5;count++) {
sum+=value;
dsum+=value;
}
cout << "float=" << sum << "\t" << "double=" << dsum <<endl;
system("pause");
return 0;
}
Adding 1/1,000 1,000 times float=0.999991 double=1 Adding 1/10,000 10,000 times float=1.00005 double=1 Adding 1/100,000 100,000 times float=1.00099 double=1 Press any key to continue . . .
#include <iostream>
using namespace std;
int main()
{
float epsilon;
int count=0;
epsilon=1.0;
cout << "Computing machine epsilon " << endl;
while(1.0+epsilon > 1.0) {
count++;
epsilon/=2;
}
epsilon*=2;
cout << "It took " << count << " iterations to compute epsilon = " << epsilon << endl;
system("pause");
return 0;
}
Computing machine epsilon It took 64 iterations to compute epsilon = 1.0842e-019 Press any key to continue . . .
int k=5; float x,y=20.0; x=y/k;
float x; int a=7, b=3; x=a/b;What is the result?
float x; int a=7, b=3; x=Now what is the result?a/b;
#include <iostream.h>
void main()
{
cout << "char " << sizeof(char) << endl;
cout << "short " << sizeof(short) << endl;
cout << "int " << sizeof(int) << endl;
cout << "long " << sizeof(long) << endl;
cout << "float " << sizeof(float) << endl;
cout << "double " << sizeof(double) << endl;
}
I got the following results from running on different architectures:
| Size of Variables | ||||||
|---|---|---|---|---|---|---|
| Type | Vax | Sun | Convex | Cray | Apollo | MS VC++ |
| char | 1 | 1 | 1 | 1 | 1 | 1 |
| short | 2 | 2 | 2 | 8 | 2 | 2 |
| int | 4 | 4 | 4 | 8 | 4 | 4 |
| long | 4 | 4 | 4 | 8 | 4 | 4 |
| float | 4 | 4 | 4 | 8 | 4 | 4 |
| double | 8 | 8 | 8 | 8 | 8 | 8 |
char 1 bool 1 short 2 int 4 long 4 float 4 double 8 Press any key to continue . . .
int main()
{
int a,b;
float c;
char d;
}
int main()
{
int a=1;
float b=1.2e3;
char c='a';
}
Local variables are initialized every time the block of code
is executed.
#include <iostream>
using namespace std;
void f(); // function prototype
int main()
{
f(); // call function f
f(); // call function f
system("pause");
return 0;
}
void f() // function f
{
int localX=1; // declare and set local varaible
cout << "In Function f " << endl;
cout << "localX is " << localX << endl;
localX=2;
return;
}
In Function f localX is 1 In Function f localX is 1 Press any key to continue . . .
#include <limits>
#include <float.h>
#include <iostream>
using namespace std;
int main()
{
int max=INT_MAX;
cout << "The maximum value for an integer is : " << max << endl;
cout << "Add +1 to go over the max - overflow" << endl;
max++;
cout << "Now the value is : " << max << endl;
cout << endl;
float maxf= FLT_MAX;
cout << "The Maximum Float Value is : " << maxf <<endl;
cout << "Times 1 million to force overflow" << endl;
maxf*=1.0e6;
cout << "After overflow : " << maxf <<endl;
cout << endl;
float minf=FLT_MIN;
cout << "The Minimum Float Value is " << minf << endl;
cout << "Divide by 1 trillion to force underflow " << endl;
minf/=1.0e12;
cout << "After underflow : " << minf << endl;
system("pause");
return 0;
}
The maximum value for an integer is : 2147483647 Add +1 to go over the max - overflow Now the value is : -2147483648 The Maximum Float Value is : 3.40282e+038 Times 1 million to force overflow After overflow : 1.#INF The Minimum Float Value is 1.17549e-038 Divide by 1 trillion to force underflow After underflow : 0 Press any key to continue . . .