In: Computer Science
C++
Download the attached program and complete the functions. (Refer to comments)
main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of x. Then use indirection to print out the // value of x using pointer variable. (See Program 10-3) return 0; } int exercise_3() { int a[] = {1,2,3,4,5,6,7,8,9}; int *n; // Print the address of the array a, // Notice that printing the address of an array is different then printing // the address of a variable. // Assign the address of the array to the variable n. Then de-reference n // to print out the first, third and fifth elements. (See Programs 10-5 and // 10-6) return 0; } int exercise_4() { int a[] = {1,2,3,4,5,6,7,8,9}; int *n; // Assign the address of the array to the variable n. Then use a loop to // print out each element using a while loop (See Program 10-9) for (int i=0; i<9; i++) { cout << "Element " << i << " is " << "... " << endl; } return 0; } int main() { exercise_1(); exercise_2(); exercise_3(); exercise_4(); }
#include <iostream>
#define END_OF_LIST -999
using namespace std;
/*
*
*/
int exercise_1() {
int x = 100;
int *ptr;
// Assign the pointer variable, ptr, to the address of x. Then
print out
// the 'value' of x and the 'address' of x. (See Program
10-2)
ptr = &x;
cout<<"Value of x: "<<x<<"\nAddress of x:
"<<&x;
return 0;
}
int exercise_2() {
int x = 100;
int *ptr;
// Assign ptr to the address of x. Then use indirection to print
out the
// value of x using pointer variable. (See Program 10-3)
ptr = &x;
cout<<"\nValue of x using pointer variable:
"<<*ptr;
return 0;
}
int exercise_3() {
int a[] = {1,2,3,4,5,6,7,8,9};
int *n;
// Print the address of the array a,
cout<<"\nAddress of array a: "<<a;
// Notice that printing the address of an array is different then
printing
// the address of a variable.
// Assign the address of the array to the variable n. Then
de-reference n
// to print out the first, third and fifth elements. (See Programs
10-5 and
// 10-6)
n= a;
cout<<"\n1st, third, fifth array elements:
"<<*n<<","<<*(n+2)<<","<<*(n+4);
return 0;
}
int exercise_4() {
int a[] = {1,2,3,4,5,6,7,8,9};
int *n;
// Assign the address of the array to the variable n. Then use a
loop to
// print out each element using a while loop (See Program
10-9)
n=a;
for (int i=0; i<9; i++) {
cout << "\nElement " << i << " is " << "...
" ;
cout<<*(n+i);
}
return 0;
}
int main() {
exercise_1();
exercise_2();
exercise_3();
exercise_4();
}
output:
Value of x: 100 Address of x: 0x7fff404780b4 Value of x using pointer variable: 100 Address of array a: 0x7fff40478080 1st, third, fifth array elements: 1,3,5 Element 0 is ... 1 Element 1 is ... 2 Element 2 is ... 3 Element 3 is ... 4 Element 4 is ... 5 Element 5 is ... 6 Element 6 is ... 7 Element 7 is ... 8 Element 8 is ... 9