0% found this document useful (0 votes)
4 views37 pages

Arrays

The document provides an overview of data structures and algorithms, focusing on arrays and pointers in programming. It explains the definition of data structures, basic types like linked lists and stacks, and details on declaring, creating, and manipulating arrays. Additionally, it covers pointer operations, arithmetic, and their relationship with arrays in C programming.

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

Arrays

The document provides an overview of data structures and algorithms, focusing on arrays and pointers in programming. It explains the definition of data structures, basic types like linked lists and stacks, and details on declaring, creating, and manipulating arrays. Additionally, it covers pointer operations, arithmetic, and their relationship with arrays in C programming.

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Data Structures Algorithms and Applications

Prepared by:

ENGR. VANESH KUMAR


Lecturer, Computer Science Deptt:
Thar Institute of Engineering, Sciences & Technology
What is Data Structures?
• Example:library
• is composed of elements
(books)
• Accessing a particular book
requires knowledge of the
arrangement of the books
• Users access books only
through the librarian
the 7ogico7 om•ongement of data
elements, combined with
the set ofoperafions w e need to access
the elements.
0rdcr¢d by s bj rt
Basic Data Structures
• Structures include
▫linked lists
▫Stack, Queue
▫binary trees
▫…and others
What is Algorithm?
• Algorithm:
▫A computable set of steps to achieve a desired result
▫Ralationship to Data Structure
Example: Find an element

2 4 6

1 3 5 7

1 2 3 4 5 6 7
Introducing Arrays
Array is a data structure that represents a collection
of the same types of data.
double[] myList = new double[10];

myList reference myList[0]


myList[1]
myList[2] An Array of 10
myList[3] Elements
myList[4] of type d o u b l e
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
Declaring Array Varia les
• datatype[] arrayname;
Example:
double[] myList;

• datatype arrayname[];

Example:

double myList[];
Creating Arrays
arrayName = new datatype[arraySize];

Example:
myList = new double[10];

myList[0] references the first element in the array.


myList[9] references the last element in the array.
Declaring and Creating in ne Ste
• datatype[] arrayname = new
datatype[arraySize];

double[] myList = new double[10];

• datatype arrayname[] = new


datatype[arraySize];

double myList[] = new double[10];


The Length of Arrays

•Once an array is created, its size is fixed. It


cannot be changed. You can find its size using

arrayVariabIe.iength

For exampie,
myList.length returns
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] =i;

• Declaring, creating, initializing in one step:


double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand syntax must be in one statement.


Declaring, creating, initiaiizing Usingthe
Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5);

This shorthand notation is equivalent to the following


statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] =2.9;
myList[2] =3.4;
myList[3] =3.5;
CAUTI
Using the shorthand notation, you have to
deciare, create, and initialize the array aii in
one statement. Spiitting it wou cause a syntax
error. For example, the foiiowing is wrong:
double[] myList;

myList = {1.9, 2.9, 3.4, 3.5};


Testing Arrays

• Objective: The program receives 6 numbers from


the keyboard, finds the largest number and
counts the occurrence of the largest number
entered from the keyboard.
Suppose you entered 3, 5, 2, 5, 5, and 5, the
largest number is 5 and its occurrence count is 4.

TestArray Run
Assigning Grades

• Objective: read student scores (int) from the


keyboard, get the best score, and then assign
grades based on the following scheme:
• Grade is A if score is >= best—10;
• Grade is B if score is >= best—20;
• Grade is C if score is >= best—30;
• Grade is D if score is >= best—40;
° Grade is F otherwise.
Passing Arrays as Arguments

•Objective: Demonstrate ifferences of


passing primitive data type variables
and array variables.

TestPassArra
Example
swap(a[0], a[1]) a[0] 1 a[1] 2

Pass by value

swap( nl, n2) n1 1 n2 2

swapFirstTwoInArray(a) a Referenc a[0]


a[1]
ass by value (Reference value)

swapFirstTwoInArray(array) array Referenc


Copying Arrays
In this example, you will see that a simple
assignment cannot copy arrays in the
following program. The program simply
creates two arrays and attempts to copy one
to the other, using an assignment statement.

TestCopyArray Run
Copying Arrays
Before the assignment After the assignment
list2 = list1; list2 = list1;

list1 listl
Contents Contents
of listl of listl

list2 list2
Contents Contents
of list2 of list2
Garbage
Copying Arrays

Using aloop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];

for (int i = 0; i < sourceArrays.length; i++)


targetArray[i] = sourceArray[i];
ultidimensional Arrays
Declaring Variables of Multidimensional Arrays and Creating
Multidimensional Arrays
int[][] matrix = new int[10][10];
OR
int matrix[][] = new int[10][10];
matrix[0][0] =3;

for (int i=0; i<matrix.length; i++) for


(int j=0; j<matrix[i].length; j++)
matrix[i][j] =(int)(Math.random()*1000);

double[][] x;
ultidimensional Array Illustration

0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3

1 1 1 4 5 6

2 2 2 7 8 9

3 3 3 10 11 12

4 4 int[][] array = (
{1, 2, 31,
matrix = new int[5][5]: matrix[2] [1] = 7; {4, 5, 6},
t 7, 8, 9 j,
10, 11, 12
Declaring, Creating, and Initializing Using Shorthand
Notations
You can also use a shorthand notation to declare, create and initialize a two-dimensional
array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}

This is equivalent to the following statements:


int[][] array = new int[4][3];
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
Lengths of ultidimensional Arrays
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}

array.length
array[0].length
array[1].length
array[2].length
Definition Pointer

O 1 > 3 4 5 6 7 8 9 10 11

• A value indicating the number of (the first byte of)


a data object
• Also called an Address or a Location
• Used in machine language to identify which data
to access
• E.g., stack pointer is address of most recent entry of The Stack
• Usually 2, 4, or 8 bytes, depending upon machine
architecture
emory Addressing
oxFFFFFFFF
stack
(dynamically allocated)

heap
address space (dynamicall

code
PC
OXOOOOOOOO
(text)
Declaring Pointers in
• int *p; a pointer to an int
• double *q; a pointer to a double
• char **r; a pointer to a pointer to a char

• type *s; a pointer to an object of type type

• E.g, a struct, union, function, something defined by a typedef, etc.


Declaring Pointers in (continued)
• Pointer declarations:—read from right to left
• const int *p;
• p is a pointer to an integer constant
• I.e., pointer can change, thing it points tocannot
• int *const q;
• q is a constant pointer to an integer variable
• i.e., pointer cannot change, thing it points to can!
• const int *const r;
• r is a constant pointer to an integer constant
Pointer Operations in C++
• Creation
&variableReturns variable’s memory address
• Dereference
*pointerReturns contents stored at address
• Indirect assignment
•pointer=valStores value at address
• Of course, still have...
• Assignment
pointer=ptrStores pointer in another variable
sing Pointers
int i1;
int i2;
int tr1; 0x1014
int tr2;
0x1010

i1= 1; 0x100C
i2 = 2;
0x1008

ptrl = Ai1; 0x1004


ptr2 = ptrl; 0x1000

*ptrl = 3;
i2 =*ptr2;
Pointer Arithmetic
• int *p, *q;
q = p +1;
° Construct a pointer to the next integer after *p and
assign it to q
• double *p, *r;
int n;
r = p + n;
• Construct a pointer to a double that is n doubles beyond
*p, and assign it to r
• n may be negative
Pointer Arithmetic (continued)
• float *p, *q;
int n;
n = p - q;
° n is the number of floats between *p and *q; i.e., what
would be added to q to get p
Pointer Arithmetic
pointer + number pointer —number

E.g., pointer + 1 adds 1 something to a pointer


char
char
char b; b;

In each, p now points to b


(Assuming compiler doesn’t
reorder variables in memory)
Adds 1*sizeof(char) to Adds 1*sizeof(int) to
t h e memory address the memory address

Pointer arithmetic should be used cautiously


Pass-by-Reference
set_x_and y(int *x,

*x= 1001; a
*y = 1002 ;
b

int b 2;
set x and y(&a, b);
Arrays and Pointers
• Arrays and pointers are closely related in C
• In fact, they are essentially the same thing!
• Esp. when used as parameters of functions
• int A[10];
int *p;
• Type of A is int *
• p = A; and A = p; are legal assignments
° *p refers toA[O]
*(p + n) refers to A[n]
• p = &A[5]; is the same as p = A + 5;
Arrays and Pointers
• double A[10]; vs. double *A;
• Only difference:—
° double A[10] sets aside ten units of memory, each large
enough to hold a double
° double *A sets aside one pointer-sized unit of memory
• You are expected to come up with the memory elsewhere!
° Note:— all pointer variables arethe same size in any given
machine architecture
• Regardless of what types they point to
ote
• C does not assign arrays to each other
• E.g,
• double A[10];
double B[10];

A = B;
• assigns the pointer value B to the pointer value A
• Contents of array A areuntouched
Arrays as Function Parameters
• void init(float A[], int arraysize);
void init(float *A, int arraysize);

• Are identical function prototypes!


• Pointer is passed by value i.e. caller copies the value of a
pointer to float into the parameter
• A Called functioncan reference through that pointer to reach
thing pointed to.

You might also like