CP For Course Hero 07

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab task # 12

Computer Programming Lab Tasks

Department of Computer Science - BUIC


SECTION: BS CS (1-B)
DATE: 10TH Dec 2023

Exercises/Lab Journal 12

Task 0: Write the two ways of displaying the 4th element of an array num of type
float and size 10.

using array subscript notation: #include<iostream>


using namespace std;
int main()
{
float num[10] =
{ 1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5 };
cout << "the 4th element of the given array is " <<
num[3] << endl;
return 0;
}

using array offset notation: #include<iostream>


using namespace std;
int main()
{
float num[10] =
{ 1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5 };
cout << "the 4th element in the given array is " << *(num
+ 3) << endl;
return 0;
}

Task 1: 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) it has been assigned the address
“d” so it will print the value stored at the address “d” which is “S”.
p3 = p1;
cout << "*p3 = " << *p3 // (2) now “p3” is reassigned a value i.e.,
‘p1” which is the address of “c”
<< ", p3 = " << p3 << endl; // (3) it will print the value of “p3”
stored at address “c” which is “T” (6940)
*p1 = *p2;
cout << "*p1 = " << *p1 // (4) the value stored at the address “c”
i.e., “p1” is assigned the value of address “d” i.e., “p2”.It will print “S”.
<< ", p1 = " << p1 << endl; // (5) it will print value of “p1” is 6940
1. 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; //it will change the value of “i” by the pointer “p”. the value at
the address assigned by “p” which is the address of “i” will be set to 75.
E. Two or more of the answers will change i to 75.
2. Explain the error.
char c = 'A';
double *p = &c;
Answer:
Character variable cannot be assigned to the datatype “double”.
The datatype should be same for variable “c’ and the pointer.

Task 2: 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!).

Code:
#include <iostream>
using namespace std;
int main()
{
int x = 2;
int y = 8;

int* p = &x;
int* q = &y;

cout << "The address of x : " << p << " and the value of x : " << x << endl;
cout << "The value of p : " << p << " and the value of *p : " << *p << endl;

cout << "The address of y : " << q << " and the value of y : " << y << endl;
cout << "The value of q : " << q << " and the value of *q : " << *q << endl;

cout << "The address of p : " << &p << endl;


cout << "The address of q : " << &q << endl;
return 0;
}

Output:

Task 3: Introduce int variables x, y, z and int* pointer variables p, q, r. Set x,


y, z to three distinct values. Set p, q, r to the addresses of x, y, z respectively.
(1) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
(2) Print the message: Swapping values.
(3) Execute the swap code: z = x; x = y; y = z;
(4) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.

Task 4: Give the value of the left-hand side variable in each assignment statement.
Assume the lines are executed sequentially. Assume the address of the blocks array is
4434.
int main()
{
char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0];
char temp;

temp = blocks[0];
temp = *(blocks + 2);
temp = *(ptr + 1);
temp = *ptr;

ptr = blocks + 1;
temp = *ptr;
temp = *(ptr + 1);
ptr = blocks;

temp = *++ptr;
temp = ++*ptr;
temp = *ptr++;
temp = *ptr;
return 0;
}
Task 5: Write the output
int main ()
{
int numbers[5]; //declaring an array of name num and size
5.
int * p; //declaring a pointer of the datatype “int”.
p = numbers; //assigning address of first element of the numbers
to pointer “p”.
*p = 10; p++; //store the value of first element i.e., 10, at the
memory located assigned by pointer “p” and move pointer to the
next element
*p = 20; //same process is repeated, i.e., assigning and storing
value to a new memory location.
p = &numbers[2]; //it will move the pointer to the next element in the
array.
*p = 30; //storing the new value
p = numbers + 3; //moving to the next element
*p = 40; //storing the new value
p = numbers; //moving back to the beginning of first element of array.
*(p+4) = 50; //moving to the 5th and last element of the array.
for (int n=0; n<5; n++)
cout << numbers[n] << ", "; //it will print all elements present in the
array.
return 0;
}

Output:
10, 20, 30, 40, 50

You might also like