DS Lab 1 - Arrays (Static, Dynamic and Multi-Dimensional)
DS Lab 1 - Arrays (Static, Dynamic and Multi-Dimensional)
Lab-1
Development of Arrays (Static, Dynamic and Multi-dimensional) in C++
Table of Contents
1. Introduction 3
1.1 Basic Concepts about Data Structures 3
1.2 Arrays in C++ 3
1.3 Dynamic Memory Allocation 4
1.4 Multi Dimensional Arrays 4
1.5 Relevant Lecture Readings 4
4. Concept Map 5
4.1 Arrays in C++ 5
4.2 Initializing Arrays 5
4.3 Accessing values from an array 6
4.4 Static Arrays 6
4.5 Dynamic Memory Allocation in C++ 6
4.6 Multi Dimensional Arrays 8
4.6.1 Two Dimensional Arrays in C++ 8
4.6.2 Three Dimensional Arrays in C++ 10
7. Practice Tasks 13
7.5 Out comes 14
9. Evaluation criteria 14
Objective of this lab is to provide you knowledge about development of arrays in C++.
You have come to know about the basic concepts about C++ language in courses: computer
programming and object oriented programming.You havelearned about the statements used in
this language like input/output statements, selection statements (if..else, switch) and iterative
statements/loops (while, do..while, for). Further you will also have knowledge about classes,
encapsulation, inheritance and polymorphism.
A program is a set of instructions that processes data which is given as input to it. If we
need to develop a good (efficient) program, we need to organize data in computer’s memory in
effective way. This organized data is called a Data Structure.
Study of computer science includes the study of how data (information) is organized in
computer and how it can be manipulated and utilized by programs. It is extremely important for
you to understand the concepts of data (information) organization and manipulation. Data
structures for storing information are lists, stacks, queues, trees, graphs and tables.
In C++ arrays are used to store such items whose data type is same. Normally arrays are
used to represent lists, trees and graphs; which are types of data structure. We can perform
different operations on arrays such as: inserting an item, removing an item, finding a value in
array, performing mathematical operations on elements of array etc. If we want to initialize any
global or local array by assigning some values to its element we can do so. We can access value
of an element of array in our program as it was a normal variable. We are allowed to read and
modify the values of these elements.
Static arrays are allocated memory at compile time and their size is fixed, it means it
cannot be changed latter during program execution. If we want to change the size of array at
program execution time, we will need to declare dynamic arrays. A dynamic array is allocated
memory using new operator and is used in the same way as a static array. Dynamic arrays are
accessed using pointers.
Assume you have a program which is used to store information about the employees of
some organization, when this program starts its execution we are not sure that how many number
of employee records will be stored in this program. Therefore how much memory is required for
this program is dependent on the fact that how many records we will enter when program will be
executing. So memory required by program is to be estimated at program execution time, so we
will need a memory allocation mechanism which is called dynamic memory allocation for this
program, which should allocate and de allocate memory at runtime according to requirements of
program.
Some timethe data which we need to process in our program is more easily interpretable
if represented in the form of a table. A table is a structure which contains some rows and some
columns, on intersection of each row and a column some data is stored. Examples of such data
representations may be a parcel weight-location chart used by some post office; a public
transport rent chart and representation of a digital image etc. So we may easily understand this
data representation if we use a two dimensional array to represent it (remember a two
dimensional array is a special case of a multi dimensional array).
Some time data to be represented is based on more than two dimensions, for example if
we need to represent the sales of some departmental stores by product, year of sale and location
wise, it can be easily represented by a three dimensional structure, which may be logically
represented as a cube. To represent such data we will use three dimensional arrays.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.
Example:
An array named “Numbers” containing five elements of type integer (int) can be defined
as:
Figure 1 represents the logical representation of this array, which shows elements of the
array are located consecutively:
When an array is declared in a local scope, if not specified otherwise, its elements are not
initialized by some value by default. Elements of global and static arrays are automatically
initialized by their default values (zeros for numeric data types).
If we want to initialize any global or local array by assigning some values to its element
we can do so. Following example represents the initialization of an array of 5 integer elements.
Example:
For example, to store the value 25 in the third element of Numbers, we could write the following
statement:
To pass the value of the third element of Numbers to a variable called a, we could write:
a = Numbers[2];
int a[10];
int b[5] {8, 20, 25, 9, 14};
Dynamic memory allocation in C++ is performed using new and delete operators. These
operators can be used with primitive data types (int, float, char etc) as well as with user defined
data types (UDT), such as classes. Following examples illustrate usage of new and delete
operators for primitive as well as user defined data types.
int *pointer;
Above code will create a pointer variable named “pointer” which is pointing to a dynamic
variable of int type created by new operator. After the creation of variable a constant value 5 is
assigned to this variable using the pointer variable, because a dynamic variable does not have a
name to be referenced. When dynamic variable is used and is no more required, we can de
allocate its memory by using delete operator. With delete operator we use name of pointer
variable which is pointing to that dynamic variable required to be de allocated.
int size;
delete [] array;
new and delete operators can be used with a user defined type or class/struct as well.
Assume following is a class definition for which we need to create a dynamic object.
class Rectangle {
private: int x, y;
public:
voidset_values (int,int);
int area () {return (x*y);}
};
Following code segment creates and destroys a dynamic object of above described class.
A two dimensional array is a specific case of multi dimensional array in which two
subscripts are involved in definition of array. For example “Numbers” represents an array of 3
per 5 elements. The way to declare this array in C++ should be:
Indices of first subscripts are logical represented as rows and indices of second subscripts
are logically represented as columns. Now for example the way to reference the second element
vertically and fourth horizontally in an expression would be:
numbers [1][3];
Following figure 3 shows the element in the two dimensional array which is referred
above.
numbers[1][3] = 7;
To initialize a two-dimensional array, it is easiest way to use nested braces, with each set
of numbers representing a row:
int numbers[3][5] =
{{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2 };
We may initialize a two dimensional array of type int by values 0 by using following
statement. Remember that we have to specify the size of array in terms of rows and columns for
proper initialization.
int numbers[3][5] = { 0 };
Two dimensional arrays can be created and accessed using pointers to pointers.
Following code segment provides information that such array usage.
The above code segment declares an array of integer pointers to integer pointers. The first
thing we do is then make a point to an array of integer pointers. Then we have to loop through all
of those pointers and assign them to an array of ints of size width. What this does is create an
array in the size of Height x Width.
Multi dimensional arrays may be larger than two dimensions, arrays which have 3
dimensions are called multi dimensional arrays, they can be logical represented by a cube shape.
An array representing product sales for departmental stores at five cities/locations, 10 products
and containing sale information for 7 years can be declared as:
floatproductSales [5][10][7];
Where first subscript represents the location/city number, second subscript represents the
product number and third subscript represents the year number for which product sale
information will be stored in this array.
5.3.1 Task-1
Make a list of features of static and dynamic arrays in C++, which should provide
5.3.2 Task-2
Provide comparison between dynamic memory allocation schemes in C++ and Java,
provide differences and similarities.
6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.
6.2 Editing, Compiling and Running programs in Visual Studio 2008[Expected time = 5mins]
You are required to use Microsoft Visual Studio 2008 environment for programming on
data structures. It is expected that you are already familiar with this environment in your courses
of Computer Programming and Object Oriented Programming. You should be able to write code
in source code files (cpp files) and perform compilation of code. Beside that you are also
required to be proficient in performing line by line debugging and break point based debugging
to remove the errors from your code.
Following program represents the concept related to static and dynamic arrays; you are
required to type this program in editor of Visual Studio 2008, compile and run to see the output
of this program.Figure 3 shows the source code window of this program in Visual Studio 2008.
Figure 3: A program about static and dynamic arrays in Visual Studio 2008.
Output of this program is shown in figure 4, when program is compiled and executed.
Figure 6: Output window displaying values of elements of two dimensional arrays in Microsoft Visual Studio
2008.
7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and placesolution code in a
folder specified by your lab instructor.
7.2 Create a dynamic array of user defined size. Now move the largest number in the array to
first position. Print the array after moving the number. All array operations should be done using
pointers.
7.3 Write a program to create a dynamic array of user defined size. Array should be character
type. Write a function RemoveVowels() that should remove all the vowels in the array. All array
operations should be done using pointers.
7.4 Write a program to create a dynamic array of user defined size. Size should be in the
range of 0 to 15. Write a function FindLarge that should ask user to enter a non-negative number.
Function should find the next largest number than the input number in the list.
Sample input:
Enter size: 5
After insertion:
13 4 55 29 32
Sample output:
Enter number: 15
Next largest number from 15 is: 29
After completing this lab, student will be able to understand and develop programs related to
static and dynamic arraysin C++ using Microsoft Visual Studio 2008 environment.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
1. http://www.cplusplus.com/doc/tutorial/
2. http://www.learncpp.com/
3. http://www.tutorialspoint.com/cplusplus/