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

Lec02 (Week 1) - CS 232 - Data Structure

Uploaded by

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

Lec02 (Week 1) - CS 232 - Data Structure

Uploaded by

bwork4758
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ARRAYS

Instructor: Abid Ali


WHAT IS AN ARRAY?
 An Array is a type of Non Primitive Linear type of Data Structure.
 Array is a static Data Structure.
 An array is a collection of items stored at contiguous memory locations.
 The idea is to store multiple items of the same type together.
 Array is a container which can hold a fix number of items and these items
should be of the same type.
 Mostly Array are used for implementation of certain algorithms.
 Following are the important terms that we need to know in order to
understand Array
 Element: Each item stored in an array is called an element.
 Index: Each location of an element in an array has a numerical index, which is used
to identify the element.
ARRAY REPRESENTATION
WHY DO WE NEED ARRAY?
 Arrays are best for storing multiple values in a single variable
 Arrays are better at processing many values easily and quickly
 Sorting and searching the values is easier in array
LET’S CREATE AN ARRAY IN
C++
 Declaration of Array
 we can declare an array by the help of following syntax

 Syntax
 dataType arrayName[arraySize];

 Example
 Declaration Initialization Array Items
Syntax
dataType arrayName[arraySize] = {arrayItems};
Example
HOW TO ACCESS A SPECIFIC
ARRAY VALUE
 Syntax
ArrayName[indexNum]
 Example
 The Output will be
 200
USING LOOP IN AN ARRAY
THE OUTPUT WILL BE
ARRAY OPERATIONS IN
C++
 In C++ you have to program the logic yourself for performing the insert,
delete, search update and traverse operations on C++ arrays.
Insert
 The logic for insertion operation goes as follows:
 loop through the array items
 shift them to a greater index
 add a new array item at a given index
 In the following example, we have 5 items in the “my1starray” array, and
we want to add a new item just after the value of 200. This means we have
to shift all the items after 200 to a greater index, and then insert our new
value of 150.
THE OUTPUT WILL BE

You might also like