Basics of One Dimensional Array

16
Arrays - One Dimensional 1 Instructor Dhiviya Rose J , AP-Sr. Scale | SoCSE University of Petroleum and Energy Studies

Transcript of Basics of One Dimensional Array

Page 1: Basics of One Dimensional Array

Arrays - One Dimensional

1

Instructor

Dhiviya Rose J , AP-Sr. Scale | SoCSEUniversity of Petroleum and Energy Studies

Page 2: Basics of One Dimensional Array

INFO 106 – Computer Programming

Recap

2

Page 3: Basics of One Dimensional Array

INFO 106 – Computer Programming

Variables Vs Array3

int a,b;

a=10;b=20;c=30;

int marks[5];

marks[0]=51;marks[1]=62;marks[2]=43;marks[3]=74;marks[4]=55;

Page 4: Basics of One Dimensional Array

INFO 106 – Computer Programming

IndexDefinition of Arrays.

Types of Arrays (1-D and 2-D)

1-D Arrays:

-Array Declaration.

-Accessing Elements of an array.

-Entering Data into an array.

-Reading data from an array.

-Array Initialization.

-Array Elements in Memory.

4

Page 5: Basics of One Dimensional Array

INFO 106 – Computer Programming

Introduction•Arrays

▫Structures of related data items▫Static entity – same size throughout

program▫Derived data types

▫Group of consecutive memory locations ▫Same name and data type

5

Page 6: Basics of One Dimensional Array

INFO 106 – Computer Programming

•To refer to an element, specify▫Array name▫Position number/ Index

•Format:arrayname[ position number ]

▫First element at position 0▫n element array named c:

c[ 0 ], c[ 1 ]...c[ n – 1 ]

6

Name of array (All elements of the array have the same name, c)

Position number or Index of the element within array c

c[6]

-4560

721543-89

062-31

645378

c[0]c[1]c[2]c[3]

c[11]c[10]c[9]c[8]c[7]

c[5]c[4]

Page 7: Basics of One Dimensional Array

7

int marks[5];

marks[0]=51;marks[1]=62;marks[2]=43;marks[3]=74;marks[4]=55;

Array Name= ?Array Size=?Index Range=?No of Elements in the Array=?

Page 8: Basics of One Dimensional Array

INFO 106 – Computer Programming

Characteristics of Array8

Page 9: Basics of One Dimensional Array

INFO 106 – Computer Programming

•Array elements are like normal variables

c[0] = 3;printf( "%d", c[0] );

▫Perform operations in subscript. c[0] = 10;C[1] = 20;C[2] = c[0] + c[1]printf( “%d”, c[2] );

9

Page 10: Basics of One Dimensional Array

INFO 106 – Computer Programming

Types of Arrays•One Dimensional Array (1D)

•Two Dimensional Array (2D)

•Multi Dimensional Array

10

Page 11: Basics of One Dimensional Array

INFO 106 – Computer Programming

Declaring Arrays – 1D•When declaring arrays, specify

▫Name▫Type of array▫Number of elementsarrayType arrayName[ numberOfElements ];

▫Examples:int c[10]; float myArray[20];

11

Page 12: Basics of One Dimensional Array

INFO 106 – Computer Programming

Initializing Arrays – 1D• Initializers

int n[5] = { 1, 2, 3, 4, 5 };

▫If not enough initializers, rightmost elements become 0int n[ 5 ] = { 0 }

All elements 0▫If too many a syntax error is produced syntax error▫C arrays have no bounds checking

• If size omitted, initializers determine itint n[ ] = { 1, 2, 3, 4, 5 };

▫5 initializers, therefore 5 element array

12

Page 13: Basics of One Dimensional Array

INFO 106 – Computer Programming

Initializing at Run time - scanfvoid main(){int ans,myarr[2];

printf(“Enter the myarr[0]”);scanf(“%d”,&myarr[0];

printf(“Enter the myarr[1]”);scanf(“%d”,&myarr[1];

ans=myarr[0]+myarr[1];printf(“The added answer is %d”,ans);}

13

Page 14: Basics of One Dimensional Array

INFO 106 – Computer Programming

Accessing elements array using for– 1D

14

Page 15: Basics of One Dimensional Array

INFO 106 – Computer Programming

Application of Arrays•Given a list of test scores, determine the

maximum and minimum scores.

•Read in a list of student names and rearrange them in alphabetical order (sorting).

•Given the height measurements of students in a class, output the names of those students who are taller than average.

15

Page 16: Basics of One Dimensional Array

INFO 106 – Computer Programming

16