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;
}
Code:
#include <iostream>
using namespace std;
int main()
{
unsigned variableY;
int variableX;
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;
cout << "\nThe address of the memory location of variableX: " << &variableX << endl;
cout << "The address of the memory location of variableY: " << &variableY << endl << endl;
if ((void*)&variableX < (void*)&variableY)
{
if (sizeof(int) == ((long)&variableY - (long)&variableX))
{
cout << "They are next to one another in memory!" << endl;
}
else
{
cout << "They are not adjacent to one another in memory!" << endl;
}
}
else if ((void*)&variableX > (void*)&variableY)
{
if (sizeof(int) == ((long)&variableX - (long)&variableY))
{
cout << "They are next to one another in memory!" << endl;
}
else
{
cout << "They are not adjacent to one another in memory!" << endl;
}
}
return 0;
}