My homework has a bonus question.
Bonus question:
Can anyone give me a hint on how I could resolve this problem?
Nevermind.. I got it working with reinterpret_cast
Bonus question:
- Declare two variables, one is of regular integer type and the other of unsigned integer type.
- Find out their sizes.
- Find out if those two variables are next to one another in the memory.
- Find out the largest integer (signed or unsigned) that these two variables can hold.
Code:
if (&variableX+1 == &variableY){
Code:
if (&variableY+1 == &variableX){
Code:
//Assignment 1
#include <iostream>
using namespace std;
int main()
{
//Declare a variable of double type
double variableA;
//Display the size of that variable
cout << "The size of variableA: " << sizeof variableA << " bytes" << endl;
//Display the location of that variable
cout << "The address of the memory location of variableA: " << &variableA << endl;
//Display what is in it before you put any data into it
cout << "The contents of variableA: " << variableA << endl << endl;
//Prompt the user to enter a number from the keyboard
cout << "Enter a number: "; cin >> variableA;
//then display the tripled value of the number
cout << variableA << " multiplied by 3 is " << variableA*3 << endl << endl;
system("PAUSE");
//Pick one other data type that C++ supports to do the same
int variableB;
//Display the size of that variable
cout << "\nThe size of variableB: " << sizeof variableB << " bytes" << endl;
//Display the location of that variable
cout << "The address of the memory location of variableB: " << &variableB << endl;
//Display what is in it before you put any data into it
cout << "The contents of variableB: " << variableB << endl << endl;
//Prompt the user to enter a number from the keyboard
cout << "Enter a number: "; cin >> variableB;
//then display the tripled value of the number
cout << variableB << " multiplied by 3 is " << variableB*3 << endl << endl;
system("PAUSE");
//Bonus question:
//Declare two variables, one is of regular integer type and the other of
//unsigned integer type
int variableX;
unsigned variableY;
//Find out their sizes
cout << "\nNote: variableX is a signed int while variableY is an unsigned int";
cout << "\n\nThe size of variableX is: " << sizeof variableX << " bytes";
cout << "\nThe size of variableY is: " << sizeof variableY << " bytes" << endl;
//Find out if those two variables are next to one another in the memory
cout << "\nThe address of the memory location of variableX: " << &variableX << endl;
cout << "The address of the memory location of variableY: " << &variableY << endl << endl;
if (&variableX+1 == &variableY){
cout << "They are next to one another in memory!" << endl;
}
else {
if (&variableY+1 == &variableX){
cout << "They are next to one another in memory!" << endl;
}
else {
cout << "They are not adjacent to one another in memory!" << endl;
}
}
system("PAUSE");
return 0;
}
Nevermind.. I got it working with reinterpret_cast