0% found this document useful (0 votes)
40 views

CS221 Lab Pointers

Here are the steps to swap two integer variables created dynamically: 1. Declare two integer pointer variables to hold the addresses of the dynamically allocated integers: int *p1, *p2; 2. Dynamically allocate memory for two integers and assign their addresses to the pointer variables: p1 = new int; p2 = new int; 3. Assign values to the dynamically allocated integers: *p1 = 10; *p2 = 20; 4. Declare a temporary integer variable to hold one value during swap: int temp; 5. Store the value pointed to by p1 in temp: temp = *p1

Uploaded by

momo26.mn0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CS221 Lab Pointers

Here are the steps to swap two integer variables created dynamically: 1. Declare two integer pointer variables to hold the addresses of the dynamically allocated integers: int *p1, *p2; 2. Dynamically allocate memory for two integers and assign their addresses to the pointer variables: p1 = new int; p2 = new int; 3. Assign values to the dynamically allocated integers: *p1 = 10; *p2 = 20; 4. Declare a temporary integer variable to hold one value during swap: int temp; 5. Store the value pointed to by p1 in temp: temp = *p1

Uploaded by

momo26.mn0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

You might also like