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

C Array: Declaration, Initialization, and Manipulation, Cheat Sheet of Computer Science

A comprehensive guide on c arrays, explaining their definition, advantages, and disadvantages. It covers array declaration, initialization, traversal, sorting, and the use of two-dimensional arrays. Additionally, it demonstrates how to pass arrays to functions in c. This resource is ideal for university students studying c programming, particularly in the second or third year of a computer science or software engineering degree.

Typology: Cheat Sheet

2023/2024

Available from 05/27/2024

ithireddy
ithireddy 🇮🇳

1 document

1 / 9

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.
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.
data_type array_name[array_size];
Now, let us see the example to declare the array.
int marks[5];
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C Array: Declaration, Initialization, and Manipulation and more Cheat Sheet Computer Science 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. 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. data_type array_name[array_size]; Now, let us see the example to declare the array. int marks[5];

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. marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; C array example #include<stdio.h> int main(){ int i=0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; //traversal of array for (i=0;i<5;i++){ printf("%d \n",marks[i]); }//end of for loop return 0; } Output 80 60

C Array Example: Sorting an array In the following program, we are using bubble sort method to sort the array in ascending order. #include<stdio.h> void main () { int i, j,temp; int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; for (i = 0; i<10; i++) { for (j = i+1; j<10; j++) { if (a[j] > a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf("Printing Sorted Element List ...\n"); for (i = 0; i<10; i++) { printf("%d\n",a[i]); } } Program to print the largest and second largest element of the array. #include<stdio.h> void main () { int arr[100],i,n,largest,sec_largest; printf("Enter the size of the array?");

scanf("%d",&n); printf("Enter the elements of the array?"); for (i = 0; i<n; i++) { scanf("%d",&arr[i]); } largest = arr[0]; sec_largest = arr[1]; for (i=0;i<n;i++) { if (arr[i]>largest) { sec_largest = largest; largest = arr[i]; } else if (arr[i]>sec_largest && arr[i]!=largest) { sec_largest=arr[i]; } } printf("largest = %d, second largest = %d",largest,sec_largest); } 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.

arr[0][0] = 1 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 2 arr[1][1] = 3 arr[1][2] = 4 arr[2][0] = 3 arr[2][1] = 4 arr[2][2] = 5 arr[3][0] = 4 arr[3][1] = 5 arr[3][2] = 6 Passing Array to Function in C In C, there are various general problems which requires passing more than one variable of the same type to a function. For example, consider a function which sorts the 10 elements in ascending order. Such a function requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different numbers and then passing into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity since the function will now work for any number of values. Consider the following syntax to pass an array to the function. functionname(arrayname);//passing array Methods to declare a function that receives an array as an argument There are 3 ways to declare the function which is intended to receive an array as an argument. First way: return_type function(type arrayname[])

Declaring blank subscript notation [] is the widely used technique. Second way: return_type function(type arrayname[SIZE]) Optionally, we can define size in subscript notation []. Third way: return_type function(type *arrayname) You can also use the concept of a pointer. In pointer chapter, we will learn about it. C language passing an array to function example #include<stdio.h> int minarray( int arr[], int size){ int min=arr[0]; int i=0; for (i=1;i<size;i++){ if (min>arr[i]){ min=arr[i]; } }//end of for return min; }//end of function int main(){ int i=0,min=0; int numbers[]={4,5,7,3,8,9};//declaration of array min=minarray(numbers,6);//passing array with size printf("minimum number is %d \n",min); return 0; }