Lab Activity 4B: Array and Pointer: Duration: 4 Hours
Lab Activity 4B: Array and Pointer: Duration: 4 Hours
Duration: 4 Hours
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 : 105 minutes
1. Fill in the blank (memory block) if one variable named rate of type integer and assign an
initial value as follows:
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()
Output:
b)
#include <iostream>
using namespace std; int main ()
intvar = 20;// actual variable declaration. int*ip;// 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
int main()
int n;
cout << "Enter total number of students: ";
cin >> n; float* ptr;
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;
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++;
}
}