CP Arrays
CP Arrays
Using Arrays in C: C supports a derived data type known as array that can be
used to handle large amounts of data (multiple values) at a time.
Definition:
Or
An array is a data structured that can store a fixed size sequential collection of
elements of same data type.
Need of an array
Advantage of C Array
IT-Ramesh 9848353570 1
4) Random Access: We can access any element randomly using the array.
Disadvantage of Array
Declaration of an Array
For example:
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of
array. It means array arr can only contain 10 elements of int type. Index of an
array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0]
address and last element will occupy arr[9].
Example
int age[5]={22,25,30,32,35};
10 20 30 40 50
IT-Ramesh 9848353570 3
If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.
C O M P U T E R
//error : number of initial values are more than the size of array.
int a[5]={10,15};
10 15 0 0 0
IT-Ramesh 9848353570 4
Access the elements of an array: We can access elements of an array by
indices/index. You can use array subscript (or index) to access any
element stored in array. Subscript starts with 0, which means
array_name[0] would be used to access first element in an array.
Input data into array: As you can see, in above example that I have used „for
loop‟ and „scanf statement‟ to enter data into array. You can use any loop for
data input.
Code:
IT-Ramesh 9848353570 5
Reading out data from an array: For example, you want to read and display
array elements, you can do it just by using any loop. Suppose array is
mydata[20].
printf("%d\n", mydata[x]);
Exmaple
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int arr[]={2,3,4}; //Compile time array initialization
for(i=0 ; i<3 ; i++)
{
printf("%d\t",arr
[i]);
}
getch();
}
Exmaple
include <stdio.h>
#include <conio.h>
void main( )
{
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
clrscr( );
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
IT-Ramesh 9848353570 6
}.
getch();
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element");
for(i=0;i<4;i++)
{
scanf("%d",&arr[i]);//Run time array initialization
}
for(j=0;j<4;j++)
{
printf("%d\n",arr[j]);
}
getch();
}
data_type array_name[size1][size2];
Example
int twodimen[4][3];
IT-Ramesh 9848353570 7
Example : int a[3][4];
Initialization of 2D Array
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Example
#include <stdio.h>
#include <conio.h>
void main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
clrscr();
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
IT-Ramesh 9848353570 8
}//end of j
}//end of i
getch();
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[25][25],b[25][25],c[25][25], i, j, m, n;
clrscr();
printf("enter the rows and colums of two matrics:\n");
scanf("%d%d",&m,&n);
printf("\nenter the elements of A matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d", &a[i][j]);
}
IT-Ramesh 9848353570 9
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
int test[2][3][4] = {
};
Example
#include <stdio.h>
int main()
{
int i, j, k, test[2][3][2];
IT-Ramesh 9848353570 10
printf("Enter 12 values: \n");
for(i = 0; i < 2; ++i)
{
for (j = 0; j < 3; ++j)
{
for(k = 0; k < 2; ++k ) {
scanf("%d", &test[i][j][k]);
}
}
}
printf("\nDisplaying values:\n");
for(i = 0; i < 2; ++i)
{
for (j = 0; j < 3; ++j)
{
for(k = 0; k < 2; ++k ) {
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Or
In „C‟ language the group of characters, digits, and symbols enclosed within
double quotation ( " " ) marks are called as string otherwise a string is an array
of characters and terminated by NULL character which is denoted by the
escape sequence „\0‟.
C Strings
Using this declaration, the compiler allocates 9 memory locations for the
variable a ranging from 0 to 8.
0 1 2 3 4 5 6 7 8
str[2]; ---> A
Note: In initialization of the string we can not initialized more than size of string
elements.
Ex:
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
int a[10]={“R‟,‟A‟,‟M‟,‟A‟ };
The compiler allocates 10 bytes for the variable a ranging from 0 to 9 and
initializes first four locations with the ASCII characters of „R‟, „A‟, „M‟, „A‟.The
remaining locations are automatically filled with NULL characters (i.e,\0).
R A M A \ \ \ \ \ \
0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
C O M P U T E R
Here, the string length is 8 bytes. But , string size is 9 bytes. So the compiler
reserves 8+1 memory locations and these locations are initialized with the
characters in the order specified. The string is terminated by \0 by the
compiler.
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
IT-Ramesh 9848353570 14