Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

TMF1434 Data Structure and Algorithms 1

LAB 3

TMF1434 Data Structure and Algorithms


Semester 2, 2022/2023
LAB03: Pointers & Arrays

A. Arrays

1. Simple Array

#include <iostream> //header file


using namespace std;

int main () // main function


{
int sample [10]; // this reserves 10 integer elements
int num; // variable declaration

// load the array


for (num =0; num <10; ++num)
sample[num]= num;

// display the array


for (num =0; num <10; ++num)

cout << sample[num] << ' ';

return 0;
}

Exercise :

a) Modify the program above by utilizing a constant variable “SIZE” to define the size of the array to
value “10” and making use of this constant value throughout your program.

b) Create a new array of type integer named as “myZeroArray” with number of elements as many as your
constant “SIZE” defined earlier and initial with zero “0” value to all elements in this array in a single
statement. Display this array.

c) Create another new array named as “myOtherArray” and initial with 3 different integer values in this
array. Think of a way to display the content of this array without specify value “3” as the number of
elements in this array. (Hint: this array is containing int data type in each element)
TMF1434 Data Structure and Algorithms 2
LAB 3

2. Multidimensional Array
Declaring and Accessing Elements in a Multidimensional Array. Copy the code below and study how
multi-dimensional array is implemented.

#include <iostream>
using namespace std;

int main()
{
int TwoDArray [3][3] = {{-501, 206, 2011},
{989, 101, 206},
{303, 456, 596}};

cout << "Row 0: " << TwoDArray [0][0] << " "
<< TwoDArray [0][1] << " "
<< TwoDArray [0][2] << endl;

cout << "Row 1: " << TwoDArray [1][0] << " "
<< TwoDArray [1][1] << " "
<< TwoDArray [1][2] << endl;

cout << "Row 2: " << TwoDArray [2][0] << " "
<< TwoDArray [2][1] << " "
<< TwoDArray [2][2] << endl;

return 0;
}

B. POINTERS
1. Single pointer (Single indirection)
#include <iostream>
using namespace std;

int main()
{
int balance;
int *balptr;
int value;

balance = 3200;
balptr = &balance;
value = *balptr;

cout << "balance is: " << value << '\n';


return 0;
}

Exercise :

Modify the program, create a new pointer of type int named it as “newBalance” and copy the content of
balptr to this pointer. Print out the contents of both balptr and newBalance as well the value of their
references.
TMF1434 Data Structure and Algorithms 3
LAB 3

2. A Demonstration of Bad Programming Using Invalid Pointers


(CAUTION: Save all your works before trying out this program)

#include <iostream>
using namespace std;
int main()
{
// uninitialized pointer (bad)
int* pTemperature;
cout << "Is it sunny (y/n)?" << endl;
char UserInput = 'y';
cin >> UserInput;

if (UserInput == 'y')
{
pTemperature = new int; //Dynamic memory allocation
*pTemperature = 30;
}

// pTemperature contains invalid value if user entered ‘n’


cout << "Temperature is: " << *pTemperature;
//delete also being invoked for those cases new wasn’t done
delete pTemperature;
return 0;
}

3. Passing pointer to function

Passing a pointer to a function. Copy and study the code below.

#include <iostream>
using namespace std;
void f(int *j);
int main()
{
int i;
int *p;
p = &i; // p now points to i
f(p);
cout << i; // i is now 100
return 0;
}

void f(int *j)


{
*j = 100; // var pointed to by j is assigned 100
}
TMF1434 Data Structure and Algorithms 4
LAB 3

4. Calling function with array


Run and study how to implement calling function with array.

#include <iostream>
using namespace std;
void display(int num[10]);
int main()
{
int t[10],i;
for(i=0; i<10; ++i)
t[i]=i;

display(t); // pass array t to a function


return 0;
}

// Print some numbers.


void display(int num[10])
{
int i;
for(i=0; i<10; i++)
cout << num[i] << ' ';
}

5. Calling function with pointers


In the following program, examine the function cube( ), which converts the value of each element in an
array into its cube. To call cube( ), pass the address of the array as the first argument, and the size of the
array as the second.

#include <iostream>
using namespace std;
void cube(int *n, int num);

int main()
{
int i, nums[10];
for(i=0; i<10; i++) nums[i] = i+1;
cout << "Original contents: ";

for(i=0; i<10; i++) cout << nums[i] << ' ';


cout << '\n';
cube(nums, 10); // compute cubes
cout << "Altered contents: ";

for(i=0; i<10; i++)


cout << nums[i] << ' ';
return 0;
}

void cube(int *n, int num)


{
while(num) {
*n = *n * *n * *n;
num--;
n++;
}
}
TMF1434 Data Structure and Algorithms 5
LAB 3

Here is the output produced by this program:

Original contents: 1 2 3 4 5 6 7 8 9 10
Altered contents: 1 8 27 64 125 216 343 512 729 1000

You might also like