c programming - arrays

22
Arrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Prepared by: Bryan G Dadiz

Upload: timothy-bryan-marc

Post on 12-Jul-2015

363 views

Category:

Software


3 download

TRANSCRIPT

Page 1: C Programming - Arrays

Arrays – C programming

Reference: AL Kelly, (2012) A Book on C,

Programming in C

Prepared by: Bryan G Dadiz

Page 2: C Programming - Arrays

Objectives

• At the end of this Training the Programmer can:

• Demonstrate the parts of an array.• Create an array declaration.• Demonstrate an array with initial values.• Demonstrate the Array Subscripting

procedure.• Create Multidimensional Array• Multidimensional Array Processing

Page 3: C Programming - Arrays

Topic Overview

• Arrays– Parts of an Array– Array Characteristics– Initializing an Array– Subscripting– 1D and 2D Arrays– Array Exercise

Page 4: C Programming - Arrays

ARRAYS

• Is also know as set.

• An array is a data type • uses subscripted

variables

• representation of a large number of homogeneous values.

Page 5: C Programming - Arrays

Dimensions of ArrayOne Dimensional Array or 1D Array

Two Dimensional or 2D Array

Page 6: C Programming - Arrays

Array

[0] [1] [2] [3] [4]

index

tray[3] = egg

tray[0] = null

Page 7: C Programming - Arrays

ArraysInstead of initializing:

int grade0, grade1, grade2, grade4, grade6;

Where is grade3?

We put this into array to be

int grade[3];

[ 1 ] [ 2 ] [ 3 ]grade

Page 8: C Programming - Arrays

Parts of an Array

int num[5];

Primitive datatype Integer thatsets the homogenouscharacteristic of the array

The name of the array that acts likea pointer for all array’s subscripts

This represents the number of elementsIn the array

The array declaration example.

Page 9: C Programming - Arrays

Array Subscript

Page 10: C Programming - Arrays

Array Element

• Array elements are the scalar data that make up an array. 

Index or Subscript Element

num[0] num[0] = 1

num[1] num[1] = 2

num[2] num[2] = 3

num[3] num[3] = 4

Seen at right part of the table the numbers 1-4 are the elements also known as array values

Page 11: C Programming - Arrays

Defining the size of the array

• It is a good programming practice to define the size of an array as symbolic constant

• Example#define size 10

int a[size];

Page 12: C Programming - Arrays

Initialization of an Array

• Arrays may be a storage class automatic, external or static, but not a register.

• Examplefloat f[5] ={0.0, 1.0, 2.0, 3.0, 4.0};

This above example put initial value for subscript f[0]to 0.0, f[1]to 1.0 and so on

Page 13: C Programming - Arrays

Initialization of an Array

If a list of initializers is shorter, then the rest of the remaining elements are initialized to zero.

int a[100] = {0};

Initializes all the elements of a to zero.

Page 14: C Programming - Arrays

Initialization of an Array

• If an array is declared without a size and initialized by a series of values, it implicitly assumes the count of the initializers is the size of the array. Thus,

int a[ ]={2,3,5,-4};

and int a[4]={2,3,5,-7};

are equivalent declarations.

Page 15: C Programming - Arrays

Initialization of an Array

• This feature works with character arrays as well.

char s[ ] = “abc”;

is equivalent to,

char s[ ] = {‘a’, ‘b’, ‘c’, ‘\0’};

Page 16: C Programming - Arrays

Subscripting

• Let us assume that the declaration int i, a[N];

• Has been made, where N is a symbolic constant.

• The expression a[i] can be made to refer to any element of the array by assignment of an appropriate value to the subscript ‘i’

• Where i >= 0 and i <= (N – 1).

Page 17: C Programming - Arrays

Accessing the Array

• The standard programming idiom for processing array elements is with a for loop.

int a[N];for(i = 0; i<N; i++){

a[i] = i;printf(“%d\n”, i);

}

Using a for loop how can we put the numbers 1 to 10 inside a[ ] ?

Page 18: C Programming - Arrays

Multi- Dimensional Array

Rows

Columns

int color[r] [c];

And array with 2 or more dimensions are called multi-dimensional array

[0]

[1]

[2]

[0] [1] [2]

2D array

Page 19: C Programming - Arrays

Accessing 2D array

• 2D arrays are usually accessed by two for loops.

#define row 3#define col 5int a[row] [col];

for(i=0; i<row; i++){for(j=0; j<col; j++){

a[i][j] = j }}

0 1 2

0 1 2

0 1 2

0 1 2

0 1 2

How can we create a 2x2 matrix assigning 1 as the element of its subscripts?

Page 20: C Programming - Arrays

Let us have an exercise.List of what to do.

• Demonstrate the parts of this array on the board.float percentage[3];

• Convert this redundant declaration to an array declaration.int num1, num2, num3, num4, num5;int a1, a2, a3, a4;

• Create an array declaration of the datatype char and initialize all the English alphabet letters from “a-z” inside the array.

• Given array: float decimal[] = { 2.3 , 3.4, 4.5 , 5.6};

What is the value of subscript decimal[3]?What is value of subscript decimal[0]?

Advanced Question:Given array: int num[5] = {1, 2, 3}; num[0] + num[2]?num[2] + num[4]?num[3] + num[5]?

Page 21: C Programming - Arrays

Summary

• Demonstrate the parts of an array.• Create an array declaration.• Demonstrate an array with initial values.• Demonstrate the Array subscripting

procedure.• Demonstrate array processing• Create Multidimensional Array• Multidimensional Array Processing

Page 22: C Programming - Arrays

End

• Next Training – Single Dimensional Array–Multidimensional Array