Vous êtes sur la page 1sur 11

PERI INSTITUTE OF TECHNOLOGY

MANNIVAKKAM, CHENNAI-48

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING CONTINUOUS ASSESMENT TEST-III - ANSWER KEY GE6151- COMPUTER PROGRAMMING

1. What is an array? An array is a group of similar data types stored under a common name. 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. Example: int a 1!"# $ere a 1!" is an array %ith 1! values. &. 'efine (trings %ith example. (trings: )he group of characters, digit and symbols enclosed %ithin *uotes is called as (tirng +or, character Arrays. (trings are al%ays terminated %ith -.!/ +0122, character. )he compiler automatically adds -.!/ at the end of the strings. Example: char name "34-5/,/6/,/2/,/2/,/E/,/7/,/E/,/E/,/.!/8# 9. What are functions and ho% %ill define a function in 5?

A function is a group of statements that together perform a task. Every 5 program has at least one function %hich is main+,, and all the most trivial programs can define additional functions.
'efining a :unction:

)he general form of a function definition in 5 programming language is as follo%s:


return_type function_name( parameter list ) { body of the function }

A function definition in 5 programming language consists of a function header and a function body. $ere are all the of a function: 1. &. 9. =. ;eturn )ype :unction 0ame <arameters :unction >ody

=. ?ention the various (tring ?anipulation :unctions in 5.

strcpy+s1, s&,# 5opies string s& into string s1. strcat+s1, s&,# 5oncatenates string s& onto the end of string s1. strlen+s1,# ;eturns the length of string s1. strcmp+s1, s&,# ;eturns ! if s1 and s& are the same# strstr+s1, s&,# ;eturns a pointer to the first occurrence of string s& in string s1. @. What are the t%o types of passing parameters to a function?

<assing <arameters to a :unction )here are t%o %ays to pass parameters to a function:

<ass by Aalue: mechanism is used %hen you donBt %ant to change the value of passed paramters. When parameters are passed by value then functions in 5 create copies of the passed in variables and do re*uired processing on these copied variables. <ass by ;eference mechanism is used %hen you %ant a function to do the changes in passed parameters and reflect those changes back to the calling function. Cn this case only addresses of the variables are passed to a function so that function can %ork directly over the addresses. What are formal and actual parameters? )he parameters %ritten in function definition is called as E:ormal <arametersF.

D.

:ormal <arameters: Actual <arameters: )he parameters %ritten in function call is called EActual <arametersF. Example:

$ere num1 is EActual <arametersF and para1 is E:ormal <arametersF. G. What are the four types of functions in 5? :unction %ith no arguments and no return value :unction %ith no arguments and return value :unction %ith arguments but no return value :unction %ith arguments and return value.

H.

What are I and J operators means? -I/ operator means -value at the address/ -J/ operator means -address of/

K.

What is a <ointer? $o% a variable is declared to the pointer?

<ointer is a variable %hich holds the address of another variable. <ointer 'eclaration: datatype IvariableLname# Example: int Ix, c3@# x3Ja#
1!.What is multi dimension array.

)he array %e used in the last example %as a one dimensional array. Arrays can have more than one dimension, these arraysLofLarrays are called multidimensional arrays. )hey are very similar to standard arrays %ith the exception that they have multiple sets of s*uare brackets after the array identifier.
<A;) L > 1. Write a 5 program to do the matrix multiplication using 5. &. )%oL'imensional Arrays:

)he simplest form of the multidimensional array is the t%oLdimensional array. A t%oLdimensional array is, in essence, a list of oneLdimensional arrays. )o declare a t%oLdimensional integer array: datatype array0ame x " y "# Where type can be any valid 5 data type and array0ame %ill be a valid 5 identifier. A t%oLdimensional array can be think as a table %hich %ill have x number of ro%s and y number of columns.

A 2-dimensional array a, which contains three rows and four columns can be shown as below:

)hus, every element in array a is identified by an element name of the form a i " M ", %here a is the name of the array, and i and M are the subscripts that uni*uely identify each element in a. CnitialiNing )%oL'imensional Arrays:

?ultidimensional arrays may be initialiNed by specifying bracketed values for each ro%. :ollo%ing is an array %ith 9 ro%s and each ro% has = columns. int a 9" =" 3 4 4!, 1, &, 98 , OI initialiNers for ro% indexed by ! IO 4=, @, D, G8 , OI initialiNers for ro% indexed by 1 IO 4H, K, 1!, 118 OI initialiNers for ro% indexed by & IO 8# )he nested braces, %hich indicate the intended ro%, are optional. )he follo%ing initialiNation is e*uivalent to previous example: int a 9" =" 3 4!,1,&,9,=,@,D,G,H,K,1!,118# Accessing )%oL'imensional Array Elements: An element in &Ldimensional array is accessed by using the subscripts, i.e., ro% index and column index of the array. :or example: int val 3 a &" 9"# )he above statement %ill take =th element from the 9rd ro% of the array. We can verify it in the above diagram. <rogram to handle a t%o dimensional array: Pinclude Qstdio.hR PincludeQconio.hR void main +, 4 OI an array %ith @ ro%s and & columnsIO int a @" &" 3 4 4!,!8, 41,&8, 4&,=8, 49,D8,4=,H88# int i, M# OI output each array elementBs value IO for + i 3 !# i Q @# iSS , 4 for + M 3 !# M Q &# MSS , 4 printf+Ta Ud" Ud" 3 Ud.nT, i,M, a i" M" ,# 8 8 getch+,# 8 When the above code is compiled and executed, it produces the follo%ing result: a !" !": ! a !" 1": ! a 1" !": 1 a 1" 1": & a &" !": & a &" 1": =

a a a a

9" 9" =" ="

!": 9 1": D !": = 1": H

9. 'efine ;ecursion in 5. Explain the concept of recursion %ith example.

)his is %here a function repeatedly calls itself to perform calculations. )ypical applications are games and (orting trees and lists. 5onsider the calculation of DV + D factorial , ie DV 3 D I @ I = I 9 I & I 1 DV 3 D I @V DV 3 D I + D L 1 ,V nV 3 n I + n L 1 ,V <rogram: PincludeQstdio.hR int fact+int n, 4 int x31# %hile+nR1, 4 x3nIfact+nL1,# 8 return+x,# 8 void main+, 4 int n,fact1# scanf+TUdT,Jn,# fact13fact+n,# printf+TUdT,fact1,# 8 Advantages and 'isadvantages of ;ecursion ;ecursion is more elegant and re*uires fe% variables %hich make program clean. ;ecursion can be used to replace complex nesting code by dividing the problem into same problem of its subLtype. Cn other hand, it is hard to think the logic of a recursive function. Ct is also difficult to debug the code containing recursion.

=. Write a 5 <rogram to find %hether the given string is <alindrome or 0ot.

@.Write a program to perform diagonal matrix to a given matrix: & 9 = G @ K = D H


6utput needed:

& ! ! ! @ ! ! ! H
D. Write a program for finding 7reatest of 0 0umbers: G. Explain +i,5all by value +ii, 5all by reference %ith example. +i, 5all by Aalue: When the value is passed directly to the function it is called call by value. Cn call by value only a copy of the variable is only passed so any changes made to the variable does not reflects in the calling function. Example: write a program compulsory +ii,5all by ;eference: Example: write a program compulsory

H. :unctions: A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a 5 <rogram as re*uired. :eatures: :unction groups a number of program statements into a unit and gives it a name. )his unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Cnstead its re*uests other program like entities W called functions in 5 W to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

(tructure of a :unction: )here are t%o main parts of the function. )he function header and the function body. int sum+int x, int y, 4 int ans 3 !# ans 3 x S y# return ans

OOholds the ans%er that %ill be returned OOcalculate the sum OOreturn the ans%er

8 :unction $eader: Cn the first line of the above code int sum+int x, int y, Ct has three main parts )he name of the function i.e. sum )he parameters of the function enclosed in paranthesis ;eturn value type i.e. int

:unction >ody: )he content is %ritten %ith in 4 8 in the above example is the body of the function. :unction <rototypes: )he prototype of a function provides the basic information about a function %hich tells the compiler that the function is used correctly or not. Ct contains the same information as the function header contains. )he prototype of the function in the above example %ould be like int sum +int x, int y,# )he only difference bet%een the header and the prototype is the semicolon # there must the a semicolon at the end of the prototype. 1. &. 9. =. :unction %ithout return type and %ithout argument :unction %ithout return type and %ith argument :unction %ith return type and %ithout argument :unction %ith return type and %ith argument

:unction %ithout return type and %ithout argument: PincludeQstdio.hR PincludeQconio.hR void add+, 4 int a,b,c# printf+T.n.nEnter & numbers:T,# scanf+TUdUdT,Ja,Jb,# c3aSb# printf+T.n(um: UdT,c,# 8 void main+, 4 add+,# getch+,# 8 :unction %ithout return type and %ith argument :

PincludeQstdio.hR PincludeQconio.hR void add+int a,int b, 4 int c# c3aSb# printf+T.n(um: UdT,c,# 8 void main+, 4 int a,b# printf+T.n.nEnter & numbers:T,# scanf+TUdUdT,Ja,Jb,# add+a,b,# getch+,# 8 :unction %ith return type and %ithout argument: PincludeQstdio.hR PincludeQconio.hR int add+, 4 int a,b,c# printf+T.n.nEnter & numbers:T,# scanf+TUdUdT,Ja,Jb,# c3aSb# return+c,# 8 void main+, 4 printf+T.n(um: UdT,add+,,# getch+,# 8

:unction %ith return type and %ith argument: PincludeQstdio.hR PincludeQconio.hR int add+, 4 int c# c3aSb# return+c,# 8 void main+, 4 int a,b# printf+T.n.nEnter & numbers:T,# scanf+TUdUdT,Ja,Jb,#

printf+T.n(um: UdT,add+a,b,,# getch+,# 8 K. Write a note on ho% to pass an array of elements from one function and another 1!. <ointers: A pointer is a variable %hose value is the address of another variable, i.e., direct address of the memory location. 2ike any variable or constant, you must declare a pointer before you can use it to store any variable address. )he general form of a pointer variable declaration is: dataXtype IvarLname# $ere, type is the pointerBs base type# it must be a valid 5 data type and varLname is the name of the pointer variable. )he asterisk I you used to declare a pointer is the same asterisk that you use for multiplication. Array of <ointers: PincludeQstdio.hR PincludeQconio.hR void main +, 4 int var " 3 41!, 1!!, &!!8# int i,Iptr 9"# for +i3!#iQ9#iSS, 4 ptr i" 3 Jvar i"# 8 for+i3!#iQ9#iSS, 4 printf+TAalue of var Ud" 3 Ud.nT, i, Iptr i" ,# 8 getch+,# 8 <ointer and Array in <ointer Arithmetic: )here are four arithmetic operators that can be used on pointers: SS, LL, S, L. Pinclude Qstdio.hR
void main () { int numbers[5]; int * p; p = numbers; *p = !; p""; *p = #!; p = $numbers[#]; *p = %!; p = numbers " %;

*p = &!; p = numbers; ''numbers[!] *(p"&) = 5!; for +int n=!; n(5; n"") printf() *d+,numbers[n];

getch+,#
}

6utput: 1! 1@+b,. <ointer to <ointer: A pointer to a pointer is a form of multiple inLdirection, or a chain of pointers. 0ormally, a pointer contains the address of a variable. When %e define a pointer to a pointer, the first pointer contains the address of the second pointer, %hich points to the location that contains the actual value as sho%n belo%. &! 9! =! @!

A variable that is a pointer to a pointer must be declared as such. )his is done by placing an additional asterisk in front of its name. :or example, follo%ing is the declaration to declare a pointer to a pointer of type int: int IIvar# <rogram: PincludeQstdio.hR PincludeQconio.hR void main +, 4 int var31!!# int Iptr# int IIpptr# ptr 3 Jvar# pptr 3 Jptr# printf+TAalue of var 3 Ud.nT, var ,# printf+TAalue available at Iptr 3 Ud.nT, Iptr ,# printf+TAalue available at IIpptr 3 Ud.nT, IIpptr,# getch+,# 8 6utput: Aalue of var 3 9!!! Aalue available at Iptr 3 9!!! Aalue available at IIpptr 3 9!!! 0122 <ointers in 5:

Ct is al%ays a good practice to assign a 0122 value to a pointer variable in case you do not have exact address to be assigned. )his is done at the time of variable declaration. A pointer that is assigned 0122 is called a null pointer. )he 0122 pointer is a constant %ith a value of Nero defined in several standard libraries. 5onsider the follo%ing program: Pinclude Qstdio.hR
void main () {

int Iptr 3 0122# printf+T)he value of ptr is : Ux.nT, ptr ,# getch+,# 8 When the above code is compiled and executed, it produces the follo%ing result: )he value of ptr is !