Documente Academic
Documente Profesional
Documente Cultură
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 4B(i), 4B(ii) and 4B(iii).
Declare pointer
Assign the address of variable to pointer
Manipulate the value of variables using pointer
Explain new and delete operator
Design, write, run, test and debug program using pointer
SCENARIO:
Suria found that by using a pointer it is easier to do the input output process and access to the
data can be done efficiently. So Suria decides to change the process of input and storing
salaries into array by using pointer. But Suria still not yet proficient in the use of pointer. Suria
should put more effort to learn topics related to pointer. Therefore Suria has taken steps to study
examples of programs that use pointers.
Activity 4B(i)
Declaring a pointer, assigning the address of variable and manipulate the value of variables
using pointer.
Duration : 90 minutes
1. Fill in the blank (memory block) if one variable named rate of type integer and assign an
initial value as follows:
0|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
a)
1|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
b)
2|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
3. For each of the following, write a program that performs the indicated task.
a) Declare the variable fptr to be a pointer to an object of type float.
b) Declare the floating point variables num1 and num2.
c) Assign 100.20 to num1 as initial value.
d) Assign the address of variable num1 to pointer variable fptr.
e) Print the value of object pointed to by fptr.
f) Assign the value of the object pointed to by fptr to variable num2.
g) Print the value of num2.
h) Print the address of num1.
i) Print the address stored in fptr.
Your program:
#include<iostream>
using namespace std;
int main()
{
float *kptr;
float num1,num2;
num1=100.20;
kptr=&num1;
num2=*kptr;
3|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
4|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
Activity 4B(ii)
Explain new and delete operator.
Duration : 30 minutes
5|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
Activity 4B(iii)
The following code illustrates how to declare and initial values into a one dimensional array, and
access its elements by using a pointer.
6|Page
DFC 2073 PROGRAMMING FUNDAMENTALS
7|Page