Presentation is loading. Please wait.

Presentation is loading. Please wait.

One Dimensional Arrays Rohit Khokher. One Dimensional Arrays Definition – A one-dimensional array is a linear structure of components. 0123456789 A linear.

Similar presentations


Presentation on theme: "One Dimensional Arrays Rohit Khokher. One Dimensional Arrays Definition – A one-dimensional array is a linear structure of components. 0123456789 A linear."— Presentation transcript:

1 One Dimensional Arrays Rohit Khokher

2 One Dimensional Arrays Definition – A one-dimensional array is a linear structure of components A linear structure Components

3 One Dimensional Arrays Points to remember 1.An array must have a variable name 2.Every component must hold a data element. 3.All components must hold the same types of data elements. 4.Data elements must be accessed by specifying the component positions that hold the data element. 5.Positions must be specified with a single index value (also called as subscript). 6.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 X[1]X[2]X[3]X[4]X[5]X[6]X[N] X Array name X1], X[2], …, X[N] are referred to as subscripted variable names X is the variable name 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] X Base address: The address of the first components of the array in the data segment Component address =Base address + index value, or Component address =Base address + Subscript value Component address =Base address + Offset, or Address of X[i] =Base address of X + value of i Example 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] All integer numbers X[1]X[2]X[3]X[4]X[5]X[6]X[N] 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 1.Start 2.Set N = 5 3.Declare array X[N] 4.Read X 5.S = 0 6.For i = 1 to N do 1.S=S + X[i] 7.Print S 8.Stop N i 5X[1]X[2]X[3]X[4]X[5] S Memory addresses

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

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

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

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

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

14 One Dimensional Arrays (Algorithms) Find the difference between the largest and smallest elements of an 1-D array Start Set N = 5 Declare array X[N] Read X smallest = X[1] largest = X[1] 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 A block/range/scope of the for loop Trace the algorithm for X=

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

16 One Dimensional Arrays (Algorithms) 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 Trace the algorithm for X=

17 Stop No Exchange Takes Place Pass 1Pass 2Pass 3Pass 4 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. Hint: While change do set change to false begin for all the array elements do if element x[i] > x[i+1] then begin 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 Rohit Khokher. One Dimensional Arrays Definition – A one-dimensional array is a linear structure of components. 0123456789 A linear."

Similar presentations


Ads by Google