arrays c programming - wordpress.comarrays – c programming reference: al kelly, (2012) a book on...

22
Arrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C

Upload: others

Post on 09-Aug-2020

52 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Arrays –

C programming

Reference:

AL Kelly, (2012) A Book on C,

Programming in C

Page 2: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Topic Overview

• Arrays

– Parts of an Array

– Array Characteristics

– Initializing an Array

– Subscripting

– 1D and 2D Arrays

– Array Exercise

Page 4: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Dimensions of Array

One Dimensional Array or 1D Array

Two Dimensional or 2D Array

Page 6: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Array

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

index

tray[3] = egg

tray[0] = null

Page 7: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Arrays

Instead 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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Array Subscript

• FORMULA: An array starts with subscript 0

and ends with the array size, minus one.

Example

int num[5];

Question, what are the possible subscripts?

Page 10: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

Accessing 2D array

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

#define row 5

#define col 3

int 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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

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: Arrays C programming - WordPress.comArrays – C programming Reference: AL Kelly, (2012) A Book on C, Programming in C Objectives •At the end of this Training the Programmer can:

End

• Next Training

– Single Dimensional Array

– Multidimensional Array