You are on page 1of 4

University of Wah

Department of Computer Science

Data Structures and Algorithm (CS-201)


Lab Manual 3

Goals for today’s lab:

 2-D Arrays
 Structure and pointers
1. 2-D Arrays

 The two-dimensional array can be defined as an array of arrays.


 The 2D array is organized as matrices which can be represented as the collection of rows
and columns.
 However, 2D arrays are created to implement a relational database lookalike data
structure. It provides ease of holding the bulk of data at once which can be passed to any
number of functions wherever required.

 The syntax to declare a 2-D array is:


Datatype array_name[no. of rows][no. of columns];

Task 1:
Write a program that first inputs rows and columns to apply the multiplication operation on two
matrices.

The output of the program will look like this:

Output
2. Structures and Pointer
 A structure is a user-defined data type in C/C++. A structure creates a data type that can
be used to group items of possibly different types into a single type.

 The ‘struct’ keyword is used to create a structure. The general syntax to create a structure
is as shown below:

struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};
How to access the members of a structure?
 Using objects of structure (dot (.) operator)
structureName s1;
s1.member2;
 Using pointer variables (-> operator)
structureNAME *ptr= new structureName;
ptr -> member2;

Task1:

Write a program that take input values from the user swaps the values with the help of pointers.

Output

Task 2:

Write a program to initialize a void pointer, pass address of a float value to pointer and print the
address of float value and the pointer

Task 3:
Write a C++ program to make a structure of a student consisting of integer age, char name and
structure roll number that further divides into department, session, registration number and
degree. Set and display all the values from main function and display the record on screen to
present the access of structure within structure.

Output:
Task 4:
Modify above stated program by accessing structure using pointer type variable of structure.
Output

Task 5:
Write a program using structures that first inputs the data for 3 employees including their names,
emp-ids and salary. Apply linear search on this data to search the employee with his name and
display its further record on screen.

You might also like