Docsity

Arrays in Computing Fundamentals: Definition, Indexing, and Examples, Slides of Computer Fundamentals

Partial preview of the text

Download Arrays in Computing Fundamentals: Definition, Indexing, and Examples and more Slides Computer Fundamentals in PDF only on Docsity!

.

ISE105: Computing Fundamentals

.

Chapter 6 - Arrays

Outline 6.1 Introduction 6.2 Arrays 6.3 Declaring Arrays 6.4 Examples Using Arrays

.

6.1 Introduction

  • Arrays
    • Structures of related data items
    • Static entity – same size throughout program
    • Dynamic data structures discussed later

Data Structures : “In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently” (NIST)

.

Arrays

  • Problem statement:
    • Store total marks of 90 students in a class
    • Need to find average of class.
    • Assign grades to each student based on his marks
  • Usual way:
    • Use 90 variables to store student information 
    • Do manipulation on them (average etc)
    • Use another 90 variables to store grades. 
  • Any better idea ??

.

6.2 Arrays

  • Array
    • Group of consecutive memory locations
    • Same name and type
  • To refer to an element, specify
    • Array name
    • Position number
  • Format: arrayname [ position number ]
    • First element at position 0
    • n element array named c:
      • c[ 0 ], c[ 1 ]...c[ n – 1 ]

Namethat ofall array elements (Note of this array have the same name, c)

Positionthe element number within of array c

c[6]

  • 6 0 72 1543

0 62

1 6453 78

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

c[11]

c[10]

c[9]

c[8]

c[7]

c[5]

c[4]

.

6.3 Defining Arrays

  • When defining arrays, specify
    • Name
    • Type of array
    • Number of elements arrayType arrayName[ numberOfElements ]; // [] subscript operator
    • Examples: int c[ 10 ]; float myArray[ 3284 ];
  • Defining multiple arrays of same type
    • Format similar to regular variables
    • Example: int b[ 100 ], x[ 27 ]; int age [10] , height [10] , names [20] ; int i , j , age [10] ;

.

Array indexing

  • The index always runs from 0 to (n-1) inclusive, and NOT 1 to n.
  • Index should be a positive integer
  • First element at index 0
  • Last element at index n-
    • Example: int day[7];
  • day[0] to day[6] are legal, but day[7] is not.
  • day[0] is the first slot
  • day[6] is the seventh slot

.

Array indexing

int age[10]; Accessing 5 th^ element of the array c=age[3+2]; OR i=2; c=age[i+3];

.

Example1: Using Arrays

  • Array elements are like normal variables int age[5],i; for ( i = 0 ; i < 5 ; i ++ ) { scanf("%d",&age[i]); }

.

Example 2

int totalAge = 0 ; int age[10]; for ( i = 0 ; i < 10 ; i ++ ) { totalAge + = age [ i ] ; }

Outline

.

16

Program Output

Element 0 Value 0 (^12 ) (^34 ) (^56 ) (^78 ) 9 0

.

Copying Arrays

  • Data types should be identical
    • Size should be same
  • int a [ 10 ] ;
  • int b [ 10 ] ;

.

Example: 4

  • Take the sum of squares of 10 different numbers which are stored in an array int a [ 10 ] ; int arraySize =10 ; int sumOfSquares = 0 ; for ( i = 0 ; i < arraySize ; i ++ ) { sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ; }

.

Example 5

Search a number z in an array

for ( i =0 ; i < 100 ; i ++ ) { if ( z == a [ i ] ) { found = 1 ; break ; } } if ( found == 1 ) printf( “ We found the integer at position %d” ,i) ; else printf( “ The number was not found” );