CS221 - Lab 10 : Pointers
Task- 1: Trace and Answer
A. Consider the following statements:
int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
After these statements, which of the following statements will change the value of i to 75?
a) k = 75;
b) *k = 75;
c) p = 75;
d) *p = 75;
e) Two or more of the answers will change i to 75.
B. Explain the error.
char c = 'A';
the error is because of the types
double *p = &c;
double *p ;
p= &c ;
Task- 2: Simple Pointers and Varaibles
Introduce int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x,
and q to the address of y. Then print the following information:
(1) The address of x and the value of x.
(2) The value of p and the value of *p.
(3) The address of y and the value of y.
(4) The value of q and the value of *q.
(5) The address of p (not its contents!).
(6) The address of q (not its contents!).
Sample Run:
Task- 3: Find the Output
Assume the definitions and initializations:
char c = 'T', d = 'S';
char *p1 = &c;
char *p2 = &d;
char *p3;
Assume further that the address of c is 6940, the address of d is 9772, and the address of e is
2224. What will be printed when the following statements are executed sequentially?
p3 = &d;
cout << "*p3 = " << *p3 << endl; // (1) *p3= S
p3 = p1;
cout << "*p3 = " << *p3 // (2) p3= T
<< ", p3 = " << p3 << endl; // (3) p3=&c =6940
*p1 = *p2;
cout << "*p1 = " << *p1 // (4) p1= s
<< ", p1 = " << p1 << endl; // (5) p1= &d=9772
Task- 4 : update variables using pointers
Write two functions updateValue1 and updateValue2 that has no return and shall update the value of two
numbers x and y as following:
• updateValue1 : subtracting 10 from x and adding 50 to y.
• updateValue2: multiplying x by 2 and dividing y by 10.
Write those two functions in two diverse ways: once using & operator and once using * operator. Then
call both functions inside the main method and compare between the usage of both functions.
whenever i have a void function with pointer i must put star within the function header and function prototype
but in the call function i must put the address
Task #5 Calculate the Body Mass Index (BMI)
The body mass index is calculated by dividing the weight (in kilograms) over the height squared (in
meters). A healthy BMI ranges between 19 and 25. Ask the user to enter their weight and height and
𝑊𝑒𝑖𝑔ℎ𝑡 (𝑘𝑚)
calculate the user’s BMI using the formula, 𝐵𝑀𝐼 = 𝐻𝑒𝑖𝑔ℎ𝑡 2 (𝑚)
and tell them their result whether they are
healthy or not healthy.
Constraints:
1- Use dynamic variables to solve this question
2- Use the below function to calculate the BMI and display the result message.
3- Delete the dynamic variables when you no longer need to use them.
4- Use the function to calculate the BMI; double BMI (double *weight, double *height );
Sample Run:
Enter your weight and height 45 1.53
healthy
Enter a positive number to continue1
Enter your weight and height 65 1.75
healthy
Enter a positive number to continue1
Enter your weight and height 45 1.80
unhealthy
Enter a positive number to continue-1
Task #6 swap dynamic variables
Write a program to swap between two integer variables which are created dynamically.