


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
good paper and que quality and for best learning
Typology: Cheat Sheet
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.
Declaration: dataType arrayName[arraySize]; Keynotes: Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element. If the size of an array is n, to access the last element, the n- 1 index is used. In this example, mark[4] Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on. This is because the size of a float is 4 bytes. Example: #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } printf("Displaying integers: "); for(int i = 0; i < 5; ++i) { printf("%d\n", values[i]); } return 0;
Example: #include <stdio.h> int main() { int marks[10], i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i < n; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); sum += marks[i]; } average = sum / n; printf("Average = %d", average); return 0;
Similarly, we can declare a three-dimensional (3d) array. For example, float y[2][4][3]; Here, the array y can hold 24 elements. Example: // C Program to store and print 12 values entered by the user #include <stdio.h> int main() { int test[ 2 ][ 3 ][ 2 ]; printf("Enter 12 values: \n"); for (int i = 0 ; i < 2 ; ++i) { for (int j = 0 ; j < 3 ; ++j) { for (int k = 0 ; k < 2 ; ++k) { scanf("%d", &test[i][j][k]); } } } // Printing values with proper index. printf("\nDisplaying values:\n"); for (int i = 0 ; i < 2 ; ++i) { for (int j = 0 ; j < 3 ; ++j) { for (int k = 0 ; k < 2 ; ++k) { printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]); } } } return 0 ;