0% found this document useful (0 votes)
6 views18 pages

chapter#04

The document provides an overview of arrays as a linear data structure, detailing their characteristics, types (one-dimensional and two-dimensional), and operations such as traversing, inserting, searching, and sorting. It explains how arrays store similar data types in contiguous memory locations and includes examples of array representation and operations in Java. Additionally, it highlights the efficiency of using arrays for data processing and the simplicity of accessing elements via indexing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

chapter#04

The document provides an overview of arrays as a linear data structure, detailing their characteristics, types (one-dimensional and two-dimensional), and operations such as traversing, inserting, searching, and sorting. It explains how arrays store similar data types in contiguous memory locations and includes examples of array representation and operations in Java. Additionally, it highlights the efficiency of using arrays for data processing and the simplicity of accessing elements via indexing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data

Data Structure
Structure &
&
Algorithms
Algorithms

Mohammad Nawid Rahmani 1


ARRAY
• Array is the collection of similar data items

and is also called a linear data structure.


• All elements have the same data type.
• Stored in adjacent memory locations.
• Each array have a name followed by [].
• Each elements have a unique position and that
is called its index that be always integer.
• There are two types of array:
1. One dimensional array
2. Two dimensional array

Mohammad Nawid Rahmani


• As
– Data type array-name [index element]
– int A[5];
– Its indexes are A1,A2,A3….An
– A1 or A(1) or A[1]
– In the above notations, ‘A’ is the name of
the array and the numbers 1,2,3 ,……n
are indexes of individuals elements.
– The maximum number of elements in the
array is called the size/length of the array.
– The index of first element of the array is
called its lower bound (LB) and the index
of the last element is called its upper
bound (UB).
– The index can begin at 0 or 1
Mohammad Nawid Rahmani
One dimensional array

• One dimensional array is a finite number of


elements and elements can be stored in one
dimensional array in either row or in column.
A
10 20 30 40 50 60 70

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


• The size of one dimensional array can be
calculated by the following formula
size = Ub – Lb +1
6-0+1=7

Mohammad Nawid Rahmani


Why they are used:
• Arrays are useful because instead of having to
separately store related information in different
variables (named memory locations), you can store
them—as a collection—in just one variable.
• It is more efficient for a program to access and
process the information in an array, than it is to deal
with many separate variables.
• The advantage of array lies in the fact that it uses
indexed representation enabling us to access any
member element directly.
• An array has the property that items stored in it are
stored contiguously and can be accessed randomly
(by position or index) .
• They are simple, fast and can be used as the basis for
more advanced data structures.
• A few statements are required to process data in an
array. Therefore the use of arrays in program reduces
size of the program.

Mohammad Nawid Rahmani


Representation of array in memory
• An array occupies a continuous block of
memory.
• This memory block is Divided into equal
parts.
• Each part of the block represents one
element of the array.
• Starting address of array is called base
address.
• Since the elements of an array occupy a
continuous block of memory.
• If base address of the array is known, the
address of any element of the array can be
easily computed.
Mohammad Nawid Rahmani
• Example
– If an array x have 5 element ,each of 2
bytes of length ,then to find the third
element of x when the base address is
200
– Solution:
• L(x[k])=L0+C*(K-1)
• =200+2*3-1
=204
– Thus the memory address of the third
element of array X is 204.
– The memory address of an element of the
array is used to access its data directly.
Mohammad Nawid Rahmani
Operation on array
– Several operations can be performed on linear
arrays.
– The most important of these operations include
traversing, inserting searching and sorting.
• Traversing Operation
• In this each element of array is accessed exactly once
for processing.
• This is also called the visiting of the array.
• An array is used to process its data.
– Example
• To compute the sum of all elements of an array, so
each element is accessed once and their values are
added.

Mohammad Nawid Rahmani


• Similarly, to print values of each element of an

array, each element of an array is accessed

exactly once and the values are printed.

• Arrays are linear data structures and simple steps

are required to traverse them.

• The following algorithm explains traversing of

arrays.

Mohammad Nawid Rahmani


Algorithm – traversing linear arrays
1. INPUT values in Array ABC
2. SUM=0 [initialize 0 to variable SUM]
3. [compute sum of array ABC]
REPEAT FOR c=0 to 9
4. SUM=SUM + abc [c]
[end of loop]
5. PRINT SUM [print the calculated sum]
6. EXIT

Mohammad Nawid Rahmani


Implementation in java
import java.util.Scanner;
public class Array{
public static void main(String args[]){

int a[]=new int[10];


int sum=0;
Scanner s = new Scanner(System.in);
for(int i=0;i<=9;i++){
a[i]=s.nextInt();
sum=sum+a[i];
System.out.println("Sum is ="+sum);
}
}
}
Mohammad Nawid Rahmani
Implementation in java
public class SumArray{
public static void main(String args[]){

int a[]=new int[10];


int sum=0;
a[0]=100; a[1]=3; a[2]=4; a [3]=5; a
[4]=6;
a [5]=7; a [6]=8; a [7]=2; a [8]=3;
a[9]=1;
for (int i=0;i<=9;i++){
sum=sum+a[i];

System.out.println("sum is="+sum);
}
}
} Mohammad Nawid Rahmani
Inserting operation
• In inserting operation, new items are
added to an array.
– A new items can be added:
• At the end of an array without disturbing
other elements of the array if there is an
empty elements.
• At any other place with disturbing other
element.
– Inserting at the end of Array
– Consider an array ‘Temp’ having 5 elements with
its first three elements having values as shown
below:

Mohammad Nawid Rahmani


– To insert a value 78 at the end of the
array, the assignment statement is
written as:
• Temp [3] = 78
– New values can be inserted in the above
array from fourth to tenth element as
these elements are empty.
Temp
44 23 11

0 1 2 3 4

Mohammad Nawid Rahmani


Algorithm – Insertion at the end
• Write an algorithm to add six values
in to elements at the end of array
’Temp’ that has only four items
stored in its first four elements.
1. REPEAT step-2 to 3 FOR I = 4 To 9
2. INPUT value in N
3. Temp [I] = N
[end of step-2 loop]
4. EXIT

Mohammad Nawid Rahmani


Implementation in java
import java.util.Scanner;
public class Insertion{
public static void main(String args[]){
int abc[]=new int[10];
Scanner s=new Scanner(System.in);
abc[0]=100;
abc[1]=30;
abc[2]=60;
abc[3]=400;
for(int i=4;i<=9;i++){
abc[i]=s.nextInt();
}
for(int i=0;i<=9;i++){
System.out.println(abc[i]);
} } }
Mohammad Nawid Rahmani
Adding 50 students with their numbers
• import java.util.Scanner;
• public class listapp {
• public static void main(String[] args) {
• String stname[] = new String[50];
• int stlist[] = new int[50];
• Scanner keyboard = new Scanner(System.in);
• for(int i=0; i<50;i++){
• System.out.println("Enter student name and score ");
• stname[i] = keyboard.next(); stlist[i] = keyboard.nextInt();
• }
• for(int i=0; i<50;i++){
• System.out.printlin(stname[i]+”_”+stlist[i]);
• }}}

Mohammad Nawid Rahmani


Mohammad Nawid Rahmani

You might also like