Beruflich Dokumente
Kultur Dokumente
GDR @ PMCTECH
Array
C Array is a collection of variables belongings to the same data type. You can
elements in memory.
Two dimensional array
Three dimensional array
four dimensional array etc…
Array elements
int age[5];
3
T .GODHANDARAMAN
C Array Declaration
T .GODHANDARAMAN 5
Initializing 1-D Array
Ways Of Array Initializing 1-D Array :
Size is Specified Directly
Size is Specified Indirectly
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];
T .GODHANDARAMAN 15
2D Array Initialization
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
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