CS221 Lab Pointers
CS221 Lab Pointers
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.
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:
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:
Sample Run:
Write a program to swap between two integer variables which are created dynamically.