Sie sind auf Seite 1von 15
SECOND TERM EXAMINATION SECOND SEMESTER (B.TECH) [APRIL 2014] INTRODUCTION TO PROGRAMMING [ETCS-108] Time : 1.30. hrs. M.M.:30 Note: Question I is compulsory. Attempt any two from rest of two. Each question is of 10 marks. Q.1(a) Define the terms: Structure and Union. Ans. Structures are used to group together different types of variables under the same name. For example you could create a structure “student”: which is Le Up ofa string (that is used to hold the name of the student) and an integer (that is used to hol the student.For example: Struct student { (5x2) char name[20}; int id } 81,82; Union is also a derived type as structure. Union can be defined in same manner as structures just the keyword used in defining union in union where keyword used in defining structure was struct. For example: Union car { char name[50]; int price; }c1,c2; There is difference in memory allocation between union and structure as suggested in above example: The amount of memory required to store a structure variables is the sum of memory size of all members. Q.1(b) Differentiate between malloc() and calloc(). Ans. When calloc is used te allocate a block of memory, the allocated region is initialized to zeroes. In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values. This could potentially be a security risk because the contents of memory are unpredictable and programming errors may result in a leak of these contents. malloc) | Allocates requested size of bytes and returns a pointer first byte of allocated space calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory Q.1(c) What is the purpose of initialization of an array. Write suitable array declaration for the following: Ans. Array is a data structure, which 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 ote" more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number, -» #4 number99, we declare one array variable such as Arr[100] which will use numbe ] 108] 1.M.:30 9. Each (5x2) der the up ofa to hola anner lused re as rea on is sated bea rrors tion fen and ps as ik LP. University-(B.Tech)-Akash Books 2014-7 numbers(0], numbers{1}, and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. : Allarrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. First Element Last Element | Numbers{0] | Numbers{1] |Numbers{2] | Numbers{3] Array Syntax: ‘Type arrayNamefarraysize]; @) 100 items of integer type: int arr{100]; Gi) A string of 26 characters: char str[25}; (ii) A matrix of order 5x4: int arr{5][4]; Q.1(d) How a string can be stored in an array? Ans. In C, a string can be referred either using a character pointer or as a character array. Strings as character arrays char str{4] ="GfG"; /*One extra for string terminator*/ rm OR*/ char str[4] = (G’, f, ‘G’, ‘\0}; *‘\0'is string terminator */ When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[J is an auto variable then string is stored in stack segment, if it's a global or static variable then stored in data segment, etc. Q.1(e) Give the output: Void main() { Int a=10,"p, a; Q=&P; Printf(“ %d%d%u%u%uU”,"p,"*q,P, } Ans.* p=10,**q=10,p=65231,q=65432,*q=65231 Q.2. (a) Write a program that reads two matrices of the order mxn and compute the following (5+2) ‘qi C=AxB Ans. #include int main() { int m,n, p,q, ¢, d, k, sum = 0; int first{10}{10], second{10}[ 10}, multiply{10}{ 10); printt(“Enter the number of rows and columns of first matrix\n"), scant(“%d%d", &m, &n); print((“Enter the elements of first matrix\n”); 2014 Second Bemester, Introduction to Programming for(c#0,c

Das könnte Ihnen auch gefallen