LAB ACTIVITY 4B: ARRAY AND POINTER
Duration: 4 Hours
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 : 105 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;
Memory address
Variable name
Variable stored value
2. Type the programs given below and trace the output.
a)
#include <iostream> using namespace std;
int main()
int x; int *p;
// A normal integer
// A pointer to an integer
p = &x; // Read it, "assign the address of x to p"
cout<<"Insert a value:"; cin>> x;
cout<< *p <<"\n"; system("pause");// Put a0;
return value in x, we could also use *p here
// Note the use of the * to get the value
Output:
b)
#include <iostream>
using namespace std; int main ()
intvar = 20;// actual variable declaration. int*ip;// pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: "; cout << var << endl;
// print the address stored in ip pointer variable cout << "Address stored in ip varia
cout << ip << endl;
// access the value at the address available in pointer cout << "Value of *ip variable
cout << *ip << endl; system("pause"); return 0;
Output:
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:
Activity 4B(ii)
Explain new and delete operator.
Duration : 30 minutes
Type the programs given below and trace the output.
#include <iostream> #include <cstring> using namespace std;
int main()
int n;
cout << "Enter total number of students: ";
cin >> n; float* ptr;
ptr = new float[n]; // memory allocation for n number of floats
cout << "Enter GPA of students." <<endl; for (int i = 0; i < n; ++i)
cout << "Student" << i+1 << ": "; cin >> *(ptr + i);
cout << "\nDisplaying GPA of students." << endl; for (i = 0; i < n; ++i)
cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
delete [] ptr;// ptr memory is released system("pause");
return 0;}
Output:
Activity 4B(iii)
Solve the given scenario by writing a program using array and pointer.
Duration : 105 minutes
SCENARIO:
After study all the examples of program that use pointer and array, Suria decided to upgrade
the payroll system using array and pointer. Help Suria to change the process of input and
storing salaries into array by using pointer.
Procedure :
Step 1: Edit the program given below by adding the code to store OT payment using array
and access value using pointer for payroll system.
#include <iostream.h>
#define NUM 10
void main()
{
int salary[NUM] = {2000, 3400, 1900, 2500, 3300, 1238, 3200, 2700,
3600, 4500};
int *ptr;
ptr = salary;
cout<<"Staff ID \tSalary"<<endl;
for (int i=0; i<NUM; i++)
{
cout<<i<<"\t\t"<<*ptr<<endl;
ptr++;
}
}
Step 2: Compile the program.
Step 3: Write the output.
Step 4: Write the output.
Step 5: Save the program as