Presentation is loading. Please wait.

Presentation is loading. Please wait.

One Dimensional Arrays

Similar presentations


Presentation on theme: "One Dimensional Arrays"— Presentation transcript:

1 One Dimensional Arrays
Rohit Khokher

2 One Dimensional Arrays
Definition A one-dimensional array is a linear structure of components. Components A linear structure 1 2 3 4 5 6 7 8 9

3 One Dimensional Arrays
Points to remember An array must have a variable name Every component must hold a data element. All components must hold the same types of data elements. Data elements must be accessed by specifying the component positions that hold the data element. Positions must be specified with a single index value (also called as subscript). An index must lie within the lower (Position of the left-most component) and upper (Position of the right-most component) bounds of array size

4 One Dimensional Arrays
Array name X X[1] X[2] X[3] X[4] X[5] X[6] X[N] X is the variable name X1], X[2], …, X[N] are referred to as subscripted variable names Integers 1, 2, 3, …, N are referred to as indices/subscripts An index/subscript must be a constant or variable name of integer type The value of an index/subscript must be non-negative integer An index/subscript value is also referred to as an offset value Program loader allocates consecutive memory locations in data segment for all the components of an array

5 One Dimensional Arrays
X[1] X[2] X[3] X[4] X[5] X[6] X[N] Base address: The address of the first components of the array in the data segment X Component address =Base address + Offset , or Component address =Base address + index value, or Component address =Base address + Subscript value Example Address of X[i] =Base address of X + value of i Suppose the loader loads the array X of size 50 components starting at address, say, (0250)10 and the value of the index i is (20)10 then the address of X[20] will be : Address of X[20] = (0250)10 + (20)10 = (0270)10

6 One Dimensional Arrays
Component type All the components must be of same type (data/structure) X[1] X[2] X[3] X[4] X[5] X[6] X[N] 100 240 034 345 004 012 00001 All integer numbers X[1] X[2] X[3] X[4] X[5] X[6] X[N] 1.00 2.40 03.4 34.5 0.04 01.2 .00001 All floating point numbers Data type mixing not allowed

7 One Dimensional Arrays
Operations on 1-D Arrays create array (by explicit declarations in C like programming languages) Destroy array Initialize array elements Update array elements Insert elements in an array Delete array elements Search an array for an specified element Find if an array is full Find if an array is empty Find the largest/smallest elements of an array Add elements of an array Add elements of two arrays Sort elements of an array in ascending/descending order …………

8 One Dimensional Arrays (Algorithms)
Add elements of an array of size N Memory addresses Start Set N = 5 Declare array X[N] Read X S = 0 For i = 1 to N do S=S + X[i] Print S Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 S 5 1.5 2.5 3.5 0.5 1.0 i 5 1.5 2.5 3.5 0.5 1.0 1 5 1.5 2.5 3.5 0.5 1.0 9.0 5 1.5 2.5 3.5 0.5 1.0 4.0 2 5 1.5 2.5 3.5 0.5 1.0 7.5 3 5 1.5 2.5 3.5 0.5 1.0 8.0 4

9 Algorithm & C Code Can we write it as scanf (“%f”, &x[i]);?
void main () { int i, n; float s; float x[5], value; n = 5; for ( i=0; i < n; i++) { scanf (“%f”, &value); x[i] = value; } s=0; s= s + x[i]; printf (“\n sum = %5.2f \n”, s); } Must declare all the variables used in the program Start Set N = 5 Declare array X[N] Read X S = 0 For i = 1 to N do S=S + X[i] Print S Stop Can we write it as scanf (“%f”, &x[i]);? Read Ch.4 on Input / Output in the text book prog. In ANSI C by E. Balgurusamy dd.dd

10 One Dimensional Arrays (Algorithms)
Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = 0 For i = 1 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 L 5 1.5 2.5 3.5 0.5 1.0 i 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 4 5 1.5 2.5 3.5 0.5 1.0 1 5 1.5 2.5 3.5 0.5 1.0 2 5 1.5 2.5 3.5 0.5 1.0 3

11 One Dimensional Arrays (Algorithms)
Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = 0.0 For i = 1 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 i -1.5 -2.5 -3.5 -0.5 -1.0 0.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 3 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 1 Wrong ?

12 One Dimensional Arrays (Algorithms)
Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = X[1] For i = 2 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 i -1.5 -2.5 -3.5 -0.5 -1.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 3 Correct?

13 One Dimensional Arrays (Algorithms)
Find the smallest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = X[1] For i = 2 to N do If X[i]< L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 i -1.5 -2.5 -3.5 -0.5 -1.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 3 Correct?

14 One Dimensional Arrays (Algorithms)
Find the difference between the largest and smallest elements of an 1-D array For i = 2 to N do Begin If X[i]< smallest then smallest =X[i] If X[i] > largest then largest = x[i] end Print | largest –smallest| Stop Start Set N = 5 Declare array X[N] Read X smallest = X[1] largest = X[1] A block/range/scope of the for loop Trace the algorithm for X= -1.5 -2.5 -3.5 -0.5 -1.0

15 One Dimensional Arrays (Algorithms)
Find which element is the largest element of an 1-D array For i = 2 to N do If X[i] > largest then Begin largest = x[i] k=i end Print “The “ k “th element” Stop Start Set N = 5 Declare array X[N] Read X largest = X[1] k=1 A block/range/scope of the for loop Trace the algorithm for X= -1.5 -2.5 -3.5 -0.5 -1.0

16 One Dimensional Arrays (Algorithms)
Trace the algorithm for X= Start Set N = 5 Declare array X[N] Read X For i=1 to N-1 do For j = i+1 to N do If X[j] > X[i] then Begin T= X[i] X[i]= x[j] X[j] = T end Print X Stop -1.5 -2.5 -3.5 -0.5 -1.0 -0.5 -2.5 -3.5 -1.5 -1.0 -0.5 -1.5 -3.5 -2.5 -1.0 -0.5 -1.0 -3.5 -2.5 -1.5 -0.5 -1.0 -2.5 -3.5 -1.5 -0.5 -1.0 -1.5 -3.5 -2.5 -0.5 -1.0 -1.5 -2.5 -3.5

17 Practice problem: Starting from pass 1 to pass 4, the following figure illustrates operations on an array. Give a pseudo code that gives an algorithmic representation of the operations depicted in this figure. 7 2 8 5 4 85 Stop No Exchange Takes Place Pass 1 Pass 2 Pass 3 Pass 4 Hint: While change do set change to false begin for all the array elements do if element x[i] > x[i+1] then swap (X[i] and x[i+1] change is true end

18 Summary You should know Variable declarations
One dimensional array declarations Reading elements of 1-D arrays (Formatted input) Writing elements of 1-D arrays (formatted output) Array based simple algorithms 1-D array manipulation using for statements 1-D array manipulation using while and do while statements 1-D array manipulation using if and switch statements


Download ppt "One Dimensional Arrays"

Similar presentations


Ads by Google