Data Structures
Fast NUCES –Department of Computer Science
(Abstract Data Types Using Arrays)
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
Pre-Lab Activity:
Question-01
Write a program in C++ to take two arrays from user may be of same or different
sizes, combine them in to a single array such that the resulted array will be the
sorted one.
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
Abstract Data Types :( ADTs)
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented. It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It is called “abstract” because it
gives an implementation-independent view. The process of providing only the essentials and
hiding the details is known as abstraction.
The user of data type does not need to know how that data type is implemented, for example, we have
been using Primitive values like int, float, and char data types only with the knowledge that these data
type can operate and be performed on without any idea of how they are implemented. So a user only
needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box
which hides the inner structure and design of the data type.
Array as Abstract Data Type:
The array is an abstract data type (ADT) that holds a collection of elements accessible by an index. The
elements stored in an array can be anything from primitive types such as integers to more complex types
like instances of classes. An element is stored in a given index and they can be retrieved at a later time by
specifying the same index. The Array (ADT) have one property, they store and retrieve elements using an
index. The array (ADT) is usually implemented by an Array (Data Structure).
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
The way indices work is specific to the implementation, but you can usually think of them as the slot
number in the array that the value occupies.
Following are the various operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − add an element at given index.
Deletion − delete an element at given index.
Search − search an element using given index or by value.
Update − update an element at given index.
1D-Array:
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its
elements involves a single subscript which can either represent a row or column index.
Example:
int anArrayName [10];
It declares a one-dimensional array of ten integers.
2D-Array:
Like a 1D array, a 2D array is a collection of data cells, all of the same type, which can be given
a single name. However, a 2D array is organized as a matrix with a number of rows and
columns.
Example:
int disp [2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
Or
int disp [2][4] = {10, 11, 12, 13, 14, 15, 16, 17};
It declares and initialize a two-dimensional array of 2*4 integers.
Dynamic Memory Allocation for arrays:
Memory in your C++ program is divided into two parts −
The stack − All variables declared inside the function will take up memory from the stack.
The heap − this is unused memory of the program and can be used to allocate the memory
dynamically when program runs.
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
Single Dimensional Array:
#include <iostream>
#define N 10
// Dynamically Allocate Memory for 1D Array in C++
int main()
{
// dynamically allocate memory of size N
int* A = new int[N];
// assign values to allocated memory
for (int i = 0; i < N; i++)
A[i] = i + 1;
// print the 1D array
for (int i = 0; i < N; i++)
std::cout << A[i] << " "; // or *(A + i)
// deallocate memory
delete[] A;
return 0;
}
Two Dimensional Array
1. Using Single Pointer
In this approach, we simply allocate one large block of memory of size M*N dynamically and
assign it to pointer. Then we can use pointer arithmetic to index 2D array.
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
#include <iostream>
// M x N matrix
#define M 4
#define N 5
// Dynamically Allocate Memory for 2D Array in C++
int main()
{
// dynamically allocate memory of size M*N
int* A = new int[M * N];
// assign values to allocated memory
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
*(A + i*N + j) = rand() % 100;
// print the 2D array
for (int i = 0; i < M; i++)
{for (int j = 0; j < N; j++)
std::cout << *(A + i*N + j) << " "; // or (A + i*N)[j])
std::cout << std::endl;
}// deallocate memory
delete[] A;
return 0;
}
2. Using Array of Pointers
We can dynamically create array of pointers of size M and then dynamically allocate memory of size N
for each row as shown below.
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
#include <iostream>
// M x N matrix
#define M 4
#define N 5
// Dynamically Allocate Memory for 2D Array in C++
int main()
{
// dynamically create array of pointers of size M
int** A = new int*[M];
// dynamically allocate memory of size N for each row
for (int i = 0; i < M; i++)
A[i] = new int[N];
// assign values to allocated memory
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
A[i][j] = rand() % 100;
// print the 2D array
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
std::cout << A[i][j] << " ";
std::cout << std::endl;
}
// deallocate memory using delete[] operator
for (int i = 0; i < M; i++)
delete[] A[i];
delete[] A;
return 0;
}
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
Safe Array:
In C++, there is no check to determine whether the array index is out of bounds. During
program execution, an out-of-bound array index can cause serious problems. Also, recall
that in C++ the array index starts at 0.
Safe array solves the out-of-bound array index problem and allows the user to begin the
array index starting at any integer, positive or negative.
"safely" in this context would mean that access to the array elements must not be out of range. ie. the
position of the element must be validated prior to access. For example in the member function to allow
the user to set a value of the array at a particular location:
1 struct safe_array
2{
3 int set( int value, int pos )
4 {
5 if( pos < 0 || pos >= size )
6 {
7 // error, out of range access
8 }
9 else a[pos] = value ;
10 }
11
12 // ...
13 int a[100] ; // can store upto a maximum of 100 elements
14 int size ; // number of actual elements ( 0 < size < 101 )
15 };
Jagged Array:
A Jagged array is also multi-dimensional array, comprising arrays of varying sizes
as its elements (rows).it also referred as ragged array.
Jagged array in memory:
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
Exercise
Question 1:
Write a program that will read 10 integers from the keyboard and place them in an array.
The program then will sort the array into ascending and descending order and print the sorted list.
The program must not change the original array or create any other integer arrays.
Hint: It requires two pointer arrays.
Question 2:
Write a program that will read n integers from the keyboard and place them in a jagged array as shown.
Question 3:
Create a StringADT.h file -> containing the definition of all the required functions
Create a [Link] file -> containing the implementation of all the functions defined in header file.
(Make sure to include StringADT.h file in this file).
Implement and Test all the functions and Requirements.
o Is Integer (checks if the string is an integer?)
o To Integer (converts a string in to an Integer)
o Read String (Reads a string and Returns it)
o Concatenate (Concatenate two string if not too long)
o String Greater (Checks if the first string is greater)
o String Smaller (Checks if the first string is smaller)
o String Equal (Checks if the strings are equal)
Question 4:
Write a program to do following operation on two dimensional array A of size m x n. You should use
user-defined functions which accept 2-D array A, and its size m and n as arguments. The options are:
By: Ammara Yaseen
Data Structures
Fast NUCES –Department of Computer Science
To input elements into matrix of size m x n
To display elements of matrix of size m x n
Sum of all elements of matrix of size m x n
To display row-wise sum of matrix of size m x n
To display column-wise sum of matrix of size m x n
To create transpose of matrix B of size n x m
Question 5:
Write user defined functions for square matrix to calculate
1. Left diagonal sum
2. Right diagonal sum
By: Ammara Yaseen