Professional Documents
Culture Documents
Declaring Arrays
To declare an array, a programmer specifies the
type of the elements and the number of elements
required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimensional array.
The arraySize must be an integer constant greater
than zero and type can be any valid C data type.
e.g. double balance[10];
Now balance is a variable array which is
sufficient to hold upto 10 double numbers.
Initializing Arrays
double balance[5] = {1000.0, 2.0, 3.4, 7.0, All arrays have 0 as the index of their first
50.0}; element which is also called base index and last
The number of values between braces { } can not index of an array will be total size of the array
be larger than the number of elements that we minus 1.
declare for the array between square brackets [
].
If you omit the size of the array, an array just
big enough to hold the initialization is created.
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as you did
in the previous example.
Following is an example to assign a single
element of the array:
balance[4] = 50.0;
1
3/23/2022
Two-Dimensional Arrays
Element[0] = 100 type arrayName [ x ][ y ];
Element[1] = 101 Where type can be any valid C data type
Element[2] = 102 and arrayName will be a valid C identifier.
Element[3] = 103 A two-dimensional array can be think as a table
which will have x number of rows and y number of
Element[4] = 104
columns.
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
2
3/23/2022
Run-time initialization of
Run-
two dimensional array
a[0][0]: 0 int arr[3][4];
a[0][1]: 0 int i,j,k;
a[1][0]: 1 printf("Enter array element");
a[1][1]: 2 for(i=0;i<3;i++)
a[2][0]: 2 {
a[2][1]: 4 for(j=0; j < 4; j++)
a[3][0]: 3 {
a[3][1]: 6 scanf("%d",&arr[i][j]);
a[4][0]: 4 }
a[4][1]: 8 }
Multi-
Multi-dimensional Array
for(i=0; i < 3; i++) 2D Array:
{ int array[2][3];
for(j=0; j < 4; j++) array[0] => [0, 1, 2]
{ array[1] => [0, 1, 2]
printf("%d",arr[i][j]); 3D Array:
} int array[2][3][2];
} array[0] => [0] => [0, 1]
[1] => [0, 1]
[2] => [0, 1]
array[1] => [0] => [0, 1]
[1] => [0, 1]
[2] => [0, 1]
3
3/23/2022
for(i=1;i<n;++i) Output
{ Enter total number of elements(1 to 100): 8
if(arr[0]<arr[i]) Enter Number 1: 23.4
arr[0]=arr[i]; Enter Number 2: -34.5
} Enter Number 3: 50
printf("Largest element = %f",arr[0]); Enter Number 4: 33.5
return 0; Enter Number 5: 55.5
} Enter Number 6: 43.7
Enter Number 7: 5.7
Enter Number 8: -66.5
Largest element = 55.5
4
3/23/2022
5
3/23/2022
6
3/23/2022
Pass one-
one-dimensional arrays to
function
//function definition double getAverage(int [], int); // function
double getAverage(int arr[], int size) declaration
{ int main ()
int i; {
double avg; int balance[5] = {1000, 2, 3, 17, 50}, e;
double sum; double avg;
for (i = 0; i < size; i++) /* pass pointer to the array as an argument */
{ avg = getAverage( balance, 5 ) ; //call by
reference
sum += arr[i];
printf( "Average value is: %f ", avg );
}
return 0;
avg = sum / size;
}
return avg;
}
7
3/23/2022
Pass two-
two-dimensional arrays to
function
void Function(int c[2][2]); void Function(int c[2][2])
int main() {
{ int i,j;
int c[2][2],i,j; printf("Displaying:\n");
for(i=0;i<2;++i)
printf("Enter 4 numbers:\n");
{
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{ for(j=0;j<2;++j)
{
{ printf("%d\n",c[i][j]);
scanf("%d",&c[i][j]); }
} }
} }
Function(c);
return 0; }
Size of Array
Output void func(int a[])
Enter 4 numbers: {
2 printf("\nIn func:");
3 printf("\nThe number of elements in a is %d\n",
sizeof(a)/sizeof(a[0]));
4
}
5
int main()
Displaying:
{
2
int a[]={3,5,8,9,11};
3
printf("\nThe number of elements in a is %d\n",
4
sizeof(a)/sizeof(a[0]));
5
func(a);
return 0; }
8
3/23/2022
9
3/23/2022
10
3/23/2022
11
3/23/2022
C. Array Can be Used for Sorting Elements D. Array Can Perform Matrix Operation
We can store elements to be sorted in an array Matrix operations can be performed using the
and then by using different sorting technique we array.We can use 2-D array to store the matrix.
can sort the elements. Matrix can be multi-dimensional
Different Sorting Techniques are :
Bubble Sort
Insertion Sort
Selection Sort
Bucket Sort
12
3/23/2022
13