unimap sem2-10/11 dkt121: fundamental of computer programming1 arrays

31
UniMAP Sem2- 10/11 DKT121: Fundamental of Computer Programming 1 Arrays

Upload: geoffrey-lynch

Post on 17-Jan-2018

241 views

Category:

Documents


0 download

DESCRIPTION

UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming3 What is an Array? The variables that we have used so far have all common characteristics: Each variable could only store a single value at a time. Example: int count, length, baki; double average, jumlah; char selection; An array is a collection of a fixed number of components wherein all of the components are of the same type

TRANSCRIPT

Page 1: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 1

Arrays

Page 2: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 2

Outline6.1 Introduction6.2 Arrays of Data6.3 Declaration of Array6.4 Array Initialization6.5 Operations on Array6.5 Multidimensional Arrays6.6 Index out of bound

Page 3: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 3

What is an Array? The variables that we have used so far have all

common characteristics: Each variable could only store a single value

at a time. Example:

int count, length, baki; double average, jumlah; char selection;

An array is a collection of a fixed number of components wherein all of the components are of the same type

Page 4: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 4

What is an Array? (Example) Example: Let us work with a list of

five integers: 5,10, 15, 20, and 25.

Previously we would declare five variables: int num1, num2, num3, num4, num5;

By using array, since they are all of the same data type, we could just write:int num[5];

Page 5: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 5

What is an Array? (Example)

510152025

num

num[0]

num[1]

num[2]

num[3]

num[4]

5 components/elements in this array.

Elements are referred to index.

Element num[2] has index 2 and value 15.

Page 6: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 6

Arrays of Data Engineering applications usually involves

large chunk of data (of common type) Arrays provide easier and more efficient

concept for data storage / management Arrays are usually processed through

loops (processing is very common) Arrays are accessed by indicating an

address or index

Page 7: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 7

Arrays in C Arrays can assume any type

(including the primitive data types)int, char, string, double, float, etc.

Like any other instances, arrays must be declared before use.

Page 8: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 8

Declaration of Array Format:

data_type array_name[int value]; Example:

int list[5]; const int Max_List_Size = 10;

int hours[Max_List_Size]; const int SIZE = 100;

double amount[SIZE]; const int Max_List_Size = 6;

char alphas[Max_List_Size]; #define N 10

double b[N];

Page 9: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 9

Multiple Instances vs. Array

// multiple instanceint value1, value2, value3;

printf (“Enter first value: “);scanf (“%d”, &value1);

printf(“Enter second value: “);scanf(“%d”, &value2);

printf (“Enter third value: “);scanf(“%d”, &value3);

// process or display

// arrayint valueArray[3];

for(int count=0;count<3;count++)

{printf (“Enter value #”);printf (“%d : ”, count+1);scanf (“%d”, &valueArray[count]);

}

// process or display

Page 10: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 10

Arrays - Memory Allocation Arrays are

allocated bulk memory

Single reference used for multiple locations

Items are accessed based on index (address) with reference to first item

Page 11: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 11

Arrays Arithmetic Operations on arrays are similar to

that on basic variables. sum = num[0] + num[1] + num[2]

+ num[3]; kali = 3 * num[1]; baki= num[3] % 3; Jumlah= num[1] * num[2];

Page 12: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 12

Array Initialization Arrays can be initialized directly, but

assignments are done using loops Like any other simple variable, arrays can also

be initialized while they are being declared. double sales[5] = {12.25, 32.50, 16.90, 23,

45.68}; sales[0]=12.25, sales[1]= 32.50, sales[2]=16.90,

sales[3]= 23.00, and sales[4]=45.68;

Page 13: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 13

Array Initialization (cont…) 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

Page 14: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 14

Sample Program#include <stdio.h> int main() {

int a[3]= {11,22}, b[]={44, 55, 66},i; double x[2],y[10];

printf("a[0]=%2d, a[1]=%2d, a[2]=%2d \n" "b[0]=%2d, b[1]=%2d, b[2]=%2d \n\n",

a[0],a[1],a[2],b[0],b[1],b[2]);

printf("Please enter two real numbers\n"); scanf("%lf %lf",&x[0], &x[1]); printf("x[0] = %.1lf x[1] = %.1lf\n\n", x[0], x[1]);

for (i=0;i<10;i++)

{ y[i]= i*100.0; printf("y[%1d]=%.2lf\n", i, y[i]); }

return 0;}

Using a loop to fill all the elements of the y[] array.

Initializes the first 2 elements of the a[]array. All the other elements are then automatically set to zero

Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values.

Page 15: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 15

Sample Program(cont…) Output:

a[0]=11, a[1]=22, a[2]= 0b[0]=44, b[1]=55, b[2]=66

Please enter two real numbers77.0 88.0x[0] = 77.0 x[1] = 88.0

y[0]=0.00y[1]=100.00y[2]=200.00y[3]=300.00y[4]=400.00y[5]=500.00y[6]=600.00y[7]=700.00y[8]=800.00y[9]=900.00

Page 16: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 16

Array Initialization During Declaration

When initializing arrays while declaring them, it is not necessary to specify the size of the array.

The size of the array is determined by the number of initial values in the braces.

double sales[] = {12.25, 32.50, 16.90, 23, 45.68};

Page 17: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 17

A simple example The program declares and initializes

the array y. It uses a for loop with index i to access the successive elements of y. For each loop iteration, the value accessed id added to the variable total which is finally displayed. Note that the loop index i starts from 0 to 4(not from 1 to 5). Also, note that the array size n is declared in the define statement.

Page 18: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 18

A simple example (cont..)#include<stdio.h>#define n 5 //define number of n in the array

void main(){

int i, total = 0; //variable declaration int y[n]={9,6,20,5,12}; // array initializationfor (i=0;i<n;i++)total = total + y[i];printf ("\nTotal = %d\n“, total);

}

Page 19: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 19

Notes The defined constants, #define is used

to ease any future amendments of the codes, for instance, if the array is to be widen to an n of 10 instead of 5, it would be adequate by modifying the line:#define n 5 #define n 10with no need to make any other changes to the program, thus making the life of programmer easier.

Page 20: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 20

Operations on Array Reading data in an arrayfor (index = 0; index < 10; index++)

scanf (“%d”, &sale[index]);

Printing an array for (index = 0; index < 10; index++)

printf (“%d ”, sale[index]);

Page 21: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 21

Parallel Arrays Two (or more) Arrays are called

parallel if their corresponding components hold related information. int studentId[50];char courseGrade[50];

Page 22: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 22

Multi-Dimensional Arrays Arrays can have multiple dimensions Most used is the 2-dimensional array

(for matrix implementation) Actual implementation is a single

array (segmented) Nested loop structure usually used

to access items

Page 23: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 23

2-Dimensional Array (Example)

Page 24: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 24

Multi-Dimensional Arrays(cont..) A collection of the same type of

data stored in contiguous and increasing memory locations.

Declaration of multi-dimensional array:

int b[2][3] = {51, 52, 53, 54, 55, 56};

array_type array_name

Array dimension = 2

two rowsthree columns first row

initial values

second row initial values

Page 25: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 25

Multi-Dimensional Arrays(cont..) Multi-dimensional array can be initialized

directly in the declaration statement. For example:

int b[2][3] = {51, 52, 53, 54, 55, 56}; which initializes the elements to beb[0][0] = 51 b[0][1] = 52 b[0][2] = 53b[1][0] = 54 b[1][1] = 55 b[1][2] = 56* note that C begins its subscripts at 0. The

rightmost subscript is incremented first.

Page 26: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 26

Multi-Dimensional Arrays(cont..)

Also can use braces ({ }) to separate row in 2-dimensional arrays.

For example: int c [4][3] = {{1, 2, 3},

{4, 5, 6}, {7, 8, 9}, {10,11,12}};

int c [4][3] = {{1, 2}, {4, 5, 6}, {7}, {10,11,12}};initializes c[0][2], c[2][1] and c[2][2] to be zero

int c [ ][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}};implicitly declares the number of rows to be 4

4 rows

3 columns

rowscolumns

Page 27: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 27

Notes on Arrays Arrays enables better and easier

data management system Closely related to loops Indexing is zero-based (0 to n-1 for

an array with n locations) Multi-dimensional arrays requires

nested loop structure (e.g. 2-dimensional array)

Page 28: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 28

Index out of bound Out of bounds is when (index < 0) or

(index > arraySize -1)

double num [10]num [0], num [1],….. , num [9]

Page 29: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 29

Index out of bound In C, no guard against this problem

Does not check whether index value is within range or not

Can result in accessing data of wrong memory location

Page 30: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 30

How to overcome? Use defined loops

for (i = 0; i < 10; i ++)list [ i ] = 0;

Page 31: UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays

UniMAP Sem2-10/11

DKT121: Fundamental of Computer Programming 31

End Arrays

Q & A!