presentation about arrays

14
Arrays

Upload: slave-of-allah

Post on 08-Apr-2017

20 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Presentation about arrays

Arrays

Page 2: Presentation about arrays

Introduction to Arrays One-Dimensional Arrays Declaration of One-Dimensional Arrays Initialization of One-Dimensional Arrays

Arrays

Page 3: Presentation about arrays

Basic data types are constrained by the fact that only one value can be

stored. eg; int x=2; If you need a large of volume data to be handled ,

array is essential. An array is a fixed-size sequenced collection of

elements of the same data type. Types of arrays: 1- one-dimensional array 2- two-dimensional array 3- multidimensional array

Introduction to Arrays

Page 4: Presentation about arrays

General form of declaration : type variable-name[size]; e.g. int x[10];

Note: – type can be any type (int, float, char, …) – array name is an identifier – capacity is the number of values it can store (indexing starts at 0)

Declaration of One-Dimensional Arrays

Page 5: Presentation about arrays

How the computer prepare space (memory)

Page 6: Presentation about arrays

The value to the array elements can be assigned as follows

x[0] = 95 ;x[1] = 93 ;x[2] = 94 ;x[3] = 96 ;x[4] = 97 ;x[5] = 92 ;x[6] = 91 ;x[7] = 100 ;x[8] = 99 ;x[9] = 98 ;

Page 7: Presentation about arrays

A list of items can be given one variable name using only one subscript such variable is called One-dimentional

array.int x[10];

Page 8: Presentation about arrays

Elements in an array must be initialized after it is declared, otherwise they will contain “garbage”.

An array can be initialized with two ways: 1- At compile time (when writing the code) 2- At run time ( excution time )

Initialization of One-Dimensional Arrays

Page 9: Presentation about arrays

Compile Time Initialization General form: type array-name[size] = {list of values}; e.g. int x[10]={6,5,9,3,2,3,1,8,6,4}; int number[] = {9,3,2}; float mark[5] = {2.3,3.04,4.5,56.0,6}; char name[] = {‘J’,’A’,’C’,’K’}; or “JACK” int number[5] = {1,2}; // remain 0 char name[10] = {‘J’,’A’,’C’,’K’} ; // remain null int array[3] = {1,2,3,4} ; // illegal

Initialization of One-Dimensional Arrays

Page 10: Presentation about arrays

Runtime Initialization(large size array) ……… int array[100]; for(int j = 0; j < 100; ++j) { if(j < 50) array[j] = 0; else array[j] = 1; }

Initialization of One-Dimensional Arrays

Page 11: Presentation about arrays

Runtime Initialization Using scanf . . . . . . Int x[4]; scanf(“%d%d%d%d”, &x[0], &x[1], &x[2],

&x[3]);

Initialization of One-Dimensional Arrays

Page 12: Presentation about arrays

Runtime initialization Using scanf . . . . . . . . int x[4],i; for(i=0;i<4;i++) {scanf(“%d”,&x[i]);}}

Initialization of One-Dimensional Arrays

Page 13: Presentation about arrays

Read 10 students’ marks, calculate the average void main() { float mark[10]; // declaring an array float total = 0; // declaring avariable for(int i = 0; i < 10; i++) // counter loop scanf(“%f”,&mark[i]); // initializing the array for(int j = 0; j < 10; j++) // counter loop total += mark[j]; // initializing the variable printf(“average = %f”, total/10); }

One-Dimensional Arrays

Page 14: Presentation about arrays

contact:- [email protected]

Thank you