1 / 18

One Dimensional Arrays

One Dimensional Arrays. Rohit Khokher. One Dimensional Arrays. Definition A one-dimensional array is a linear structure of components. Components. A linear structure. One Dimensional Arrays. Points to remember An array must have a variable name

Télécharger la présentation

One Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. One Dimensional Arrays RohitKhokher

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

  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 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 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) All integer numbers All floating point numbers Data type mixing not allowed

  7. One Dimensional Arrays • Operations on 1-D Arrays • createarray (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

  9. Algorithm & C Code 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 • 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

  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 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 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 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=

  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=

  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

  17. Pass 1 Pass 2 Pass 3 Pass 4 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 Stop No Exchange Takes Place 2 7 5 85 4 2 5 4 7 8 2 7 5 4 8 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 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

More Related