Array Unit 1
Array Unit 1
Properties of Array
Advantage of C Array
o 1) Code Optimization: Less code to the access the data.
o 2) Ease of traversing: By using the for loop, we can retrieve
the elements of an array easily.
o 3) Ease of sorting: To sort the elements of the array, we
need a few lines of code only.
o 4) Random Access: We can access any element randomly
using the array.
Disadvantage of C Array
o 1) Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.
data_type array_name[array_size];
EX. int marks[5];
Initialization of C Array
The simplest way to initialize an array is by using the index of each
element. We can initialize each element of the array by using the
index. Consider the following example.
int marks[5]={80,60,70,85,75};
or
int marks[]={80,60,70,85,75};
After Declaration
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
C array example
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5];//declaration of array
5. marks[0]=80;//initialization of array
6. marks[1]=60;
7. marks[2]=70;
8. marks[3]=85;
9. marks[4]=75;
10. //traversal of array
11. for(i=0;i<5;i++){
12. printf("%d \n",marks[i]);
13. }//end of for loop
14. return 0;
15. }
Output
80
60
70
85
75
OR
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5]={20,30,40,50,60};//declaration and initialization of array
5. //traversal of array
6. for(i=0;i<5;i++){
7. printf("%d \n",marks[i]);
8. }
9. return 0;
10. }
C Array Example: Sorting an array
In the following program, we are using bubble sort method to sort
the array in ascending order.
1. #include<stdio.h>
2. void main ()
3. {
4. int i, j,temp;
5. int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
6. for(i = 0; i<10; i++)
7. {
8. for(j = i+1; j<10; j++)
9. {
10. if(a[j] > a[i])
11. {
12. temp = a[i];
13. a[i] = a[j];
14. a[j] = temp;
15. }
16. }
17. }
18. printf("Printing Sorted Element List ...\n");
19. for(i = 0; i<10; i++)
20. {
21. printf("%d\n",a[i]);
22. }
23. }
Two Dimensional Array in C
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as
the collection of rows and columns. However, 2D arrays are created
to implement a relational database lookalike data structure.
1. data_type array_name[rows][columns];
Ex- int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns.
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the
declaration and initialization are being done simultaneously. However, this
will not work with 2D arrays. We will have to define at least the second
dimension of the array. The two-dimensional array can be declared and
defined in the following way.
Arr[2][0]=3
Arr[2][1]=4
Arr[2][2]=5
Arr[3][0]=4
Arr[3][1]=5
Arr[3][2]=6
1. #include<stdio.h>
2. int main(){
3. int i=0,j=0;
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
5. //traversing 2D array
6. for(i=0;i<4;i++){
7. for(j=0;j<3;j++){
8. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
9. }//end of j
10. }//end of i
11. return 0;
12. }
Output
13. arr[0][0] = 1
14. arr[0][1] = 2
15. arr[0][2] = 3
16. arr[1][0] = 2
17. arr[1][1] = 3
18. arr[1][2] = 4
19. arr[2][0] = 3
20. arr[2][1] = 4
21. arr[2][2] = 5
22. arr[3][0] = 4
23. arr[3][1] = 5
24. arr[3][2] = 6
C 2D array example: Storing elements in a matrix and
printing it.
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
9. printf("Enter a[%d][%d]: ",i,j);
10. scanf("%d",&arr[i][j]);
11. }
12. }
13. printf("\n printing the elements ....\n");
14. for(i=0;i<3;i++)
15. {
16. printf("\n");
17. for (j=0;j<3;j++)
18. {
19. printf("%d\t",arr[i][j]);
20. }
21. }
22. }
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
56 10 30
34 21 34
45 56 78
1. #include <stdio.h>
2. void printarray(char *arr)
3. {
4. printf("Elements of array are : ");
5. for(int i=0;i<5;i++)
6. {
7. printf("%c ", arr[i]);
8. }
9. }
10. int main()
11. {
12. char arr[5]={'A','B','C','D','E'};
13. printarray(arr);
14. return 0;
15. }
C Strings
The string can be defined as the one-dimensional array of
characters terminated by a null ('\0'). Each character in the array
occupies one byte of memory.
The termination character ('\0') is important in a string since it is the
only way to identify where the string ends.
char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
1. char ch[]="javatpoint";
In such case, '\0' will be appended at the end of the string by the
compiler.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
5. char ch2[11]="javatpoint";
6.
7. printf("Char Array Value is: %s\n", ch);
8. printf("String Literal Value is: %s\n", ch2);
9. return 0;
10. }
Output
A Character array is a derived data type in C that is used to store a collection of characters or
strings.
A char data type takes 1 byte of memory, so a character array has the memory of the number of
elements in the array. (1* number_of_elements_in_array).
Each character in a character array has an index that shows the position of the character in the
string.
The first character will be indexed 0 and the successive characters are indexed 1,2,3 etc...
The null character \0 is used to find the end of characters in the array and is always stored in the
index after the last character or in the last index.
There are different ways to initialize a character array in c. Let us understand them with
examples.
We can use {} brackets to initialize a character array by specifying the characters in the
array.
The advantage of using this method is we don't have to specify the length of the array as
the compiler can find it for us.
Let us see a example where we initialize an array using {} and print it.
#include <stdio.h>
#include<string.h>
int main(){
//initialization
char char_array[] = {'c', 'h', 'a', 'r', '$','*'};
int a=puts(char_array); //print array
if(a>0){
printf("printed successfully\n");
}
printf("length of character array = %d",strlen(char_array));
//prints length of the character array.
return 0;
}
Output
char$*
printed successfully
length of character array = 6
The puts() function is used to print a character array or string to the output. It has the
following syntax
The strlen() function is present in the string.h header and is used to find the length of a
string. It has the following syntax,
#include <string.h>
#include <stdio.h>
int main(){
char char_array[] = "string of characters";
puts(char_array);
printf("length of character array = %d",strlen(char_array));
return 0;
}
Output
string of characters
length of character array = 20
int main(){
char arr[][5] = { {"code"}, {"break"}};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 5; j++) {
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
Output
c o d e
b r e a k
Row-Major Order
If the compiler is implemented using row-major order, then the values will get
stored in the memory, as shown in the image below.
Row Major Order in C
For example, if we want to access value two from memory, we must declare
a[0][1].
Column Major Order
If the compiler is implemented by column-major order, the array elements will
be stored in the memory, as shown in the image below.