+ All Categories
Home > Engineering > Array in C language

Array in C language

Date post: 16-Apr-2017
Category:
Upload: harshita-chaurasiya
View: 467 times
Download: 1 times
Share this document with a friend
17
Transcript
Page 1: Array in C language
Page 2: Array in C language

ARRAYArray is a kind of data structure that can

store a fixed size sequential collection of element of same type.

C language provides a capability that enables the user to design a set of similar data type.

Page 3: Array in C language

DECLARATION OF ARRAY

Here int is data type marks is name of variable 30 tells how many elements of int type will be in

our array.[ ] tells the compiler that we are dealing with an

array.Note- array is declared before using it in a program.

int marks[50];

Page 4: Array in C language

ARRAY ELEMENTS STORAGE IN MEMORY

Page 5: Array in C language

ARRAY INITIALISATIONArray didn’t have any value .We have to store

values in an array.So we initialize array as follows –

int num[4]={1,3,4,5}int n[]={1,3,8,7}

float num[]={12.5,-9.2}

Page 6: Array in C language

Important notes-Till the Array elements are not given any

specific values they are supposed to contain garbage values.

If array is initialized where it is declared, mentioning the dimension of the array is optional as in 2nd and 3rd example in previous slide.

Page 7: Array in C language

SIMPLE ARRAY PROGRAMmain( ) {int avg, sum = 0 ;int i ;int marks[10] ; /* array declaration */for ( i = 0 ; i <= 9; i++ ){printf("\n Enter marks " ) ;scanf ( "%d", &marks[i] ) ; /* store data in array */}for ( i = 0 ; i <= 9 ; i++ )sum = sum + marks[i] ; /* read data from an array*/avg = sum / 10 ;printf ( "\nAverage marks = %d", avg ) ;}

Page 8: Array in C language

ARRAY ELEMENTS IN MEMORY OF PROGRAM

0 1 2 3 4 5 6 7 8 9

82 95 85 97 45 65 73 81 99 24

Marks[0]

Marks[1]

Marks[2]

Marks[3]

Marks[4]

Marks[5]

Marks[8]

Marks[7]

Marks[6]

Marks[9]

Page 9: Array in C language

ARRAY WORKING INSIDE MEMORY and WOKING OF FIRST FOR LOOP (cont.)

i=0 sum=0 avg

value of i in first for loop

Location in array Enter marks

i=0 marks[0] 82i=1 marks[1] 95i=2 marks[2] 85i=3 marks[3] 97i=4 marks[4] 45i=5 Marks[5] 65i=6 Marks[6] 73i=7 Marks[7] 81i=8 Marks[8] 99i=9 Marks[9] 24

Page 10: Array in C language

WORKING OF FIRST FOR LOOP IN PROGRAM (cont.)for ( i = 0 ; i <= 9 ; i++ ){printf ( "\nEnter marks " ) ;scanf ( "%d", &marks[i] ) ;} The for loop causes the process of asking for and

receiving a student’s marks from the user to be repeated 10 times.

The first time through the loop, i has a value 0, so the scanf() function will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until I becomes 9. This is last time through the loop, which is a good thing, because there is no array element like marks[10].

Page 11: Array in C language

EXPLANATION OF PROGRAM(cont.) In previous slide- i=0 , sum=0 , avg These values are initialized first in

program by using int data type.

This initialized an array –int is data type marks is name of variable 10 tells how many elements of int type will be in

our array shown in slide 8.

int marks[10]

Page 12: Array in C language

EXPLANATION OF SECOND FOR LOOP (cont.)• Variables already initialized in

program can be used in second for loop.

• These variables are as follows –

Then,

i=0 sum=0 avg

sum sum Marks[i]+

Page 13: Array in C language

EXPLANATION OF SECOND FOR LOOP (cont.)

Value of i in for loop

Sum value store in sum

marks[i] array location

Value store in marks[i]

Sum=Sum +marks[i]

i=0 0 marks[0] 82 0+82 = 82i=1 82 marks[1] 95 82+95 =

177i=2 177 marks[2] 85 177+85 =

262i=3 262 marks[3] 97 262+97 =

359 i=4 359 marks[4] 45 359+45 =

404i=5 404 marks[5] 65 404+65 =

469i=6 469 marks[6] 73 469+73 =

542i=7 542 marks[7] 81 542+81 =

623i=8 623 marks[8] 99 623+99 =

722i=9 722 marks[9] 24 722+24 =

746

Page 14: Array in C language

INSIDE MEMORY(cont.) We find sum of all marks of

students.Sum+marks[i]

sum

Page 15: Array in C language

INSIDE MEMORY (cont.)We get, sum of all marks and this

is stored in variable name sum.

Sum = 746

Page 16: Array in C language

EXPLANATION OF AVERAGE USED IN PROGRAMavg is variable used in program.avg has no initial value.We know that formula of average

is total marks divided by no. of subjects.

So, this can be done inside memory.avg Sum =

746 / 10

avg = 74.6

Page 17: Array in C language

IMPORTANT POINTSAn array is a collection of similar elements.The first element in the array is numbered 0, so

the last element is 1 less than the size of the array.

An array is also known as a subscripted variable.

Before using an array its type and dimension must be declared.

However big an array its elements are always stored in contiguous memory locations.


Recommended