/////////////////////////////////////////////////////////////////////// //storageSize.cpp #include using namespace std; int main() { cout << "The storage allocated for a char is " << sizeof(char) << " bytes" << endl; cout << "The storage allocated for an unsigned short integer is " << sizeof(unsigned short int) << " bytes" << endl; cout << "The storage allocated for an integer is " << sizeof(int) << " bytes" << endl; cout << "The storage allocated for a long integer is " << sizeof(long int) << " bytes" << endl; cout << "The storage allocated for an unsigned long integer is " << sizeof(unsigned long int) << " bytes" << endl; cout << "The storage allocated for a float is " << sizeof(float) << " bytes" << endl; cout << "The storage allocated for a double is " << sizeof(double) << " bytes" << endl; cout << "The storage allocated for a long double is " << sizeof(long double) << " bytes" << endl; return 0; } /////////////////////////////////////////////////////////////////////// //binaryRep.cpp #include using namespace std; void printBinary(unsigned short val); int main(){ unsigned short inVal; cout << "Enter a number between 0 and 65535: "; cin >> inVal; cout << "Your number in binary is "; printBinary(inVal); cout << endl; return 0; } void printBinary(unsigned short val){ for(int i = 15; i >= 0; i--) if(val & (1 << i)) cout << "1"; else cout << "0"; } //binaryRep.cpp: modified to allow for sign and used on page 16 #include using namespace std; void printBinary(short val); int main(){ short inVal; cout << "Enter a number between -32768 and 32767: "; cin >> inVal; cout << "Your number in binary is "; printBinary(inVal); cout << endl; return 0; } void printBinary(short val){ for(int i = 15; i >= 0; i--) if(val & (1 << i)) cout << "1"; else cout << "0"; } //binaryRep.cpp modified for floating point on pages 19 #include using namespace std; void printbinary(char val){ for(int i = 7; i >= 0; i--) if(val & (1 << i)) cout << "1"; else cout << "0"; } int main(){ float f; cout << "enter a number: "; cin >> f; char* pf = reinterpret_cast(&f); cout << "your number in binary is "; for(int i = sizeof(float) - 1; i >= 0; i--) printbinary(pf[i]); cout << endl; return 0; } /////////////////////////////////////////////////////////////////////// //series.cpp #include #include #include using namespace std; int main(int argc, char* argv[]){ float sumL = 0., sumU = 0.; int n = atoi(argv[1]); for(int j = 1; j <= n; j++){ sumL = sumL + 1/(float)(j*j); sumU = sumU + 1/(float)((n + 1 - j)*(n + 1 - j)); } cout << setprecision(8) << "Direct and reverse sums are " << sumL << " and " << sumU << endl; return 0; } //series.cpp: modified to use double precision on page 26 #include #include #include using namespace std; int main(int argc, char* argv[]){ double sumL = 0., sumU = 0.; double sumInf; int n = atoi(argv[1]); for(int j = 1; j <= n; j++){ sumL += 1/static_cast(j*j); sumU += 1/static_cast((n + 1 - j)*(n + 1 - j)); } cout << setprecision(8) << "Direct and reverse sums are " << sumL << " and " << sumU << endl; return 0; }