



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Array is a collection of similar data items or elements. Or we can say that array is a collection of homogeneous data elements stored continuously under a single name. Need for an array When the number of variables of same type and nature are more than it is difficult to handle them. So we need an array. Let us understand the use of array with an example: int i,a; for(i=1;i<=5;i++) { printf(“Enter the no.”); scanf(“%d”,&a); } In the above program as soon as the new value is assigned to the variable a the old value is lost. Thus, all the variables that we have used so far are not able to hold more than one value at a time. But sometimes we wish to store more than one value for a variable. For example, if we wish to arrange the marks of 10 students in ascending order. There are two ways for it: (i) Use of 10 different variables so that each variable can store marks of a single student. (ii) Use a single variable which can store the marks of all students. Obviously the second option looks better because it is easier to handle them. A single variable which can store more than one value at a time is called an Array. It is a collection of homogeneous data elements stored continuously under a single name. Array declaration: int a[6]; Here a is the name of the array, int is the data type of values which the array will store, 6 is the size of array and [] are called as subscript operator. The size of array must be a constant. The most important thing is that all the values in the array should be of the same type.
10, 20, 30, 40, 50 are the elements of the array. To use the elements of the array we refer to the index. The array index starts from 0 i.e. if the size of the array is 6, so the indices of the array will be from 0 to 5. In the definition we also said that the elements of the array are stored continuously, i.e. if the element at 0 index has address 100 then the next elements will have addresses 102, 104, 106, 108, 110 if the array is an integer array because an integer occupies only 2 bytes. But if it was a float array the addresses will have been 100, 104, 108, 112, 116, 120 since a float number occupies 4 bytes.
printf(“enter numbers :”); for(i=0;i<n; i++) { scanf(“%d”,&a[i]); } min=a[ 0 ]; for(i=0;i<n; i++) { if(min>a[i]) { max=a[i]; } } printf(“minimum element =%d”, max); getch(); }
4. Write a program in C to search a key is found in given list of numbers or not. Or WAP to implement linear search in C. void main() { int a[20], i, key; printf(“enter the number of elements in the array: “); scanf(“%d”,&n); printf(“enter numbers :”); for(i=0;i<n; i++) { scanf(“%d”,&a[i]); } printf(“enter key element: “); scanf(“%d”,&key); for(i=0;i<n; i++) { if(key ==a[i]) { printf(“%d is found at %d position in list”, key, i+1); break; } } if(i==n) { printf(“key is not found in list”); } getch(); }
**1. WAP to sort a 1-d array using bubble sort technique.
Two-dimensional array is popularly known as tables or matrix and can be easily visualized as having rows and columns. Matrix can also be thought of as arrays of arrays. To create a two-dimensional array, specifying both dimensions i.e. rows and columns in square brackets. For e.g. the following declaration creates a matrix of 4 rows and 5 columns. int mat [4][5]; or #define MAXROW 4 #define MAXCOL 5 int mat [MAXROW][MAXCOL]; 0 1 2 3 4 0 0,0 0,1 0,2 0,3 0, 1 1,0 1,1 1,2 1,3 1, 2 2,0 2,1 2,2 2,3 2, 3 3,0 3,1 3,2 3,3 3, 4 4,0 4,1 4,2 4,3 4, Array are stored in row order, thus the expression mat [0] represents the first row of 5 values, mat [1] represents second row, mat [2] represents third row, and so on. Similarly, the expression mat [0][0] refers to the upper left value in the matrix, mat [2][3] represents the fourth value in third row.