You are on page 1of 18

y

nl
O
se
U
Arrays

tre
en
C
Session 7
h
ec
pt
rA
Fo

Arrays / 1 of 17
y
Objectives

nl
O
se
U
 Explain array elements and indices

tre
 Define an array

en
 Explain array handling in C
 C
Explain how an array is initialized
h
Explain string / character arrays
ec


pt

 Explain two dimensional arrays


rA

 Explain initialization of two dimensional arrays


Fo

Elementary Programming with C/Session 7/ 2 of 18


y
Array Elements & Indices

nl
O
se
 Each member of an array is identified by unique
index or subscript assigned to it

U
The dimension of an array is determined by the

tre

number of indices needed to uniquely identify each

en
element
 C
An index is a positive integer enclosed in [ ] placed
h
immediately after the array name
ec

An index holds integer values starting with zero


pt

An array with 11 elements will look like -


rA


Fo

Player[0], player[1], player[2],…. Player[10]


Elementary Programming with C/Session 7/ 3 of 18
y
Defining an Array-1

nl
O
.

se
An array has some particular characteristics

U

and has to be defined with them

tre
These characteristics include –

en

Storage Class
C
Data Types of the elements in the Array
h
ec

Array Name Which indicates the location of the first


pt

member of the array


rA

Array Size a constant evaluating to a +ve value


Fo

Elementary Programming with C/Session 7/ 4 of 18


y
Defining an Array-2

nl
O
se
An array is defined in the same way as a

U
variable is defined. The only change is that

tre
the array name is followed by one or more

en
expressions, enclosed within square brackets
C
[], specifying the array dimension.
h
ec
pt

Storage_Class data_types array_name[size]


rA

int player[11];
Fo

Elementary Programming with C/Session 7/ 5 of 18


y
Norms with Arrays

nl
O
se
 All elements of an array are of the same type

U
Each element of an array can be used

tre

wherever a variable is allowed or required

en
 Each element of an array can be referenced
C
using a variable or an integer expression
h
ec

 Arrays can have their data types like int,


pt

char, float or double


rA
Fo

Elementary Programming with C/Session 7/ 6 of 18


y
Array Handling in C-1

nl
O
se
An array is treated differently from a variable

U

in C

tre
Two arrays, even if they are of the same type

en

and size cannot be tested for equality
C
It is not possible to assign one array directly
h

ec

to another
pt

 Values cannot be assigned to an array on the


rA

whole, instead values are assigned to the


Fo

elements of the array


Elementary Programming with C/Session 7/ 7 of 18
y
Array Handling in C-2

nl
O
se
/* Input values are accepted from the user into the array ary[10]*/
#include <stdio.h>

U
void main()
{

tre
int ary[10];
int i, total, high;
for(i=0; i<10; i++)

en
{
printf(“\n Enter value: %d : ”, i+1);

C
scanf(“%d”,&ary[i]);
}
/* Displays highest of the entered values */
h
high = ary[0];
ec
for(i=1; i<10; i++)
{
pt

if(ary[i] > high)


high = ary[i];
rA

}
printf(“\nHighest value entered was %d”, high);
/* prints average of values entered for ary[10] */
Fo

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


total = total + ary[i];
printf(“\nThe average of the elements of ary is %d”,total/i);
}

Elementary Programming with C/Session 7/ 8 of 18


y
Array Initialization

nl
O
se
 Each element of an Automatic array needs to be initialized
separately

U
 In the following example the array elements have been
assigned valued using the for loop

tre
#include <stdio.h>

en
void main()
{
char alpha[26];
int i, j;
C
for(i=65,j=0; i<91; i++,j++)
h
{
ec

alpha[j] = i;
printf(“The character now assigned is %c \n”, alpha[j]);
pt

}
rA

getchar();
}
 In case of extern and static arrays, the elements
Fo

are automatically initialized to zero


Elementary Programming with C/Session 7/ 9 of 18
Strings/Character

y
Arrays-1

nl
O
se
 A string can be defined as a character type array,
which is terminated by a null character

U
Each character in a string occupies one byte and the

tre

last character of a string is “\0” (Backslash zero)

en
 Example
#include <stdio.h> C
h
void main()
ec
{
char ary[5];
int i;
pt

printf(“\n Enter string : “);


rA

scanf(“%s”,ary);
printf(“\n The string is %s \n\n”, ary);
for (i=0; i<5; i++)
Fo

printf(“\t%d”, ary[i]);
}

Elementary Programming with C/Session 7/ 10 of 18


Strings/Character

y
Arrays-2

nl
O
se
Output -

U
tre
en
The input for the above is of 4 characters and the 5th
character is the null character
C
h
ec
pt
rA
Fo

The above output is for an input of 5 characters


Elementary Programming with C/Session 7/ 11 of 18
y
String Functions

nl
O
se
Wide range of string functions, which are found in

U
the standard header file <string.h>

tre
en
C
h
ec
pt
rA
Fo

Elementary Programming with C/Session 7/ 12 of 18


y
Two-Dimensional Arrays

nl
O
se
The simplest and the most commonly used

U

multi-dimensional array is the two -

tre
dimensional array

en
A two-dimensional array can be thought of as

C
an array of two single dimensional arrays
h
ec

 A two-dimensional array looks like a railway


pt

time-table consisting of rows and columns


rA

 A two–dimensional array is declared as -


Fo

int temp[4][3];
Elementary Programming with C/Session 7/ 13 of 18
Initialization of

y
Multidimensional Arrays-1

nl
O
se
int ary[3][4] =

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

en
The result of the above assignment will be as follows :
C
h
ec
pt
rA
Fo

Elementary Programming with C/Session 7/ 14 of 18


Initialization of

y
Multidimensional Arrays-2

nl
O
se
int ary[3][4]=

U
{

tre
{1,2,3},

en
{4,5,6},
C {7,8,3}
h
ec

};
pt
rA
Fo

Elementary Programming with C/Session 7/ 15 of 18


Initialization of

y
Multidimensional Arrays-3

nl
O
se
The result of the assignment will be as follows :

U
tre
en
C
h
ec

A two - dimensional string array is declared in the


following manner :
pt
rA

char str_ary[25][80];
Fo

Elementary Programming with C/Session 7/ 16 of 18


y
Two-Dimensional Array-1

nl
O
se
#include <stdio.h>
#include <string.h>

U
void main ()
{
Example

tre
int i, n = 0;
int item;
char x[10][12];

en
char temp[12];

clrscr();
C
printf(“Enter each string on a separate line\n\n”);
h
printf(“Type ‘END’ when over \n\n”);
ec

/* read in the list of strings */


pt

do
{
rA

printf(“String %d : ”, n+1);
scanf(“%s”, x[n]);
Fo

} while (strcmp(x[n++], “END”));


/*reorder the list of strings */
contd….
Elementary Programming with C/Session 7/ 17 of 18
Two-Dimensional Array-2

y
nl
O
n = n – 1;

se
for(item=0; item<n-1; ++item)
{

U
/* find lowest of remaining strings */
for(i=item+1; i<n; ++i) Example

tre
{
if(strcmp (x[item], x[i]) > 0)

en
{
/*interchange two stings */

C
strcpy (temp, x[item]);
strcpy (x[item], x[i]);
h
strcpy (x[i], temp);
}
ec

}
}
pt

/* Display the arranged list of strings */


rA

printf(“Recorded list of strings : \n”);


for(i = 0; i < n ; ++i)
{
Fo

printf("\nString %d is %s", i+1, x[i]);


}
}
Elementary Programming with C/Session 7/ 18 of 18

You might also like