Sie sind auf Seite 1von 27

UNIT - 3

UNIT III ARRAYS AND STRINGS

Arrays – Initialization – Declaration – One dimensional and Two dimensional


arrays. String- String operations – String Arrays. Simple programs- sorting-
searching – matrix operations.

GDR @ PMCTECH
Array
 C Array is a collection of variables belongings to the same data type. You can

store group of data of same data type in an array.

 Array might be belonging to any of the data types

 Array size must be a constant value.

 Always, Contiguous (adjacent) memory locations are used to store array

elements in memory.

 It is a best practice to initialize an array to zero or null while declaring, if we

don’t assign any values to array.

Example for C Arrays:


int a[10];       // integer array
char b[10];    // character array   i.e. string
T .GODHANDARAMAN 2
Types of C arrays

There are 2 types of C arrays. They are,

 One dimensional array


 Multi dimensional array

 Two dimensional array
 Three dimensional array
 four dimensional array etc…

Array elements

int age[5];

3
T .GODHANDARAMAN
C Array Declaration

 Array has to be declared before using it in C Program. Array is nothing


but the collection of elements of similar data types.
Syntax:
<data_type> array_name [size1][size2].....[sizen];
S.No Syntax Parameter Significance
1. data_type Data Type of Each Element of the array
2. Array_name Valid variable name
3. size Dimensions of the Array

Array declaration requirement


Requirement Explanation
Data Type specifies the type of the array. We can compute the size required for
Data Type
storing the single cell of array.
Valid identifier is any valid variable or name given to the array. Using this
Valid Identifier
identifier name array can be accessed.
Size of Array It is maximum size Tthat array can have.
.GODHANDARAMAN 4
What does C Array Declaration tells to Compiler ?

 Type of the Array


 Name of the Array
 Number of Dimension
 Number of Elements in Each Dimension

Array declaration, initialization Example


and accessing
Array declaration syntax: Integer array example:
data_type arr_name [arr_size]; int age [5];
Array initialization syntax: int age[5]={0, 1, 2, 3, 4};
data_type arr_name [arr_size]=(value1,
value2, value3,….);
Array accessing syntax: age[0]; /*0 is accessed*/
arr_name[index]; age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/

T .GODHANDARAMAN 5
Initializing 1-D Array
Ways Of Array Initializing 1-D Array :
 Size is Specified Directly
 Size is Specified Indirectly

Method 1 : Array Size Specified Directly

int num[5] = {2,8,7,6,0}; num[0] = 2


num[1] = 8
num[2] = 7
num[3] = 6
num[4] = 0
 As at the time of compilation all the elements are at Specified Position So This
Initialization Scheme is Called as “Compile Time Initialization“.
Graphical Representation :

T .GODHANDARAMAN 6
Method 2 : Size Specified Indirectly
int num[] = {2,8,7,6,0};

Explanation :
 Compiler Counts the Number Of Elements Written Inside Pair of Braces and
Determines the Size of An Array.
 After counting the number of elements inside the braces, The size of array is
considered as 5 during complete execution.
 This type of Initialization Scheme is also Called as “Compile Time Initialization“

T .GODHANDARAMAN 7
C Program to display array elements with addresses

#include<stdio.h>
#include<stdlib.h>
#define size 10 OUTPUT
int main() a[0] ,value=11 : address=2358832
a[1] ,value=22 : address=2358836
{ a[2] ,value=33 : address=2358840
int a[3] = { 11, 22, 33 };
printf("\n a[0] ,value=%d : address=%u", a[0], &a[0]);
printf("\n a[1] ,value=%d : address=%u", a[1], &a[1]);
printf("\n a[2] ,value=%d : address=%u", a[2], &a[2]);
return (0);
}

T .GODHANDARAMAN 8
//program to set values of array and display it
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i; OUTPUT
clrscr(); The value in a[0] is 10
a[0]=10; The value in a[1] is 20
a[1]=20; The value in a[2] is 30
a[2]=30; The value in a[3] is 40
a[3]=40; The value in a[4] is 50
a[4]=50;
for(i=0;i<5;i++)
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
T .GODHANDARAMAN 9
//program to get values of array from users and display it
#include<stdio.h> OUTPUT
#include<conio.h> Enter five values :
void main() 10
{ 20
int a[5],i; 30
clrscr();
40
printf("Enter five values : \n");
50
for(i=0;i<5;i++)
The value in a[0] is 10
{
scanf("%d",&a[i]); The value in a[1] is 20
} The value in a[2] is 30
for(i=0;i<5;i++) The value in a[3] is 40
{ The value in a[4] is 50
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
T .GODHANDARAMAN 10
//program to find maximum no in an array
#include<stdio.h>
#include<conio.h> printf(“Maximum no is %d“,max);
void main() getch();
{ }
int a[5],i,max;
clrscr();
printf("Enter five values : \n");
for(i=0;i<5;i++) OUTPUT
{ Enter five values :
scanf("%d",&a[i]); 10
} 20
max=a[0]; 30
for(i=1;i<5;i++) 40
{ 50
if(max<a[i]) Maximum no is 50
{
max=a[i];
}
} T .GODHANDARAMAN 11
//program to find sum of elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
OUTPUT
int a[5],i,sum=0;
clrscr(); Enter five values :
printf("Enter five values : \n"); 1
for(i=0;i<5;i++) 2
{ 3
scanf("%d",&a[i]); 4
} 5
for(i=0;i<5;i++) The sum of elements in array is :
{ 15
sum=sum+a[i];
}
printf(“The sum of elements in array is : %d”,sum);
getch();
}
T .GODHANDARAMAN 12
//program to sort number in ascending order
#include<stdio.h>
#include<conio.h> }
}
void main() printf(“Ascending order : \n”);
{ for(i=0;i<5;i++)
int a[5],i,j,t; {
clrscr(); printf(“%d\t”,a[i]);
}
printf("Enter five values : \n"); getch();
for(i=0;i<5;i++) }
{
scanf("%d",&a[i]);
} OUTPUT
for(i=0;i<5;i++) Enter five values :
{ 40
for(j=i+1;j<5;j++) 10
{ 30
if(a[i]>a[j]) 50
{ 20
t=a[i]; Ascending order :
a[i]=a[j]; 10 20 30 40 50
a[j]=t;
} T .GODHANDARAMAN 13
//program to search element in array(Linear Search)

#include<stdio.h>
#include<conio.h> else
{
printf(“Value not found”);
void main() }
{ }
int a[5]={10,20,30,40,50}; getch();
int key; }
clrscr();
printf("Enter search value:\n");
scanf("%d",&key); OUTPUT
for(i=0;i<5;i++) Enter search value : 40
{ Value found
if(key==a[i])
{
printf(“Value found”);
break;
}

T .GODHANDARAMAN 14
Multidimensional array
 Array having more than one subscript variable is called multidimensional
array.
 Multidimensional array is also called as matrix.
Syntax: data_type array_name[row_size] [col_size];

Consider the Two (2D)dimensional array –

 Two Dimensional Array requires Two Subscript Variables


 Two Dimensional Array stores the values in the form of matrix.
 One Subscript Variable denotes the “Row” of a matrix.
 Another Subscript Variable denotes the “Column” of a matrix.

T .GODHANDARAMAN 15
2D Array Initialization

Array declaration, initialization


Example
and accessing 
Array declaration syntax:
Integer array example:
data_type arr_name [num_of_rows]
int arr[2][2];
[num_of_column];

Array initialization syntax: int arr[2][2] = {1,2, 3, 4};


data_type arr_name[2][2] = {{0,0},
{0,1},{1,0},{1,1}};
Array accessing syntax: arr [0] [0] = 1;
arr_name[index]; arr [0] ]1] = 2;
arr [1][0]  = 3;
arr [1] [1] = 4;

T .GODHANDARAMAN 16
Example2D Array
Declaration and Use of Two Dimensional Array
int a[3][4];

Use :
for(i=0;i<row,i++)
for(j=0;j<col,j++)
{
printf("%d",a[i][j]);
}
Meaning of Two Dimensional Array :
 Matrix is having 3 rows ( i takes value from 0 to 2 )
 Matrix is having 4 Columns ( j takes value from 0 to 3 )
 Above Matrix 3×4 matrix will have 12 blocks having 3 rows & 4 columns.
 Name of 2-D array is ‘a‘ and each block is identified by the row & column
number.
 Row number and Column Number Starts from 0.
T .GODHANDARAMAN 17
//program to assign values to array and to display it

#include<stdio.h>
#include<conio.h>
void main()
{ OUTPUT
int a[3][3]={10,20,30,40,50,60,70,80,90};
Values in array :
int i,j;
a[0][0]=10
clrscr();
a[0][1]=20
printf(“Values in array : \n");
a[0][2]=30
for(i=0;i<3;i++)
{ a[1][0]=40
for(j=0;j<3;j++) a[1][1]=50
{ a[1][2]=60
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); a[2][0]=70
} a[2][1]=80
} a[2][2]=90
getch();
}
T .GODHANDARAMAN 18
//program to assign values to array from user and to display it
#include<stdio.h>
#include<conio.h> OUTPUT
void main() Enter 9 values in array :
{ 10
int a[3][3],i,j; 20
clrscr(); 30
printf(“Enter 9 values in array : \n"); 40
for(i=0;i<3;i++) 50
{
60
for(j=0;j<3;j++)
{ 70
scanf(“%d”,&a[i][j]); 80
} 90
} Values in array :
printf(“Values in array : \n”); a[0][0]=10
for(i=0;i<3;i++) a[0][1]=20
{ a[0][2]=30
for(j=0;j<3;j++)
a[1][0]=40
{
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); a[1][1]=50
} a[1][2]=60
} a[2][0]=70
getch(); a[2][1]=80
} a[2][2]=90
T .GODHANDARAMAN 19
//program to implement Matrix addition
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j; c[i][j]=a[i][j]+b[i][j];
clrscr(); }
printf(“Enter values of Matrix A : \n"); }
for(i=0;i<3;i++) printf(“Added Matrix\n”);
{ for(i=0;i<3;i++)
for(j=0;j<3;j++) {
{ for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]); {
printf(“%d\t”,c[i][j]);
} }
} printf(“\n”);
printf(“Enter values of Matrix B : \n"); }
for(i=0;i<3;i++) getch();
{ }
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
} T .GODHANDARAMAN 20
//program to implement Matrix addition
OUTPUT
Enter values of Matix A :
1
2
3
4
5
6
7 OUTPUT
8 Addded Matrix
9 2 4 6
Enter values of Matix B : 8 10 12
1 14 16 18
2
3
4
5
6
7
8
9
T .GODHANDARAMAN 21
//program to implement Matrix multiplication
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j,k; c[i][j]=0;
clrscr(); for(k=0;k<m;k++)
printf(“Enter values of Matix A : \n"); {
for(i=0;i<3;i++) c[i][j]=c[i][j]+a[i][k]*b[k][j];
{ }
for(j=0;j<3;j++) }
{ }
scanf(“%d”,&a[i][j]); printf(“Multiplied Matrix\n”);
} for(i=0;i<3;i++)
} {
for(j=0;j<3;j++)
printf(“Enter values of Matix B : \n"); {
for(i=0;i<3;i++) printf(“%d\t”,c[i][j]);
{ }
for(j=0;j<3;j++) printf(“\n”);
{ }
getch();
scanf(“%d”,&b[i][j]); }
}
} T .GODHANDARAMAN 22
//program to implement Matrix
OUTPUT
Enter values of Matrix A :
1
1
1
1
1
1
1 OUTPUT
1 Multiplied Matrix
1 2 4 6
Enter values of Matrix B : 8 10 12
1 14 16 18
1
1
1
1
1
1
1
1
T .GODHANDARAMAN 23
//program to find transpose of Matrix
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{
{ printf(“%d\n”,i,j,a[j][i]);
int a[3][3],i,j; }
clrscr(); }
printf(“Enter values in array : \n"); getch();
for(i=0;i<3;i++) } OUTPUT
{ Enter values in array :
1
for(j=0;j<3;j++)
2
{ 3
scanf(“%d”,&a[i][j]); 4
} 5
} 6
printf(“\nTranspose of given Matrix : \n”); 7
8
9
Transpose of given Matrix :
1 4 7
2 5 8
T .GODHANDARAMAN 24
3 6 9
Array Limitations
Static Data
 Array is Static data Structure
 Memory Allocated during Compile time.
 Once Memory is allocated at Compile Time it Cannot be Changed during Run-time

Can hold data belonging to same Data types


 Elements belonging to different data types cannot be stored in array because array data
structure can hold data belonging to same data type.
Example : Character and Integer values can be stored inside separate array but
cannot be stored in single array

Inserting data in Array is Difficult


 Deletion is not easy because the elements are stored in contiguous memory location.
 Like insertion operation , we have to delete element from the array and after deletion
empty space will be created and thus we need to fill the space by moving elements up in
the array.
T .GODHANDARAMAN 25
Bound Checking
 If we specify the size of array as ‘N’ then we can access elements upto ‘N-1’ but in C if
we try to access elements after ‘N-1’ i.e Nth element or N+1th element then we does
not get any error message.
 Process of Checking the extreme limit of array is called Bound Checking and C does
not perform Bound Checking. 
 If the array range exceeds then we will get garbage value as result.

Shortage of Memory
 Array is Static data structure. Memory can be allocated at compile time only Thus if
after executing program we need more space for storing additional information then we
cannot allocate additional space at run time.
 Shortage of Memory , if we don’t know the size of memory in advance

Wastage of Memory
 Wastage of Memory , if array of large size is defined
T .GODHANDARAMAN 26
Array Applications
 Stores Elements of Same Data Type
 Array Used for Maintaining multiple variable names using single
name
 Array Can be Used for Sorting Elements
 Array Can Perform Matrix Operation
 Array Can be Used in CPU Scheduling
Different Sorting Techniques are:
 Bubble Sort
 Insertion Sort
 Selection Sort
 Bucket Sort
 Array Can be Used in Recursive Function

T .GODHANDARAMAN 27

Das könnte Ihnen auch gefallen