I need to complete this project. Here are the instructions below and the code I
ID: 3848878 • Letter: I
Question
I need to complete this project. Here are the instructions below and the code I must use as a baseline is below the instructions. Can I get help in completing this assignment:
Using the addresses.cpp file as a base, do the following:
· Create a pass-by-value function that takes an integer as an input. Pass in the integer a and, in the function, print out the passed in variable's address.
· Create a pass-by-pointer function that takes a pointer to an integer. Pass in a's address, and print the address stored in the pointer in that function.
· Create a pass-by-reference function that takes a reference to an integer. Pass in the integer a and, in the function, print out the passed in reference's address.
Turn in your C++. In the turn in notes, discuss whether the addresses are the same or different than a's address.
/* Print some addresses in memory */
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// Create some variables and print where they are in memory
int a, b;
int items[6];
int moreitems[2][3];
cout << "Location of ints a,b: "
<< static_cast<void*>(&a)
<< " "
<< static_cast<void*>(&b)
<< endl;
cout << "Location of int array items: " << static_cast<void*>(items)
<< endl;
cout << "Location of int array moreitems"
<< static_cast<void*>(moreitems)
<< endl;
return 0;
}
Explanation / Answer
#include<iostream>
void passByValue(int a)
{
// The address of a should be different than address of a in main
cout <<"Address of a passed as value is: "<<&a<<endl;
}
void passByPointer(int *a)
{
// The address in pointer should be same as address of a in main
cout <<"Address of a passed as pointer is: "<<a<<endl;
}
void passByReference(int &a)
{
// The address of a should be same as address of a in main
cout <<"Address of a passed as reference is: "<<&a<<endl;
}
int main()
{
// Create some variables and print where they are in memory
int a, b;
int items[6];
int moreitems[2][3];
a=10;
cout << "Location of ints a,b: "
<< static_cast<void*>(&a)
<< " "
<< static_cast<void*>(&b)
<< endl;
cout << "Location of int array items: " << static_cast<void*>(items)
<< endl;
cout << "Location of int array moreitems"
<< static_cast<void*>(moreitems)
<< endl;
passByValue(a);
passByPointer(&a);
passByReference(a);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.