You are on page 1of 39

Computer Fundamental & C Programming

Module:- II
Instructor:- A.K. Uttam
Assistant Professor
IIET-Invertis University
7/14/2014 1
Array
Array is a derived data type


It is used to store similar data items in contiguous(in a
sequence) memory locations under a single name.


It holds a fixed number of equally sized data elements, of
the same data type.


7/14/2014 2
Example:- If an array of 5 integers elements is created, totally
10 contiguous bytes will be allocated in memory.
0 1 2 3 4 Index
Address
Array elements
7/14/2014 3
Address of an element of an array
Individual memory location is referred by index. [index 0
refers first location , index 1 refers second location, etc.].

Address of an array element is calculated as below:-
Address of i
th
location
= base address + (size of the individual data element * index i )

Address of 0
th
element = 1000 + (2 * 0) = 1000
Address of 1
st
element = 1000 + (2 * 1) = 1002
7/14/2014 4
Array Declaration
Arrays are declared with appropriate data type and size.

Arrays can be of single dimension or of multi dimensions.

Array declaration reserves space in memory.

General Form:
datatype arrayname[size] ;


7/14/2014 5
Example
int x[5];
Defines an integer array x of 5 integers, starting at x[0], and
ending at x[4].

char game[3] = {'S', 'R', 'D'};
Defines a character array, of 3 characters.

float sales_amt[10];
Defines a floating point array sales_amt of 10 floating point
numbers, starting at sales_amt[0] and ending at sales_amt[9].

int A[2][2];
Defines a 2*2 matrix (totally 4 elements) of integers.
7/14/2014 6
Accessing Array Elements
The array elements are accessed by specifying the subscript
/ index.

General Form:
arrayname[index or subscript]

Example:-
x[0] to access the 1
st
element in array
x[4] to access the 5
th
element in array
str[2] to access the 3
rd
character in the string (character array)
sales_amt [8] to access the 9
th
sales amount in the array
7/14/2014 7
Array Initialization
Array elements can be initialized during declaration or can be
initialized in the program.

When arrays are initialized during declaration, partial
initialization is allowed.

In partial initialization, the uninitialized array elements are
initialized to Zero or Null depending on the data type of the
array.

Zero is initialized for numeric array and Null for character array.
7/14/2014 8
If initialized, array can be declared without specifying the
exact size. In such cases, size of the array equals the
number of elements initialized.

General Form:
datatype arrayname[size] = {value(s)};
OR
datatype arrayname[ ] = {value(s)};
7/14/2014 9
Example
int a[5]={1,2,3,4,5};
/*a[0] = 1, a[1] = 2 , a[2] = 3 , a[3] = 4 and a[4] = 5*/

int a[5]={0};
/*all the array elements are initialized to zero*/

int a[5]={1,2,3,4};
/*a[4] = 0*/

int a[ ] = {1,2,3,4};
/*a[0]=1, a[1]=2, a[2]=3, a[3]=4 (if size not specified, size depends upon
the number of values initialized. ) */
7/14/2014 10
Basic Operation on Arrays
Basic operations allowed on arrays are
storing
retrieving
and processing of array elements.
Insertion and deletion can be done by moving the array
elements to the appropriate places.
Example:- 3rd element can be deleted by moving 4
th

element to 3
rd
location, 5
th
element to 4
th
location and so
on.
7/14/2014 11
Getting the value for Arrays
Input statement is used to get the values for an array.
Example
int a[3];
scanf(%d, &a[0]); /*gets value for 1
st
location*/
scanf(%d, &a[1]); /*gets value for 2
nd
location*/
scanf(%d, &a[2]); /*gets value for 3
rd
location*/

for(i=0;i<3;i++)
scanf(%d,&a[i]);
/* usually loop statement is used to get the array elements*/
7/14/2014 12
Printing out the array elements
int a[3];
printf(%d, a[0]); /*prints value of 1
st
location*/
printf(%d,a[1]); /*prints value of 2
nd
location*/
printf(%d, a[2]); /*prints value of 3
rd
location*/

printf(%d%d%d, a[0],a[1],a[2]); /* prints value of first 3
locations*/

for(i=0;i<3;i++)
{
printf(%d,a[i]);
} /*loop statement is used to print the array elements */
7/14/2014 13
{
int arr[5],i;

arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;

printf("Value in array arr[0] : %d\n",arr[0]);
printf("Value in array arr[1] : %d\n",arr[1]);
printf("Value in array arr[2] : %d\n",arr[2]);
printf("Value in array arr[3] : %d\n",arr[3]);
printf("Value in array arr[4] : %d\n",arr[4]);
printf("\n");

for(i=0;i<5;i++)
{
printf("Value in array arr[%d] : %d\n",i,arr[i]);
}
getch();
}

7/14/2014 14
Array name is a constant pointer (pointer is a variable
which holds address of another variable) to the base
address of the array.

Thus, the base address can not be changed.
7/14/2014 15
Multi-dimensional Array
The elements of an array can themselves be arrays.
Multidimensional arrays will also occupy the
contiguous memory locations.

Two dimensional arrays can be viewed as set of one
dimensional array (rows & columns) and 3
dimensional arrays can be viewed as set of two
dimensional arrays.

7/14/2014 16
Two-dimensional array Declaration
Two-dimensional arrays are defined in the same way
as one dimensional array, except that a separate pair of
square brackets is required for second dimension.


General Form:
datatype arrayname [row ][column]

7/14/2014 17
Example
int a[2][2]; // creates 8 bytes of contiguous memory
locations. (2*2 = 4 elements).
Elements are stored in row major order.
Elements of 1
st
row are stored first and then the elements of
next row. It is necessary to specify the size of the column in
declaration.
Assume that array starts at location 1000,
a[0][0] will be in location 1000 - row 0 & column 0
a[0][1] will be in location 1002 - row 0 & column 1
a[1][0] will be in location 1004 - row 1 & column 0
a[1][1] will be in location 1006 - row 1 & column 1
7/14/2014 18
Two-dimensional array Initialization
Two-dimensional arrays can also be initialized in the
declaration statement.
In partial initialization, the uninitialized array elements are
initialized to Zero.
Example
int num[2][3] = {1,2,3,4,5,6};
int num[2][3] = {1,2,3,4,5}; /*num[1][2] = 0*/
int num[2][3] = {{1,2,3},{1,2,3}};
/*row elements are initialized separately*/
int num[2][3] = {{1,2},{4}};
/*num[0][2] = 0 num[1][1]=num[1][2]=0*/
7/14/2014 19
Advantages
Simple and easy to use
Stored in Contiguous locations
Fast retrieval because of its indexed nature
No need to worry about the allocation(memory) and de-
allocation of arrays.

Limitations
Conventional arrays are static in nature(fixed in size).
Memory is allocated in the beginning of the execution. If m
elements are needed, out of n locations defined, n-m
locations are unnecessarily wasted

7/14/2014 20
#include<stdio.h>
#include<conio.h>
main()
{
int value [3] [3], i, j; // Declaring an Array
for (j = 0; j<3; j++)
{
for (i = 0; i<3; i++)
{
printf("Enter the elements of array\t");
scanf("%d", &value[j][i]);
}
}


7/14/2014 21
Program Continue
Program to insert the elements in a two dimensional array:-
Displaying the Array
for (j = 0; j<3; j++)
{
for (i = 0; i<3; i++)
{
printf("The array is\n");
printf ("\t%d",value [j] [i]); //
}
printf("\n");
}
getch();
}

7/14/2014 22
Summation of Two-Dimensional array
for (j = 0; j<3; j++)
{
for (i = 0; i<3; i++)
{

c[i][j] = a[i][j]+ b[i][j];

}
printf("\n");
}
7/14/2014 23
Multiplication Of two array
for(i=0; i<row1; i++)
{
for(j=0; j<col2; j++)
{
c[i][j]=0;
for(k=0; k < row2 ; k++)
{
c[i][j] = c[i][j] + (a[i][k]*b[k][j]);
}
}
}

7/14/2014 24
Strings
Strings are sequence of characters.
In C, there is no built-in data type for strings.
String can be represented as a one-dimensional array
of characters.
A character string is stored in an array of character
type.
String should always have a NULL character (\0) at
the end, to represent the end of string.

7/14/2014 25
String constants are always enclosed within double quotes
.
And
Character constants are enclosed within single quotes
.
Example
(1) char c[4]={s, u, m, \0);
(2) char str[16]="qwerty"; /*Creates a string. The value at str[5] is
the character y. The value at str[6] is the null character. The values from
str[7] to str[15] are undefined.*/
7/14/2014 26
Example:
main( )
{
char name[5];
name[0] = G;
name[1] = O;
name[2] = O;
name[3] = D;
name[4] = \0;
}
7/14/2014 27
Example:-
char name[6] = INDIA

/* Strings are terminated by the null character, it is preferred
to allocate one extra space to store null terminator */
7/14/2014 28
Array of Strings
Two dimensional character arrays are used to represent
array of strings.

Declaration
General Form:
char arrayname [no. of strings] [max no. of chars in strings];
Example
char studname[50][15];
/* 50 student names each with 15 characters at the maximum
*/
7/14/2014 29
Initialization
General Form:
char arrayname [ r ] [ c ]={values};

Example
char name[3][5] = {bata ,cat ,at}
char name[3][5] = { {b,a,t,a,\0}, {c,a,t,\0}, {a,t,\0} }



7/14/2014 30
char A[3][3]={"AB","CD","GH"};
printf("\n%c",A[0][0]); //A
printf("\n%c",A[0][1]); //B
printf("\n%c",A[0][2]); //null
printf("\n%c",A[1][0]); //C
printf("\n%c",A[1][1]); //D
printf("\n%c",A[1][2]); //null
printf("\n%c",A[2][0]); //G
printf("\n%c",A[2][1]); //H
printf("\n%c",A[2][2]); //null
7/14/2014 31
Reading/Input a string
String can be read either character-by-character or as an entire
string (using %s format specifier).

Array name itself specifies the base address and %s is a format
specifier which will read a string until a white space character
is encountered.

[Note: no need to use & operator while reading string using
%s]
7/14/2014 32
main( )
{
char name[ ] = COMPUTER" ;
printf ( "%s", name ) ;
}

7/14/2014 33

strcpy(s1, s2)
#include <string.h>
main( )
{
char string1[] = "Hello, world!";
char string2[20];
strcpy(string2, string1); //this function will copy string1 into string2
printf("%s\n", string2); // Hello, world!
}
7/14/2014 34
strcmp(string3, string4)
char string3[] = "this is";
char string4[] = "a test";
if( strcmp(string3, string4) == 0)
{
printf("strings are equal\n");
}
else
{
printf("strings are different\n");
}
7/14/2014 35
strcat(string5, string6)
char string5[20] = "Hello, ";
char string6[] = "world!";
printf("%s\n", string5); // Hello,
strcat(string5, string6); //concatenate two strings
printf("%s\n", string5); // Hello, world!
7/14/2014 36
strlen(string7)
char string7[ ] = "abc defg";
int len = strlen(string7);
printf("%d\n", len); // 8


// it gives the number of characters in it, not including
the \0.
7/14/2014 37
Various Function of strings
strlwr(string); Converting Uppercase Strings to Lowercase
Strings
strrev(string); Reversing the Order of a String
strupr(string); Converting Lowercase Strings to Uppercase
Strings
strcmp(string1, string2); Compare Two Strings
strlen(string); Length of a String
strcat(string1, string2); Concatenation of Strings
strcpy(string1, string2); Copy String2 into string1
7/14/2014 38

7/14/2014 39

You might also like