Array in C Language - Frontier Homepage Powered by...

14
Array in C Language

Transcript of Array in C Language - Frontier Homepage Powered by...

Page 1: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Array in C

Language

Page 2: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Index

Introducing Arrays.

Declaration of a Array. Variables, Creating Arrays.

The Length of Arrays.

Initializing Arrays.

Multidimensional Arrays.

Page 3: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Introducing Arrays

Array is a static data type which store the collection of

similar type of data within a single variable. It can be single

and multi dimension. Size of array represent the total

number of element of array, data stored in array with index,

we can retrieve data by there index.

Page 4: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Introducing Arrays

Array is a data structure that represents a collection of the same types of data.

int num[10];

Num reference

An Array of 10 Elements of type int.

num [0]

num[1]

num[2]

num [3]

num[4]

num[5]

num[6]

num[7]

num[8]

num[9]

Page 5: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Introducing Arrays

Syntax :

<data_type> <array_name> [size]

eg : int arr[5];

Page 6: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Declaring Array Variables

Data type array name[index];

Example:

int list[10];

char num[15];

float hat[20];

Page 7: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

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 8: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

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 9: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

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 10: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Multidimensional Arrays

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 11: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Multidimensional Array Illustration

0 1 2 3 4

0

1

2

3

4

0 1 2 3 4

0

1

2 7

3

4

int matrix[5] [5]; matrix[2] [1] = 7

0 1 2

0 1 2 3

1 4 5 6

2 7 8 9

3 10 11 12

int[][] array ={{1, 2, 3},{4, 5, 6},{7, 8, 9},{10, 11, 12}};

Page 12: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Initializing of Multidimensional Arrays

To declare, create and initialize a multidimensional array.

For example,int[][] array = {

{1, 2, 3},{4, 5, 6},{7, 8, 9},{10, 11, 12}

};

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 13: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

#include<stdio.h>

void main(){

int arr[5];int i, b;//inputprintf("How many element you want to enter\n");scanf("%d", &b);printf("Enter %d No for array\n", b);for(i=0 ;i<b ; i++){

scanf("%d", &arr[i]);}//outputprintf("\nElement of the array are :\n");for(i=0; i<b ; i++){

printf("Element of %d position is %d\n", i, arr[i]);}

}

Page 14: Array in C Language - Frontier Homepage Powered by Yahoofrontiernet.net/~abartczak/Asmts/04/Intro_Arrays.pdfArray in C Language Index Introducing Arrays. Declaration of a Array. Variables,

Output