0% found this document useful (0 votes)
2 views

DSA Lab 01 - Arrays in C++

The document provides an overview of arrays in C++, covering static and dynamic arrays, memory allocation, and multi-dimensional arrays. It explains how to declare, initialize, and access array elements, as well as the use of dynamic memory allocation with examples. Additionally, it includes lab tasks focused on implementing array-related problems and a rubric for evaluating the lab work.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSA Lab 01 - Arrays in C++

The document provides an overview of arrays in C++, covering static and dynamic arrays, memory allocation, and multi-dimensional arrays. It explains how to declare, initialize, and access array elements, as well as the use of dynamic memory allocation with examples. Additionally, it includes lab tasks focused on implementing array-related problems and a rubric for evaluating the lab work.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 01: Arrays in C++

Outcomes
● Array data structure in C++
● Static and Dynamic Arrays

1. Introduction
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.

1.1 Dynamic Memory Allocation


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 deallocate memory at runtime according to
requirements of program.

1.2 Multi-Dimensional Arrays


Sometimes the 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).

1
Sometimes 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.

2. Concept Map
This concept map will help students to understand the main concepts of topic
covered in lab.

2.1 Arrays in C++


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 may 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.
Example:
An array named “Numbers” containing five elements of type integer (int) can be

defined as: int Numbers [5];

Figure 1 represents the logical representation of this array, which shows elements
of the array are located consecutively:

Figure 1: Logical representation of an array of integer elements

2.2 Initializing Arrays


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

2
Example:
int Numbers [5] = { 16, 2, 77, 40,
12071 }
OR
int Numbers [] = { 16, 2, 77, 40, 12071 };
Figure 2 represents the above initialized array with values.

Figure 2: An array initialized by values

2.3 Accessing values from an array


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.
Following the previous examples in which Numbers array has 5 elements and each
of those elements was of type int, the name which we can use to refer to each
element is the following:
For example, to store the value 25 in the third element of Numbers, we could write
the followingstatement:
Numbers [2] = 75;
To pass the value of the third element of Numbers to a variable called a, we

could write:a = Numbers[2];

2.4 Static Arrays


Static arrays are allocated memory at compile time and their size is fixed, it
means it cannot be changed latter during program execution. For example two int
arrays are declared, one initialized and one without initialization.
int a[10];
int b[5] = {8, 20, 25, 9, 14};

2.5 Dynamic Memory Allocation in C++


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.
For int data type, new operator can be used as:
int *pointer;

pointer = new
int;

*pointer = 5;
delete 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.
We may allocate and de allocates memory space dynamically to an array
of integers.
Following example illustrates this
concept.

int size;
cout<< "Enter size of
array: ";cin>> size;

int* array = new int[size];

for(int i = 0; i < size; i++)


{
array[i] = i+1;
cout<< array[i] << " ";
}

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:
void set_values
(int,int); int area
() {return (x*y);}
};

void Rectangle::set_values (int


a, int b)
{
x = a;
y=b
}

Following code segment creates and destroys a dynamic object of above


described class.
Rectangle *ptr = new Rectangle();
ptr->set_values(3,5);cout<<ptr->area();
delete ptr
Lab Tasks:
1. Given an array of integers nums, calculate the pivot index of this array. The
pivot index is the index where the sum of all the numbers strictly to the left of
the index is equal to the sum of all the numbers strictly to the index's right.
2. You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as much as
every other number in the array. If it is, return the index of the largest element,
or return -1 otherwise.
3. You are given a large integer represented as an integer array digits, where
each digits[i] is the ith digit of the integer. The digits are ordered from most
significant to least significant in left-to-right order. The large integer does not
contain any leading 0's. Increment the large integer by one and return the
resulting array of digits.
4. Write a program in C++ capable of storing data of 100 student’s name, father
name, degree, courses registered and registration number. The program
should be capable of storing data, updating data of any student, deleting data
of any student and display the data of any student.
 How can you save the data of students?
 Enter data of 2 students (name, roll_no, degree, marks of 5 subjects) take
this data from user.
 Then print out in how many subject student is pass or fail. (Passing marks
is 50)
 Also print if student is pass or fail overall. (Passing marks is 50)
 Use Functions and arrays where needed
Lab # 01 Marks distribution
ER1 ER6 ER8

Task 3 points 3 points 4 points

Lab # 01 Rubric Evaluation Guideline:


# Qualities & 0 < Poor <= 1 1 < Satisfactory <= 2 2 < Excellent <=3
Criteria
ER1 Task Completion Minimal or no program Some tasks were completed, All tasks were completed,
functionality was but the program has errors or and the program runs
achieved. incomplete functionalities. without errors.
# Qualities & 0 < Poor <= 1 1 < Satisfactory <= 2 2 < Excellent <=3
Criteria
ER6 Program Output Output is inaccurate or Output is mostly accurate but Output is clear, accurate,
poorly presented. may lack labels, captions, or and well presented with
formatting. labels, captions, and proper
formatting.
# Qualities & 0 < Poor <= 1.5 1.5 < Satisfactory <= 3 3 < Excellent <= 4
Criteria
ER8 Question & Answers some questions Answers most questions Answers all questions
Answer but not confidently or confidently and based on lab confidently and
based on lab task task knowledge. demonstrates a deep
knowledge. understanding of the given
lab task.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy