basic array in c programming

19
Do You Know What Is Array In C Programming ??

Upload: sajid-hasan

Post on 23-Jan-2018

172 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Basic array in c programming

Do You Know

What Is Array In C

Programming ??

Page 2: Basic array in c programming

PRESENTED BY :

Pranto Sharma --------------------- 161-15-7539

Sabbir Rahman----------------------161-15-7123

Kaniz Assami Tokey---------------161-15-7608

Md anando Islam Riyed----------152-15-5778

Page 3: Basic array in c programming

INDEX :Introducing Arrays.

Declaration of a Array. Variables, Creating Arrays.

The Length of Arrays.

Initializing Arrays.

Defining the size of the array

Different types of arrays

Multidimensional Arrays

Page 4: Basic array in c programming

INTRODUCING ARRAYS :

An array is a collection of variables of the same type that are referred to through a common name.

int num[10];

Num reference num [0]

num[1]

num[2]

num [3]

num[4]

num[5]

num[6]

num[7]

num[8]

num[9]

Page 5: Basic array in c programming

DECLARING ARRAY VARIABLES :

Data type array name[index];

Example:

int list[10];

char num[15];

float hat[20];

Page 6: Basic array in c programming

CREATING ARRAYS :

Data type array-name[size];

Example:

int num[10];

num[0]references the first element in the

array.

num[9]references the last element in the

array.

Page 7: Basic array in c programming

THE LENGTH OF ARRAYS :

Once an array is created, its size is fixed. It

cannot be changed.

For Example,

int arr[10];

You can not insert any number to arr[11]

location because it is not initialized.

Page 8: Basic array in c programming

INITIALIZING ARRAYS :

Declaring, creating, initializing in one step:

int my Array[5] = {1, 2, 3, 4, 5};

int studentAge[4];

studentAge[0] = 14;

studentAge[1] = 13;

studentAge[2] = 15;

studentAge[3] = 16;

Page 9: Basic array in c programming

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 10: Basic array in c programming

DIFFERENT TYPES OF ARRAYS :

1- dimensional array

2-dimensional array

Multi-dimensional array

Page 11: Basic array in c programming

ONE-DIMENSIONAL ARRAYS :

• One dimensional array have only one subscript under a

common name.

• syntax

• Data_type array_name[array_size];

• Example

• int age[5]={2,4,34,3,4};

Page 12: Basic array in c programming

EXAMPLE PROGRAM :#include<stdio.h>

int main()

{ int a[4];

int i;

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

{

Scanf(“%d”,&a[i]);

}

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

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

return 0;

}

Page 13: Basic array in c programming

TWO-DIMENSIONAL ARRAYS :

Two-dimensional array are those type of

array, which has finite number of rows and

finite number of columns. 2D arrays are

generally known as matrix. We will discuss two

Dimensional array in detail but before this

have a look at the below piece of code .

Data_type Array_name [row size][column

size];

Page 14: Basic array in c programming

RowsColumns

int color[r] [c];

[0]

[1]

[2]

[0] [1] [2]

Page 15: Basic array in c programming

EXAMPLE PROGRAM :

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

int disp[3][5]; int i, j; for(i=0; i<=2; i++) {

for(j=0;j<=4;j++) {

printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]);

}

} return 0; }

Page 16: Basic array in c programming

MULTI-DIMENSIONAL ARRAYS :

An array have multiple subscript under a

common name.

Syntax

type name[size1][size2]...[sizeN];

Example

float a[2][6][7][4]….[n];

Page 17: Basic array in c programming

EXAMPLE PROGRAM :

int matrix[10] [10];

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

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

{

matrix[i] [j] = i * j;

}

float mat[5] [5];

Page 18: Basic array in c programming

INITIALIZING OF MULTIDIMENSIONAL ARRAYS :

This is equivalent to the following statements:

array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;

array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;

array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;

array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

Page 19: Basic array in c programming