(Abstract Data Types Using Arrays) : Fast NUCES - Department of Computer Science
(Abstract Data Types Using Arrays) : 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.
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.
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.
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.
#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;
return 0;
}
#include <iostream>
// M x N matrix
#define M 4
#define N 5
delete[] A;
return 0;
}
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:
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 StringADT.cpp file -> containing the implementation of all the functions defined in header file.
(Make sure to include StringADT.h file in this file).
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:
Question 5: