
















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
STACKS IN DATA STRUCTURE , STACKS IN ENGINEERING...
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















324 Computer Science and Engineering
4.0 Introduction 4.1 Definition of Array 4.2 Types of Arrays 4.3 Two - Dimensional Array 4.4 Declare, initialize array of char type
If we deal with similar type of variables in more number at a time, we may have to write lengthy programs along with long list of variables. There should be
Paper - II Programming in C 325 more number of assignment statements in order to manipulate on variables. When the number of variables increases, the length of program also increase. In the above situations described above, where more number of same types of variables is used, the concept of ARRAYS is employed in C language. These are very much helpful to store as well as retrieve the data of similar type. An Array describes a contiguously allocated non-empty set of objects with the same data type. The using arrays many number of same type of variables can be grouped together. All the elements stored in the array will referred by a common name.
Array can be defined as a collection of data objects which are stored in consecutive memory locations with a common variable name. OR Array can be defined as a group of values referred by the same variable name. OR An Array can be defined as a collection of data objects which are stored in consecutive memory locations with a common variable name. The individual values present in the array are called elements of array. The array elements can be values or variables also.
Basically arrays can divide in to
1. One Dimensional Array An array with only one subscript is called as one-dimensional array or 1- d array. It is used to store a list of values, all of which share a common name and are separable by subscript values 2. Two Dimensional Array An array with two subscripts is termed as two-dimensional array. A two-dimensional array, it has a list of given variable -name using two subscripts. We know that a one-dimensional array can store a row of elements, so, a two-dimensional array enables us to store multiple rows of elements.
Paper - II Programming in C 327
The array must be declared as other variables, before its usage in a C program. The array declaration included providing of the following information to C compiler.
328 Computer Science and Engineering from 0 to size -1. When the subscript vale is 0, first element in the array is selected, when the subscript value is 1, second element is selected and so on. Example int x [6]; Here, x is declared to be an array of int type and of size six. Six contiguous memory locations get allocated as shown below to store six integer values. Each data item in the array x is identified by the array name x followed by a pair of square brackets enclosing a subscript value. The subscript value is indexed from 0 to 5. i.e., x[0] denotes first data item, x[1] denotes second data item and x[5] denotes the last data item. Initialization of One-Dimensional Arrays: Just as we initialize ordinary variables, we can initialize one-dimensional arrays also, i.e., locations of the arrays can be given values while they are declared. The general form of initializing an array of one-dimension is as follows: data - type array - name [size] = {list of values}; The values in the list are separated by commas. Example int x [6] = {1, 2, 3, 4, 5, 6 }; as a result of this, memory locations of x get filled up as follows: Points to be considered during the declaration
x[0] x[1] x[2] x[3] x[4] x[5] 1 2 3 4 5 6 x[0] x[1] x[2] x[3] x[4] x[5]
330 Computer Science and Engineering Here, x [7] does not belong to the array x, it may belong to some other program (for example, operating system) writing into the location may lead to unpredictable results or even to system crash.
Paper - II Programming in C 331 #include void main( ) { int i, x [6] = {1, 2, 3, 4, 5, 6 }; clrscr( ); printf(“The elements of array x \n”); for(i=0; i<6; i++) printf(“%d”, x [i]); getch( ); } Input – Output: The elements of array x 1 2 3 4 5 6
An array with two subscripts is termed as two-dimensional array. A two-dimensional array, it has a list of given variable -name using two subscripts. We know that a one-dimensional array can store a row of elements, so, a two-dimensional array enables us to store multiple rows of elements. Example : Table of elements or a Matrix. Syntax of two-dimensional arrays: The syntax of declaring a two-dimensional array is: data - type array - name [rowsize] [ colsize]; Where, data-type refers to any valid C data type, array -name refers to any valid C identifier, row size indicates the number of rows and column size indicates the number of elements in each column. Row size and column size should be integer constants. Total number of location allocated = (row size * column size).
Paper - II Programming in C 333 Where, data-name refers to any data type supported by C. Array-name refers to any valid C identifier. Row size indicates the number of rows, column size indicates the number of columns of the array. initialier-list is a comma separated list of values. If the number of values in initializer-list is equal to the product of row size and column size, the first row size values in the initializer-list will be assigned to the first row, the second row size values will be assigned to the second row of the array and so on Example : int x [2] [4] = {1, 2, 3, 4, 5, 6, 7, 8 }; Since column size is 4, the first 4 values of the initializer-list are assigned to the first row of x and the next 4 values are assigned to the second row of x as shown hereinafter 1 2 3 4 5 6 7 8 Note: If the number of values in the initializer-list is less than the product of rowsize and colsize, only the first few matching locations of the array would get values from the initializer-list row-wise. The trailing unmatched locations would get zeros. Example: int x [2] [4] = {1, 2, 3, 4}; The first row of x gets filled with the values in the initializer-list. The second row gets filled with zeros. 1 2 3 4 0 0 0 0 The second form of initializing a 2-d array is as follows: data-type array-name [rowsize] [colsize] = { {initializer-list1}, {initializer-list2}, ………};The values in initializer- list 1 are assigned to the locations in the first row. The values in initializer-list are assigned to the locations in the second row and so on. Example: int x [2] [4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; data - type array - name [rowsize][colsize] = [initializer - list];
334 Computer Science and Engineering As a result of this, the array x gets filled up as follows: 1 2 3 4 5 6 7 8 Note 1. If the number of values specified in any initializer-list is less than colsize of x, only those may first locations in the corresponding row would get these values. The remaining locations in that row would get 0. Example : int x [2] [4] = { { 1, 2, 3 }, { 4, 5, 6, 7 } }; Since the first initializer-list has only three values, x [0] [0] is set to 1, x [0] [1] is set to 2, x [0] [2] is set to 3 and the fourth location in the first row is automatically set to 0. 1 2 3 0 4 5 6 7
336 Computer Science and Engineering string constant. In this variation, null character ‘\0’ will be automatically added to the end of string by the compiler. In either of these variations, the size of the character array can be skipped, in which case, the size and the number of characters in the initializer-list would be automatically supplied by the compiler. Example char str1 [ ] = { “Hello” }; The size of str1 would be six, five characters plus one for the null character ‘\0’. char str2 [ ] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; The size of str2 would be six, five characters plus one for null character ‘\0’. Example 1 Program to sort a list of numbers. #include #include void main( ) { int x [6], n, i, j, tmp; clrscr( ); printf (“Enter the no. of elements \n”); scanf (“%d”, & n); printf (“Enter %d numbers \n”, n); for (i=0; ix[j])
Paper - II Programming in C 337 { tmp = x [i]; x [i] = x [j]; x [j] = tmp; } /* sorting ends */ printf (“sorted list \n”); for (i=0; i #include #include void main( ) { int x [5] [5], y [5] [5], z [5] [5], m, n, p, q, i, j;
Paper - II Programming in C 339 printf (“%d”, z[i] [j]); printf (“\n”); } getch( ); } Example Write a program for multiplication of two matrices. #include #include #include void main( ) { int x [5] [5], y [5] [5], z [5] [5], m, n, p, q, I, j, k; clrscr ( ); printf (“Enter number of rows and columns of matrix x \n”); scanf (“%d %d”, &m, &n); printf (“Enter number of rows and columns of matrix y \n”); scanf (%d %d”, &p, &q); if (n!=p) { printf (“Matrices not compatible for multiplication \n”); exit (1); } printf (“Enter the elements of x \n”); for (i=0; i 340 Computer Science and Engineering printf (“Enter the elements of y \n”); for (i=0; i 342 Computer Science and Engineering printf (“\n”); } } getch( ); } Example 5 Write a ‘C’ program to find the average marks of ‘n’ students of five subjects for each subject using arrays. Ans. #include void main( ) { int Sno, S1,S2,S3; float tot, avg; char sname[10]; printf(“Enter student no;”); scanf(“%d”, & Sname); printf(“Enter subject - 1, sub - 2, sub - 3 marks;”); scanf(“%d %d %d”, &s1,&s2,&s3); tot = S1+S2+S3; avg = tot/3; printf(“total = %f”, tot); printf(“Average = %f”, avg); } Example 6 Write a C program to check the given word is ‘Palindrome’ or not. Ans. #include
Paper - II Programming in C 343 #include void main ( ) { char str[80], rev[80]; int k, i, j, flag = 0; clrscr ( ); printf (“Enter any string (max. 80 chars) : \n”); gets (str); for (i=0; str[i]!= ‘\0’; i++); for (j=i-1; k=0; j>=0; j—, k++) rev[k] = str[j]; rev[k] = ‘\0’; for (i=0; str[i]!= ‘\0’; i++) { if (str[i]!=rev[i]) { flag=1; break; } } if (flag = =1); printf (“Given string is not palindrome. \n”); else printf (“Given string is palindrome. \n”); getch( ); }