To store the total marks of 100 students an array will be declared as follows,

Size: px
Start display at page:

Download "To store the total marks of 100 students an array will be declared as follows,"

Transcription

1 Chapter 4 ARRAYS LEARNING OBJECTIVES After going through this chapter the reader will be able to declare and use one-dimensional and two-dimensional arrays initialize arrays use subscripts to access individual array elements write programs involving one-dimensional and two-dimensional arrays write programs for matrix operations 4.1 INTRODUCTION Consider the following program main( ) int x ; x = 10; x=15; printf ( x=%d \n,x); This program will print the value of x as 15. Because, when a value 15 is assigned to x, the earlier value of x, i.e., 10 is lost. Thus ordinary variables are capable of holding only one value at a time (as in the above example). However, there are situations in which we would want to store more than one value at a time in a single variable. For example, we wish to arrange the total marks obtained by 100 students in ascending order. To do this, it is needed to store all 100 values in the memory simultaneously. In this situation an array is used. What is an array? An array is a collective name given to a group of related quantities belonging to same data type. These related quantities can be total marks of 100 students or salaries of 500 employees or heights of 200 students. For example, we can use an array name tmarks to represent a set of total marks of a group of 100 students in a class. We can refer to the individual marks by writing a number called index or subscript in square brackets after the array name. Example4.1: To store the total marks of 100 students an array will be declared as follows, float tmarks[100]; tmarks [0], tmarks [1], tmarks [2] etc. represents the total marks of 1 st, 2 nd, 3 rd etc. students. In general, tmarks[i], i can take values 0,1,2,3, represent the total marks of (i+1) th student. Here tmarks is the array name and i is its subscript. Thus the array tmarks is the collection of 100 memory locations referred as below: tmarks[0] tmarks[1] tmarks[2]... tmarks[99] Arrays 87

2 In the above figure, it is assumed that integer value occupies 2 bytes. Thus 200 bytes of space will be allocated to the array tmarks. We can use arrays to represent not only single lists of values but also tables of data(like matrix, marks of 100 students in 6 subjects, sales of a departmental store in a week) in two, three or more dimensions. This chapter explain the use of arrays, types of arrays, declaration and initialization of one dimensional and two dimensional arrays with the help of examples. 4.2 ARRAY DECLARATION Before discussing an array, first of all let us look at the characteristic features of an array i) Array is a data structure storing a group of elements, all of which are of the same data type. ii) All the elements of the array share the same name, and they are distinguished from one another with the help of an index called subscript iii) Random access to every element using a numeric index (subscript) is possible iv) A simple data structure which is extremely useful The declaration of an array is just like any variable declaration with additional size part, indicating the number of elements of the array. Like other variables, arrays must be declared at the beginning of a function. The declaration specifies the data type of the array, the name and its size or dimension Declaration of one dimensional arrays data-type array-name[constant-size] [constant-size]; data-type refers to the type of elements to store and constant-size is the maximum number of elements. The following are some examples of array declarations Example4.2: int x[100]; // x is an array to store maximum 100 integers float height[50]; //height is an array to store heights of maximum 100 persons long int city[50]; //city is an array to store population of maximum 50 cities int fib[15]; //fib is an array to store maximum 15 elements of the fibonacci sequence It is convenient to define array size in terms of a symbolic constant rather than a fixed integer quantity. This makes it easier to modify a program that utilizes an array, since all references to the maximum array size (eg. within for loops as well as in array definitions) can be altered simply by changing the value of the symbolic constant. Example4.3: It is convenient to declare # define SIZE 50; int a[size]; rather than int a[50]; Arrays 88

3 4.3 SUBSCRIPT To refer the elements of an array subscript is used. In an array a with 50 elements, the individual elements are referred by a[0], a[1], a[2],.., a[48], a[49] as shown below: a[0] a[1] a[2] a[48] a[49] Here 0,1,2,3.49 are called subscripts. The sub scripts of an array can be integer constants, integer variables or expressions that yield integers. Example4.4: If we want to represent a set of 5 numbers, say 10, 4, 18, 20, 35 by an array variable a, then we declare the array a as follows: int a[5];and the computer reserve 5 storage locations as follows: a[0] a[1] a[2] a[3] a[4] The values can be assigned to the array elements as follows: a[0] = 10 ; a[1] = 4 ; a[2] = 18 ; a[3] = 20 ; a[4] = 35 ; This could cause the array a to store the values as shown below: a[0] a[1] a[2] a[3] a[4] 4.4 STORAGE OF ONE-DIMENSIONAL ARRAYS IN MEMORY When an array is declared, memory is allocated automatically with respect to its type and size. Array elements are stored in contagious memory locations. For example, a sample layout for the onedimensional integer array a of size 6 elements declared as int a ;is shown below. Address Memory Element number a[0],a[1],a[2], represent the first, second, third etc. elements of the list &a[0],&a[1],&a[2] etc represent the addresses of the first, second, third etc positions of the array Arrays 89

4 4.5 INITIALIZATION OF ARRAY After an array is declared, its elements must be initialized. garbage. An array can be initialized at either of the following stages. Otherwise, they will contain i) At compile time ii) At run time Compile time initialization Arrays can be initialized at the time of declaration. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed within the braces and separated by commas. Syntax of array initialization is as follows: data type array-name[size]=val1, val2,..., valn; val1 is the value for the first array element, val2 is the value for the second array element, and valn is the value for the n th array element. Example4.5: int = 10, 5, 8, 6, 2; initializes integer array a such that = 10, = 5, a[2]=8, = 6, = 2. Example4.6: char color[5]= G, R, O, B, \0 ; initializes character array color such that color[0]= G, color[1]= R, color[2]= O, color[3]= B color[4]= \0 If the number of initializers in the list is less than the number of elements then only that array elements will be initialized. The remaining elements will be set to zero automatically. Example4.7: int = 10, 5, 8; will initialize the array a such that = 10, = 5, = 8, = 0, = 0. If the number of initializers is more than the array size, then a syntax error would result. Arrays 90

5 Example4.8: int = 10, 5, 8, 12, 7, 14, 20; would result in syntax error. An array can be initialized without specifying the array size. In this case, the number of initializers is used to determine the size of the array. Example4.9: int = 10, 5, 8, 12, 7, 14, 20; would initialize a seven element array of int type Run time initialization An array can be explicitly initialized at run time. Example4.10: Consider the following segment of a C Program. for (i=0; i < 20; ++i) if (i < 5) = 0.0; else = 1.0; The first 5 elements of the array a are initialized to zero while the remaining 15 elements are initialized to 1 at run time. We can also use scanf function to initialize an array. Example4.11: The statements int ; for (i=0; i < 10;++i) scanf ( %d, & ; will initialize the array elements with the values entered through the keyboard. Program 4.1 Program to store n values in an array and display them in reverse order #define SIZE 50 void main( ) int a[size],i,n; printf( Enter the number of values: ); scanf( %d,&n); printf( Enter the values: ); for(i=0; i<n; ++i) scanf( %d,&a[i]); printf( Given values in reverse order is: \n ); for(i=n-1; i>=0; --i) printf( %d\n,a[i]); Arrays 91

6 Program 4.2 Program to find the mean of n values. #include<conio.h> #define SIZE 50 main ( ) float a[size], sum, mean; int n,i; clrscr ( ); printf( Enter the number of values : ); scanf( %d, &n); printf( Enter the values : ); for (i=0; i < n; ++i) scanf( %f, & ); /* find sum */ sum = 0 ; for (i=0; i < n; ++i) sum = sum + ; mean= sum / (float)n ; printf ( Mean of the n values : = % f \n, mean); Program 4.3 Program to arrange a list of numbers in increasing order. To arrange a list of numbers in order is called sorting. Many sorting methods are available in literature. The present program uses a simple sorting method known as Bubble sort. If the list contains n elements, this method require (n-1) passes. In the first pass, adjacent pairs of elements,... are compared and the larger element is pushed down. After the first pass largest element is pushed down to the last location. In the second pass, the pairs of elements,... are compared and the larger element is pushed down. At the end of second pass, second largest element is pushed down to the last but one location. Like this at the end of the (n-1) th pass, entire array will be re-arranged in increasing order. So, a for loop for the operations in p th pass is as follows: for (j=0 ; j< n p ; + + j) if temp = ; = temp; Arrays 92

7 This loop is to be repeated (n-1) times for the values of pass number p=1,2,3,..,(n-1). Thus the program which implements the bubble sort is as follows: #include<conio.h> #define SIZE 50 void main( ) int a[size],n,i,j,temp,p; clrscr( ); printf("enter the number of values in the list: "); scanf("%d",&n); printf("enter the values: "); for(i=0; i<n; ++i) scanf("%d",&a[i]); /*sorting process*/ for(p=1; p<n; ++p) for(j=0; j<n-p; ++j) if(a[j]>a[j+1]) temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; /*output the sorted list*/ printf("the sorted list is: \n"); for(i=0; i<n; ++i) printf("%d\n",a[i]); Program 4.4 Program to search for a specified number in a given list of numbers. Given a list of n numbers, the program will search for a given number say, key in the list. Compare key with,a[n-1], whenever the key match with one of the values of the list stop the comparisons and display a message that the number is found in the list. If the key does not match with any element of the list display the message that the number is not found in the list. # include<stdio.h> # include<conio.h> # define SIZE 50 void main ( ) int i, key, n, flag = 0; int clrscr( ); printf( Enter the number of values in the list: ); scanf( %d, & n); Arrays 93

8 printf( Enter the values : ); for(i=0; i < n; ++i) scanf( %d, & ); printf( Enter the number to be searched: ); scanf( %d, &key); for(i=0; i<n; ++i) if(key = = ) flag = 1; break ; if (flag = = 1) printf( %d is in the list at the position %d \n, key, i+1); else printf ( %d is not in the list \n, key); Program 4.5 Program to merge two sorted lists The two input sorted lists are stored in the arrays a and b respectively. Merged list is stored in array c. Corresponding elements of arrays a and b are compared and the smaller element is copied into the array c. The process is repeated until one of the lists is exhausted,if the elements of array are exhausted then the remaining elements of array a are copied into the array c, This is accomplished by the loop. while (i<m) c[k]=a[i]; ++i ; ++k ; If the elements of array a are exhausted then the remaining elements of array b are copied into c. This is accomplished by the loop while(j<n) c[k]=b[j]; ++j;++k; This procedure is implemented in the following program: #include<conio.h> void main( ) int a[10],b[10],c[20],i,j,k,m,n; printf("enter the number of elements of the first list: "); scanf("%d",&m); printf("enter the elements of the first list: "); for(i=0; i<m; ++i) scanf("%d",&a[i]); printf("enter the number of elements of the second list: "); scanf("%d",&n); Arrays 94

9 for(i=0; i<n; ++i) scanf("%d",&b[i]); i=j=k=0; while(i<m&&j<n) if(a[i]<b[j]) c[k]=a[i]; ++i; else c[k]=b[j]; ++j; ++k; while(i<m) c[k]=a[i]; ++i; ++k; while(j<n) c[k]=b[j]; ++j; ++k; printf("the merged list is: \n ); for(i=0; i<m+n; ++i) printf("%d",c[i]); /*end of main*/ Program 4.6 Program to find the binary representation of decimal integer Divide given decimal integer by 2, remainder is the least significant digit of the binary representation. Take the quotient and divide it by 2,remainder is the next binary digit. Take the new quotient and divide it by 2. Repeat this process till the quotient is zero. The remainders obtained in reverse order are the digits of the binary representation. Since the remainders are to be displayed in reverse order, store them in a one-dimensional array and display them in reverse order. This procedure is implemented in the following program #include<conio.h> void main( ) short int bdigit[[16],nd,num,digit,i=0,j; clrscr(); printf( Enter the decimal number : ); scanf( %d,&num); while(n>0) Arrays 95

10 Program 4.7 digit=n%2; bdigit[i]=digit; ++i; n=n/2; nd=i; for(j=0;j<nd;++j) printf( %d,bdigit[j]); getch(); // nd is the number of digits in binary representation Program to insert an element at a given position in a list. Let the given element be represented by key and the position be represented by pos. Move down all the elements from the given position to the end by one position. Then assign the given elements to a[pos-1]. If the list contains 7 elements and the position to be inserted is 3,then move a[6] to a[7],a[5] to a[6],..a[2] to a[3]. For this, the following code can be used for(i=7;i>=3;--i) a[i]=a[i-1]; Since an element will be inserted, the new list contains one extra element. Therefore increment the value of n by 1. If this increment is done before the moving process, the code for n elements would be ++n; for(i=n-1;i>=pos;--i) a[pos]=key; a[i]=a[i-1]; The program to implement this process is: #include<conio.h> void main( ) int a[10],n,i,key,pos; printf( Enter the number of values in the list: ); scanf( %d,&n); printf( Enter the values ); for(i=0;i<n;++i) scanf( %d,&a[i]); printf( Enter the number to be inserted: ); scanf( %d,&key); printf( Enter the position: ); scanf( %d,&pos); ++n; for(i=n-1;i>=pos;--i) a[i]=a[i-1]; a[pos-1]=key; printf( The new list after insertion is :\n ); Arrays 96

11 for(i=0;i<n;++i) printf( %d\t,a[i]); getch(); Program 4.8 Program to delete an element from the list at a given position. Let the position of element to be deleted be represented by pos. It is not possible to delete an element physically from an array, but it can be overwritten by another number. After reading the value of pos move up all elements of the array from pos to the end. If the list contains 7 elements and the value of pos is 3, the following code can be used to implement this process for(i=2;i<6;++i) a[i]=a[i+1]; Since an element is to be deleted, new list contains n-1 elements only. Therefore the value of n should be decrement by 1. If this decrement is done before the process, the code for n elements would be The program is --n; for(i=pos-1;i<n;++i) a[i]=a[i+1]; #include<conio.h> void main( ) int a[10],i,pos,n; printf( Enter the number of values in the list: ); scanf( %d,&n); printf( Enter the values: ); for(i=0;i<n;++i) scanf( %d,&a[i]); printf( Enter the position: ); scanf( %d,&pos); if(pos<1 pos>n) printf( Invalid position \n ); Arrays 97

12 exit(0); --n; for(i=pos-1;i<n;++i) a[i]=a[i+1]; printf( The newlist after deletion is :\n ); for(i=0;i<n;++i) printf( %d\t,a[i]); getch(); 4.6 TWO-DIMENSIONAL ARRAYS Two-dimensional array is defined as a set of one dimensional arrays each of same size. Twodimensional array is declared in the same manner as one-dimensional arrays. It will require two pairs of square brackets. A two-dimensional array declaration can be written as we have already seen that an n-element, one-dimensional array can be thought of as a list of values. Similarly, an m n, two-dimensional array can be thought of as a table of values having m rows and n columns, as illustrated in the following figure: Row 0 data type array-name [constant-size1] [constant-size2]; Column 0 Column 1 Column 2 Column (n-1) a 0 0 a 0 1 a 0 2 a 0 n 1 a 1 0 a 1 1 a 1 2 a 1 n 1 Row 1 a 2 0 a 2 1 a 2 2 a 2 n 1 Row 2 a m 1 0 a m 2 1 a m 3 2 a m 1 n 1 Row (m-1) Arrays 98

13 Some typical two-dimensional array definitions are shown below. int mat[5][6] ; //mat is an array with 5 rows and 6 columns float sales[6][4] ; //sales is an array with 6 rows and 4 columns 4.7 INITIALIZATION OF TWO-DIMENSIONAL ARRAYS Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declarations with a list of initial values enclosed in braces. For example, int a[2][3] = 0,0,0,1,1,1; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row and is called row major ordering. The above statement can be equivalently written as int a[2][3] = 0,0,0,1,1,1, by surrounding the elements of each row by braces. When the array is completely initialized with all values, explicitly, we need not specify the row size. That is, the statement int a[ ][3] = 0,0,0, 1, 1, 1; is permitted. If the values are missing in an initialization, they are automatically set to zero. For instance, the statement int a[ 2 ][3] = 1,1, 2; is permitted and will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero, that is a[0][0]=1 a[0][1]=1 a[0][2]=0 a[1][0]=2 a[1][1]=0 a[1][2]=0 When all the elements are to be initialized to zero, the following short-cut method may be used. int a[3][4] = 0, 0, 0; The first element of each row is explicitly initialized to zero while other elements are automatically initialized to zero. The following statement also will achieve the same result: int a[3][4] = 0; The following initialization of the array will result in syntax error, since the number of values in each inner pair of braces exceeds the defined size of the array. int a[2][3]=1,2,3,4,9,8,6,4,7; Example4.12: To store the elements of a 3x4 matrix in a two-dimensional array a, the array declaration will be Arrays 99

14 int a[3][4]; First row elements are stored in a[0][0],a[0][1],a[0][2] and a[0][3],second row elements are stored in a[1][0], a[1][1], a[1][2] and a[1][3] and third row elements are stored in a[2][0], a[2][1], a[2][2] and a[2][3]. In general, (i+1)th row elements are to be stored in a[i][0],a[i][1],a[i][2] and a[i][3] For this the following loop will be used. for(j=0; j<4; ++j) scanf("%d",&a[i][j]); To store the elements of all the rows, repeat this loop for i=0, 1, 2. Hence the required code is for(i=0; i<3;++i) for(j=0; j<4; ++j) scanf("%d",&a[i][j]); To display the elements of a 3x4 matrix in the natural form first row elements are to be displayed on the first line, second row elements are on second line and third row elements on third line. For this the following code can be used. for(i=0; i<3;++i) for(j=0; j<4; ++j) printf("%8d",a[i][j]); printf( \n ); Program 4.9 Program to add two matrices Given two matrices A of order r1 c1 and B of order r2 c2, we write a program to find their sum with suitable validation. To find the sum matrix we have to find each element of the sum matrix.in general (i,j)th element of the sum matrix C is obtained as C[i][j]= A[i][j] + B[i][j]; for all combinations of i=0,1,2,,r1-1 and j=0,1,2,c1-1. /*Program to find sum of two matrices*/ Arrays 100

15 #include<conio.h> #include <stdlib.h> #define ROWSIZE 5 #define COLSIZE 5 void main ( ) int r1, c1, r2, c2, i,j; int a[rowsize] [COLSIZE], b[rowsize] [COLSIZE],c[ROWSIZE] [COLSIZE]; printf ( Enter the number of rows and columns of first matrix: ); scanf ( %d %d, & r1, & c1); printf ( Enter the number of rows and columns of second matrix: ); scanf ( %d %d, & r2, & c2); if printf ( Given matrices are not of same order and hence addition is not possible \n ); exit (0); printf ( Enter the elements of the first matrix row wise : \n ); for (i=0 ; i < r1 ; ++i) for (j=0 ; j < c1 ; ++j) scanf ( %d, & ; printf ( Enter the elements of the second matrix row wise:\n ); for (i=0; i < r2 ; + + i) for (j=0; j < c2 ; + + j) scanf ( %d, & b ; /* add the two matrices*/ for (i=0 ; i < r1 ; ++i) for (j=0 ; j < c1 ; ++j) Arrays 101

16 /*output the sum matrix */ printf The sum matrix is : \n ); for (i=0; i < r1 ; ++i) for (j=0; j< c1 ; ++j) printf ( %8d, ; printf( \n ); /*end of main*/ Program 4.10 Program to find the product of two matrices The following procedure will be used to find the product. If A is a matrix of order m p and B is a matrix of order p n, then the (i, j) th element of the product matrix is obtained by c ij a io ai 1 ai2... a ip 1 b b b b j j j P 1 j =[a i0 b 0j +a i1 b 1j +a i2 b 2j + +a ip-1 b p-1j ] = p 1 k 0 a ik b kj Thus, the following loop can be used to find the (i, j) th element for (k = 0 ; k < p ; ++k) /*Program to find the product of two matrices*/ Arrays 102

17 #include<conio.h> #include<stdlib.h> #define ROWSIZE 5 #define COLSIZE 5 void main ( ) int i, j,k, r1, c1, r2, c2 ; int a[rowsize] [COLSIZE], b[rowsize] [COLSIZE],c[ROWSIZE] [COLSIZE]; printf ( Enter the order of the first matrix : ); scanf ( %d%d, &r1, &c1); printf ( Enter the order of the second matrix : ); scanf (%d%d, &r2, &c2); if (c1! = r2) printf ( product of given matrices does not exist \n ); exit (0); printf ( Enter the elements of first matrix row wise : \n ); for (i=0; i < r1 ; ++i) for (j=0; j < c1; ++j) scanf( %d, &a printf( Enter the elements of second matrix column wise:\n ); for(i=0; i<r2; ++i) for (j=0; j< c2; ++j scanf( %d, & b /*find the elements of product matrix*/ for(i=0; i<r1; ++i) for (j=0; j < c2; ++j) Arrays 103

18 c[i][j] = 0; for(k=0; k<c1; ++k) c[i][j] + = ; printf ( The product matrix is : \n ); for (i=0; i< r1; ++i) for (j=0; j< c2; ++j) printf ( %8d, c[i][j] ); printf( \n ); /*end of main */ Program4.11 Program to check a given square matrix is symmetric The matrix A=[a ij ] n m is symmetric if a ij =a ji for all combinations of i and j Hence, check the condition a ij =a ji for i=0,1,2,..n-2 and j=i+1,i+2, n-1. If any one pair of elements is unequal, the matrix is not symmetric #include<conio.h> void main ( ) int n,a[5][5],i,j,flag=0; printf ( Enter the order of the square matrix : ); scanf ( %d, &n); printf ( Enter the elements of first matrix row wise : \n ); for (i=0; i < n ; ++i) for (j=0; j < n; ++j) scanf( %d, &a Arrays 104

19 for(i=0;i<n-1;++i) for(j=i+1;j<n;++j) if(flag) if(a[i][j]!=a[j][i]) flag=1; break; if(flag==1) break; printf( \n Given matrix is not symmetric ); else printf( \n Given matrix is symmetric ); getch(); Note that, if any pair of elements are unequal, 1 is assigned to flag and break is executed. Since this break statement is in the inner loop only that loop execution is terminated. Hence the value of flag must be compared with 1 in the outer loop. Program4.12 Programto displayn linesof the Pascal triangle. When n=6, the triangle is as follows In each row, first and last elements are 1 s. In general i th row elements are obtained as follows: a[i][0]=a[i][i]=1 a[i][j]=a[i-1][j-1]+a[i-1][j], j=1,2,.(i-1) Arrays 105

20 Repeat this for i=0,1,2..n-1 The program which implement this procedure is : #include<conio.h> void main ( ) int a[10][10],i,j,n; printf ( Enter the value of n: ); scanf ( %d, &n); for(i=0;i<n;++i) a[i][0]=a[i][i]=1; for(j=1;j<i;++j) a[i][j]=a[i-1][j-1]+a[i-1][j]; printf( The pascal triangle is: \n ); for(i=0;i<n;++i) for(j=0;j<40-3*i;++j) //To leave 40-3i blanks in the line printf( ); for(j=0;j<=i;++j) printf( %3d,a[i][j]); Program4.13 Given the roll number and marks in three subjects of 100 students, write a program to determine the following: i. Total marks obtained by each student ii. The highest marks in each subject and the roll no, of the student who secured it. iii. The student who obtained the highest total marks. Store the roll numbers in a one dimensional integer array rno, marks in a two dimensional array m of size 100 4, where total marks are stored in 4 th column. Find the sum of marks in each row for total Arrays 106

21 marks, maximum marks in the first three columns for subject wise maximum and maximum marks in fourth column for highest total marks. Display the output in a tabular form #include<conio.h> void main ( ) int rno[100],i,s1_rno,s2_rno,s3_rno,total_rno; float m[100][4], s1_max,s2_max,s3_max,total_max; printf( Enter the Roll no. and marks in 3 subjects of 100 students: \n ); for(i=0;i<100,++i) scanf( %d%f%f%f,&rno[i],&m[i][0],&m[i][1],&m[i][2]); /* To find total marks of each student */ for(i=0;i<100;++i) m[i][3]=m[i][0]+m[i][1]+m[i][2]; /* To find subject wise maximum marks and the roll numbers of the students who secured them */ s1_max=m[0][0]; s2_max=m[0][1]; s3_max=m[0][2]; total_max=m[0][3]; s1_rno=rno[0]; s2_rno=rno[0]; s3_rno=rno[0]; total_rno=rno[0]; for(i=1;i<100;++i) if(s1_max<m[i][0]) s1_max=m[i][0]; s 1 _rno=rno[i]; Arrays 107

22 if(s2_max<m[i][1]) s2_max=m[i][1]; s2_rno=rno[i]; if(s3_max<m[i][2]) s3_max=m[i][2]; s3_rno=rno[i]; if(total_max<m[i][3]) total_max=m[i][3]; total_rno=rno[i]; printf( \n\t Roll no \t Sub1 \t Sub2 \t Sub3 \t Total\n ); for(i=0;i<100;++i) printf( \t %d \t %6.2f \t %6.2f \t %6.2f \t %6.2f \n,rno[i],m[i][0],m[i][1],m[i][2]); printf( Maximum marks in subject 1 is %6.2f and the roll number of the student is %d \n, s1_max,s1_rno); printf( Maximum marks in subject 2 is %6.2f and the roll number of the student is %d \n, s2_max, s2_rno); printf( Maximum marks in subject 3 is %6.2f and the roll number of the student is %d \n, s3_max, s3_rno); printf( Highest total marks is %6.2f and the roll number of the student is %d \n, total_max, total_rno); getch(); Arrays 108

23 SUMMARY EXERCISES C uses arrays as a way of describing a collection of data items with identical properties. The group has a single name for all its members, with the individual member being selected by an index. We have learnt in this unit, the basic purpose of using an array in the program, declaration of array and assigning values to the arrays. All elements of the arrays are stored in the contagious memory locations. Without exception, all arrays in C are indexed from 0 up to one less than the bound given in the declaration. One important point about array declaration is that they don t permit the use of varying subscripts. The numbers given must be constant expressions which can be evaluated at compile time, not run time. Global and static array elements are initialized to 0 by default, and automatic array elements are filled with garbage values. C never check whether the array index is valid either at compile time or when the program is running. Single operations, which involve entire arrays, like copying one array into another array, input or output of all the elements of the array without subscripts are not permitted in C. Multiple choice Questions: 4.1.What is the output when the following program segment is executed? int a[5]=71,82,69,69,78; for(i=0;i<5;++i) printf( %c,a[i]) a) GREEN b) c)fqddm d) ISFFO 4.2. What is the output when the following code is executed? void main() int arr[6]=12,13; printf( \n %d, %d,a[1],a[3]); a)13,garbage b)12,13 c)12,garbage d)13,0 4.3.Array name is a)an array variable b)a key word c)a common name shared by all elements d)not used in a program Arrays 109

24 4.4. Array elements occupy a)adjacent memory locations b)random location for each element c)varying length of memory locations for each element. d) no space in memory Array is used to represent a)a list of data items of integer data type. b) a list of data items of real data type. c) a list of data items of different data type. d) a list of data items of same data type Array subscripts in C always starts at a)-1 b)0 c)1 d)any value 4.7.If the size of the array is less than the number of initializes, then a) extra values are neglected b) it is an error c) the size is automatically increased d) the size is neglected What is the output when the following program is executed? main() int a[5]=1,2,3,4,5,sum=0,i=0 begin: sum+=a[i]; ++i; if(i<5) goto begin; printf( %d,sum); Arrays 110

25 a) 10 b) 1 c) 15 d) What is the output when the following program is executed? main() int a[5] = 1,3,4, i ; for ( i = 4; i> = 0; --i ) printf ( %d \t, a[i]); a) b) c) d) syntax error will occur during execution of the program What is the output when the following program is executed? main() int a[5], i; for( i=0; i < 5; ++i) a[i] = i * i ; for( i=0; i< 5 ; ++i) printf( %d, a[i]); a) b) c) d) What is the output when the following program is executed? main() int a[5] = 3,1, 10, 20, 30,i=0,x,y,z; x = ++a[1]; y = a[i++]; z = a[i]++; printf( x= %d, y = %d, z = %d \n, x, y, z ); a)x=2,y=3,z=1 b) x=4,y=3,z=1 c) x=4,y=1,z=2 d) x=3,y=3,z= Identify the error in the following code. main() Arrays 111

26 int a[5]=9,11,3,6,4,b[5],i; b=a; for(i=0;i<5;++i) printf( \n %d,b[i]); a) variable subscript i is not allowed b) since the array b is not initialized we cannot display the values of the array b c) one array cannot be assigned to another array using assignment operator d) array cannot be initialized in the type declaration statement What is the output generated by the following program segment. int a, b = 0 ; int c[10] = 1,2,3,4,5,6,7,8,9,0; for(a=0; a < 10 ; + + a) if b + = c ; printf( b = %d, b); a) b= 25 b) b= 20 c)b=45 d) What is the output generated by the following program segment? int a, b = 0 ; int c[10] = 1,2,3,4,5,6,7,8,9,0; for(a=0 ; a < 10 ; ++a) if((c[a]%2)= = 0) b + = ; printf( b=%d, b); a) b= 25 b) b= 20 c)b=45 d) What is the output generated by the following program segment? int i ; int a[5]=0; Arrays 112

27 for(i = 1; i < 5 ; ++i) = i + for(i = 0 ; i < 5 ; ++i) printf ( %d \t, ) ; a) b) c) d) unpredictable output What is the output generated by the following program segment? int a, b, c ; int x [3] [4] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; for(a=0; a < 3 ; ++a) c = 999 ; for (b=0 ; b < 4 ; ++b) if ( c =x ; printf ( %3d, c); a) b) c) d) What will be the output of the flowing program? int main() int arr[5], i=0; while(i<5) arr[i]=++i; for(i=0; i<5; i++) printf("%d\t ", arr[i]); return 0; a) b)garbage c) d) What is the output if the flowing program is executed? Arrays 113

28 main() int x[10],i,sum=0; for(i=0;i<10;i+=3) x[i]=i; sum=sum+x[i]; printf( %d \n,sum); a) 55 b) 45 c) 65 d)unpredictable output Comprehensive Questions: 4.1 Write a program to rearrange the elements of an array in reverse order without using a second array 4.2 Write a C program to find the binary equivalent of an integer number using array 4.3 Write a program to find median of a set of numbers 4.4 Write a program to compare two one dimensional arrays containing two sets of numbers 4.5 Write a program to find the median height of students in a section 4.6 Write a program to implement the binary search 4.7 A list of failed registered numbers of students is stored in an array. Write a program to determine whether a given register number is in the list using linear search 4.8 Write a program for fitting a straight line through a set of points (x i, y i ), i =1,2,... n. The straight line equation is y = mx + c and the values of m and c are given by All summations are from 1 to n. Arrays 114

29 4.9 Given a list of 200 integers each of them ranges from 0 to 10 write a program to count the number of occurrences of each character 4.10 The annual examinations results of 100 students are tabulated as follows: Roll No. Subject 1 Subject 2 Subject Write a program to read the data and determine the following: a) Total marks obtained by each student b) The highest marks in each subject and the Roll No. of the student who secured it. c) The student who secured the highest total marks Write a program to find the transpose of a matrix 4.12 Write a program to find the trace of a matrix 4.13 Write a program to check if a given matrix is symmetric 4.14 Write a program to sort the elements of each row of a matrix in ascending order 4.15 Write a program that fills a five-by-five matrix as follows: i. Upper left triangle with 1s ii. Lower right triangle with -1s iii. Right to left diagonal with zeros Display the contents of the matrix Write a program to find the transpose of a matrix without using additional array 4.17 Write a program to check if a given matrix is upper triangular 4.18 Write a program to input 10 integers and sort them. Use a menu system to determine whether the sort should be ascending or descending order. Also determine whether the elements have to be sorted by their actual value or absolute value. The process should continue till the user desires to exit. Arrays 115

Columns A[0] A[0][0] = 20 A[0][1] = 30

Columns A[0] A[0][0] = 20 A[0][1] = 30 UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

Classification s of Data Structures

Classification s of Data Structures Linear Data Structures using Sequential organization Classification s of Data Structures Types of Data Structures Arrays Declaration of arrays type arrayname [ arraysize ]; Ex-double balance[10]; Arrays

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array? 1 What is an array? Explain with Example. What are the advantages of using an array? An array is a fixed-size sequenced collection of elements of the same data type. An array is derived data type. The

More information

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

More information

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1 (a) List any four relational operators.

More information

NCS 301 DATA STRUCTURE USING C

NCS 301 DATA STRUCTURE USING C NCS 301 DATA STRUCTURE USING C Unit-1 Part-3 Arrays Hammad Mashkoor Lari Assistant Professor Allenhouse Institute of Technology www.ncs301ds.wordpress.com Introduction Array is a contiguous memory of homogeneous

More information

Introduction to Arrays

Introduction to Arrays Introduction to Arrays One Dimensional Array. Two Dimensional Array. Inserting Elements in Array. Reading Elements from an Array. Searching in Array. Sorting of an Array. Merging of 2 Arrays. What is an

More information

Single Dimension Arrays

Single Dimension Arrays ARRAYS Single Dimension Arrays Array Notion of an array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element,

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.

More information

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array Arrays Array is a collection of similar data types sharing same name or Array is a collection of related data items. Array is a derived data type. Char, float, int etc are fundamental data types used in

More information

Fundamentals of Computer Programming Using C

Fundamentals of Computer Programming Using C CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions

More information

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store either all integers, all floating point numbers, all characters,

More information

Multi-Dimensional arrays

Multi-Dimensional arrays Multi-Dimensional arrays An array having more then one dimension is known as multi dimensional arrays. Two dimensional array is also an example of multi dimensional array. One can specify as many dimensions

More information

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space

More information

Write a C program using arrays and structure

Write a C program using arrays and structure 03 Arrays and Structutes 3.1 Arrays Declaration and initialization of one dimensional, two dimensional and character arrays, accessing array elements. (10M) 3.2 Declaration and initialization of string

More information

Prepared by: Shraddha Modi

Prepared by: Shraddha Modi Prepared by: Shraddha Modi Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of

More information

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning. Chapter4: s Data: It is a collection of raw facts that has implicit meaning. Data may be single valued like ID, or multi valued like address. Information: It is the processed data having explicit meaning.

More information

Government Polytechnic Muzaffarpur.

Government Polytechnic Muzaffarpur. Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

Q 1. Attempt any TEN of the following:

Q 1. Attempt any TEN of the following: Subject Code: 17212 Model Answer Page No: 1 / 26 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The

More information

Questions Bank. 14) State any four advantages of using flow-chart

Questions Bank. 14) State any four advantages of using flow-chart Questions Bank Sub:PIC(22228) Course Code:-EJ-2I ----------------------------------------------------------------------------------------------- Chapter:-1 (Overview of C Programming)(10 Marks) 1) State

More information

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming CSE101-Lec#18 Multidimensional Arrays Application of arrays Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Defining and processing 1D array 2D array Applications of arrays 1-D array A

More information

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos. UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation

More information

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

More information

Objectives of This Chapter

Objectives of This Chapter Chapter 6 C Arrays Objectives of This Chapter Array data structures to represent the set of values. Defining and initializing arrays. Defining symbolic constant in a program. Using arrays to store, list,

More information

UNIT-I Fundamental Notations

UNIT-I Fundamental Notations UNIT-I Fundamental Notations Introduction to Data Structure We know that data are simply values or set of values and information is the processed data. And actually the concept of data structure is much

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

F.E. Sem. II. Structured Programming Approach

F.E. Sem. II. Structured Programming Approach F.E. Sem. II Structured Programming Approach Time : 3 Hrs.] Mumbai University Examination Paper Solution - May 14 [Marks : 80 Q.1(a) What do you mean by algorithm? Which points you should consider [4]

More information

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur An Example: Random

More information

Program. SUBJECT: ACP (C - Programming) Array Program. // Find the minimum number from given N element. Prepared By : Dhaval R.

Program. SUBJECT: ACP (C - Programming) Array Program. // Find the minimum number from given N element. Prepared By : Dhaval R. Program // Find the minimum number from given N element. #include #include void main() int a[50],i,n,min; clrscr(); printf("\n Enter array size : "); scanf("%d",&n); printf("\n Enter

More information

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection Morteza Noferesti Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities Continuous Internal Evaluation Test 2 Date: 0-10- 2017 Marks: 0 Subject &

More information

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani Arrays Dr. Madhumita Sengupta Assistant Professor IIIT Kalyani INTRODUCTION An array is a collection of similar data s / data types. The s of the array are stored in consecutive memory locations and are

More information

Arrays. Elementary Data Representation Different Data Structure Operation on Data Structure Arrays

Arrays. Elementary Data Representation Different Data Structure Operation on Data Structure Arrays Arrays Elementary Data Representation Different Data Structure Operation on Data Structure Arrays Elementary Data Representation Data can be in the form of raw data, data items and data structure. Raw

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

One Dimension Arrays 1

One Dimension Arrays 1 One Dimension Arrays 1 Array n Many applications require multiple data items that have common characteristics In mathematics, we often express such groups of data items in indexed form: n x 1, x 2, x 3,,

More information

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

Contents ARRAYS. Introduction:

Contents ARRAYS. Introduction: UNIT-III ARRAYS AND STRINGS Contents Single and Multidimensional Arrays: Array Declaration and Initialization of arrays Arrays as function arguments. Strings: Initialization and String handling functions.

More information

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP Contents 1. WAP to accept the value from the user and exchange the values.... 2 2. WAP to check whether the number is even or odd.... 2 3. WAP to Check Odd or Even Using Conditional Operator... 3 4. WAP

More information

Types of Data Structures

Types of Data Structures DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data

More information

UNIT III ARRAYS AND STRINGS

UNIT III ARRAYS AND STRINGS UNIT III ARRAYS AND STRINGS Arrays Initialization Declaration One dimensional and Two dimensional arrays. String- String operations String Arrays. Simple programs- sorting- searching matrix operations.

More information

Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES. Sushil Kumar Saini Mo.

Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES. Sushil Kumar Saini   Mo. Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES Sushil Kumar Saini Email: Sushilsaini04@gmail.com Mo. 9896470047 Integer Array int a[]=12,34,54,45,34,34; printf("%d",a[0]); printf(" %d",a[1]);

More information

Computers Programming Course 12. Iulian Năstac

Computers Programming Course 12. Iulian Năstac Computers Programming Course 12 Iulian Năstac Recap from previous course Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 =

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

UNIT - V STRUCTURES AND UNIONS

UNIT - V STRUCTURES AND UNIONS UNIT - V STRUCTURES AND UNIONS STRUCTURE DEFINITION A structure definition creates a format that may be used to declare structure variables. Let us use an example to illustrate the process of structure

More information

Arrays in C. By Mrs. Manisha Kuveskar.

Arrays in C. By Mrs. Manisha Kuveskar. Arrays in C By Mrs. Manisha Kuveskar. C Programming Arrays An array is a collection of data that holds fixed number of values of same type. For example: if you want to store marks of 100 students, you

More information

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1 IIT Patna 1 Array Arijit Mondal Dept. of Computer Science & Engineering Indian Institute of Technology Patna arijit@iitp.ac.in Array IIT Patna 2 Many applications require multiple data items that have

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

Tribhuvan University Institute of Science and Technology 2065

Tribhuvan University Institute of Science and Technology 2065 1CSc.102-2065 2065 Candidates are required to give their answers in their own words as for as practicable. 1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain

More information

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

More information

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare

More information

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example: Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x

More information

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan Arrays in C DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) Array C language provides a

More information

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the

More information

Arrays and Strings. Arash Rafiey. September 12, 2017

Arrays and Strings. Arash Rafiey. September 12, 2017 September 12, 2017 Arrays Array is a collection of variables with the same data type. Arrays Array is a collection of variables with the same data type. Instead of declaring individual variables, such

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,

More information

Question Bank (SPA SEM II)

Question Bank (SPA SEM II) Question Bank (SPA SEM II) 1. Storage classes in C (Refer notes Page No 52) 2. Difference between function declaration and function definition (This question is solved in the note book). But solution is

More information

Downloaded from

Downloaded from Unit-II Data Structure Arrays, Stacks, Queues And Linked List Chapter: 06 In Computer Science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

More information

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER 2017-2018 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial#4 (SOLUTION) DATE : 09-NOV-2017 Answer 1(a). #include

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

Numerical Method (2068 Third Batch)

Numerical Method (2068 Third Batch) 1. Define the types of error in numerical calculation. Derive the formula for secant method and illustrate the method by figure. There are different types of error in numerical calculation. Some of them

More information

MODULE 1. Introduction to Data Structures

MODULE 1. Introduction to Data Structures MODULE 1 Introduction to Data Structures Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way. Data Structures is about

More information

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 Arrays Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 6 - Arrays 6.1 Introduction 6.2 Arrays 6.3 Declaring Arrays 6.4 Examples Using Arrays

More information

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal MA 511: Computer Programming Lecture 3: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Last

More information

DC54 DATA STRUCTURES DEC 2014

DC54 DATA STRUCTURES DEC 2014 Q.2 a. Write a function that computes x^y using Recursion. The property that x^y is simply a product of x and x^(y-1 ). For example, 5^4= 5 * 5^3. The recursive definition of x^y can be represented as

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

MAHARASHTRASTATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRASTATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Code: 17330 Model Answer Page 1/ 22 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model

More information

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

How to declare an array in C?

How to declare an array in C? Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

Algorithms Lab (NCS 551)

Algorithms Lab (NCS 551) DRONACHARYA GROUP OF INSTITUTIONS, GREATER NOIDA Affiliated to Utter Pradesh Technical University Noida Approved by AICTE Algorithms Lab (NCS 551) SOLUTIONS 1. PROGRAM TO IMPLEMENT INSERTION SORT. #include

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

More information

Content. In this chapter, you will learn:

Content. In this chapter, you will learn: ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

More information

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, 2016-17) SOLUTION Program: B. Tech. Branch: All Term:I Subject: Logic Building and Problem Solving Using C Paper Code:

More information

Principles of Programming. Chapter 6: Arrays

Principles of Programming. Chapter 6: Arrays Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching

More information

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS Analysis of simple Algorithms Structure Page Nos. 3.0 Introduction 85 3.1 Objective 85 3.2 Euclid Algorithm for GCD 86 3.3 Horner s Rule for Polynomial Evaluation

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

Procedural Programming

Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number: UNIVERSITY OF WINDSOR 60-106-01 Fall 2007 QUIZ # 2 Solution Examiner : Ritu Chaturvedi Dated :November 27th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed.

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays Maltepe University Computer Engineering Department BİL 133 Algorithms and Programming Chapter 8: Arrays What is an Array? Scalar data types use a single memory cell to store a single value. For many problems

More information

Software Testing Laboratory Lab Assignments

Software Testing Laboratory Lab Assignments Problem Statement 01 (Week 01) Software Testing Laboratory Lab Assignments Consider an automated banking application. The user can dial the bank from a personal computer, provide a six-digit password,

More information

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional

More information

Sorting & Searching. Hours: 10. Marks: 16

Sorting & Searching. Hours: 10. Marks: 16 Sorting & Searching CONTENTS 2.1 Sorting Techniques 1. Introduction 2. Selection sort 3. Insertion sort 4. Bubble sort 5. Merge sort 6. Radix sort ( Only algorithm ) 7. Shell sort ( Only algorithm ) 8.

More information