Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Array in C programming, Study notes of C programming

It is a notes on array in c programming language Here you can understand it and its programs How it works? How types of array and many more

Typology: Study notes

2023/2024

Uploaded on 04/13/2025

sunshine-60
sunshine-60 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Array
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as pointers, structure, etc. The array is the
simplest data structure where each data element can be randomly accessed by using its
index number.
C array is beneficial if you have to store similar elements. For example, if we want to store
the marks of a student in 6 subjects, then we don't need to define different variables for the
marks in the different subject. Instead of that, we can define an array which can store the
marks in each subject at the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
Properties of Array
The array contains the following properties.
o Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
o Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn
later.
pf3
pf4
pf5

Partial preview of the text

Download Array in C programming and more Study notes C programming in PDF only on Docsity!

C Array

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. By using the array, we can access the elements easily. Only a few lines of code are required to access the elements of the array.

Properties of Array

The array contains the following properties. o Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. o Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. o Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.

Advantage of C Array

1) Code Optimization : Less code to the access the data. 2) Ease of traversing : By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting : To sort the elements of the array, we need a few lines of code only. 4) Random Access : We can access any element randomly using the array.

Disadvantage of C Array

1) Fixed Size : Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.

Declaration of C Array

We can declare an array in the c language in the following way.

  1. data_type array_name[array_size]; Test it Now Now, let us see the example to declare the array.
  2. int marks[5]; Test it Now Here, int is the data_type , marks are the array_name , and 5 is the array_size.

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.

  1. marks[0]=80;//initialization of array
  2. marks[1]=60;
  3. marks[2]=70;
  4. marks[3]=85;
  5. marks[4]=75; Test it Now

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++){

20 30 40 50 60

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. } Test it Now

Program to print the largest and second largest element of the array.

  1. #include<stdio.h>
  2. void main ()
  3. {
  4. int arr[100],i,n,largest,sec_largest;
  5. printf("Enter the size of the array?");
  1. scanf("%d",&n);
  2. printf("Enter the elements of the array?");
  3. for (i = 0; i<n; i++)
  4. {
  5. scanf("%d",&arr[i]);
  6. }
  7. largest = arr[0];
  8. sec_largest = arr[1];
  9. for (i=0;i<n;i++)
  10. {
  11. if (arr[i]>largest)
  12. {
  13. sec_largest = largest;
  14. largest = arr[i];
  15. }
  16. else if (arr[i]>sec_largest && arr[i]!=largest)
  17. {
  18. sec_largest=arr[i];
  19. }
  20. }
  21. printf("largest = %d, second largest = %d",largest,sec_largest);
  22. }

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. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

  1. data_type array_name[rows][columns]; Test it Now Consider the following example.
  2. int twodimen[4][3]; Test it Now Here, 4 is the number of rows, and 3 is the number of columns.
  1. void main ()
  2. {
  3. int arr[3][3],i,j;
  4. for (i=0;i<3;i++)
  5. {
  6. for (j=0;j<3;j++)
  7. {
  8. printf("Enter a[%d][%d]: ",i,j);
  9. scanf("%d",&arr[i][j]);
  10. }
  11. }
  12. printf("\n printing the elements ....\n");
  13. for (i=0;i<3;i++)
  14. {
  15. printf("\n");
  16. for (j=0;j<3;j++)
  17. {
  18. printf("%d\t",arr[i][j]);
  19. }
  20. }
  21. } Test it Now 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 printing the elements .... 56 10 30 34 21 34 45 56 78