basic array in c programming

Post on 23-Jan-2018

172 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Do You Know

What Is 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

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

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]

DECLARING ARRAY VARIABLES :

Data type array name[index];

Example:

int list[10];

char num[15];

float hat[20];

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.

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.

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;

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];

DIFFERENT TYPES OF ARRAYS :

1- dimensional array

2-dimensional array

Multi-dimensional array

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};

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;

}

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];

RowsColumns

int color[r] [c];

[0]

[1]

[2]

[0] [1] [2]

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; }

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];

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];

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;

top related