Você está na página 1de 8

DFC 2073 PROGRAMMING FUNDAMENTALS

LAB ACTIVITY 4B: ARRAY AND POINTER

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 4B(i), 4B(ii) and 4B(iii).

By the end of this practical session, you should be able to :

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

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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:

int rate = 100;

0|Page
DFC 2073 PROGRAMMING FUNDAMENTALS

2. Type the programs given below and trace the output.

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;

cout<<"Value of object pointed to by kptr : " <<*kptr<<endl;

num2=*kptr;

cout<<"Value of number2 : "<<num2<<endl;


cout<<endl;
cout<<"Address of number1 : "<<&num1<<endl;
cout<<"Address stored in kptr :"<<&kptr<<endl;

3|Page
DFC 2073 PROGRAMMING FUNDAMENTALS

4|Page
DFC 2073 PROGRAMMING FUNDAMENTALS

Activity 4B(ii)
Explain new and delete operator.
Duration : 30 minutes

Type the programs given below and trace the output.

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.

Step 1: Type the program given below:

Step 2: Compile the program.


Step 3: Write the output.

6|Page
DFC 2073 PROGRAMMING FUNDAMENTALS

7|Page

Você também pode gostar