Top Banner
INTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q1.(a) Define data flow diagrams. Why they are used? Ans. (a) A data flow diagram (DFD) is a graphical representation of the “flow” of data through an information system, modeling its process aspects. Often they are a preliminary step used to create an overview of the system which can later be elaborated. DFDs can also be used for the visualization of data processing (structured design). A DFD shows what kinds of information will be input to and output from the system, where the data will come from and go to, and where the data will be stored. It does not show information about the timing of processes, or information about whether processes will operate in sequence or in parallel. Q1.(b) What is program? Explain the various classification of the Programming Language. Ans. Program is a set of instructions executed in a sequential manner. C program consists of preprocessor commands, a global declaration section & one or more functions. Structure of C program Preprocessor Directives Global Declarations main () { Local Declarations Statements }
26

Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Mar 28, 2018

Download

Documents

lythu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

INTRODUCTION TO PROGRAMMING(ETCS 108)

Model Question Paper

Q.1. Short answer type: (5×5 = 25)Q1.(a) Define data flow diagrams. Why they are used?Ans. (a) A data flow diagram (DFD) is a graphical representation of the “flow” ofdata through an information system, modeling its process aspects. Often they are apreliminary step used to create an overview of the system which can later be elaborated.DFDs can also be used for the visualization of data processing (structured design).A DFD shows what kinds of information will be input to and output from the system, where the data will come from and go to, and where the data will be stored. It does not show information about the timing of processes, or information about whetherprocesses will operate in sequence or in parallel.

Q1.(b) What is program? Explain the various classification of the Programming Language.Ans. Program is a set of instructions executed in a sequential manner.C program consists of preprocessor commands, a global declaration section & oneor more functions. Structure of C programPreprocessor DirectivesGlobal Declarationsmain (){Local DeclarationsStatements}Function (){Local DeclarationsStatements}Function N(){

Page 2: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Local DeclarationsStatements}

Example To print ‘Hello’ in C#include<stdio.h>int main (){printf (‘‘Hello’’);return 0;}OutputHello

Classification of Programming Languages1. Machine language2. Assembly language3. High level language (also known as Third Generation Language)4. Very high level language

1. Machine Language: It is the only language that computer understands. All thecommands & data values are expressed using 1’s & 0’s.This is the lowest level of programming language.Advantage of M/c L/g: The code can run very fast & efficiently since it is directlyexecuted by CPU.

2. Assembly Language: These are symbolic programming language that usesymbolic notation to represent machine languae instruction. It uses symbolic codesalso known as Mnemonic codes that are easy to remember abbreviations.An assembly language statement consists of a label, an operation code & one ormore operands.Advantage: The code will again execute very fast.Disadvantage: 1. Less portable, 2. Code not machine independent.

3. High Level Language: Programs were written in English like statements,making them more convenient to use & giving the programmer more time to address aclients problem.Example: COBOL, FORTRAN, BASIC, C++, C.

Page 3: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

But C & FORTRAN combine some of the flexibility of assembly language with thepower of high level language.Advantages: 1. It is easier to write and debug a program.2. The programs written in HLL are portable b/w machines.

Q1. (c) Explains (i) break (ii) continue.Ans. (i) The break statement is used to terminate the execution of the nearestenclosing loop in which it appears.• The break statement is used with for loop, while loop and do while loop.• Syntax: break; [Just write break & terminate it with a semicolon].• When compiler encounters a break statement, the control passes to thestatement that follows the loop in which the break statement appears.

Example 1 :while (...){........if (condition)break; Transfer..... control out of the while loop}.....Example 2 :for (...){......if (condition)break;...... Transfer control out of} the For Loop......Example 3 :#include <stdio.h>int main (){init i = 0;while (i<10){if (i==5)break;Printf (‘‘%d’’, i);i = i+1;}return 0;

Page 4: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

}O/P: The code is meant to print first 10 numbers using a while loop, but it willactually print only numbers from 0 to 4. As soon as ‘i’ becomes equal to 5, the breakstatement is executed & the control jumps to the statement following the while loop.

(ii) Continue Statement: When the compiler encounters a continue statement,then the rest of the statements in the loop are skipped and the control is unconditionallytransferred to the loop-continuation portion of the nearest loop.Syntax: continue;Example 1 :While (...){............if (condition)continue;............}............

Example 2 :#include <stdio.h>int main (){int i;for (i=0; i<=10; i++){if (i==5)continue;printf (‘‘/t%d’’, i);i=i+1;}return 0;O/P : 0 1 2 3 4 5 6 7 8 9 10The code given here is meant to print nos from 0 to 10. But as soon as i becomesequal to 5, the continue strnt. is encountered, so rest of the statements in the for loopare skipped & control passes to the expression that increments the value of i.

Q1. (d) Explain the concept of Union in C langue?

Page 5: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Ans. A union is a collection of variables of different data types. It is a chink ofmemory that is used to store variables of different types.When a new value is assigned to a field, the existing data is replaced with the newdata. The unions are used to save memory. They are useful for applications that involvemultiple members, where values need not be assigned to all members at any one time.Declaring a UnionSyntax :Union union-name{datatype var-name1;datatype var-name2;.......};Accessing a Member of a Union: To access the fields of a union, use the dot (⋅)operator.Initializing Unions: In union, the fields share same memory space, so fresh datareplaces any existing dta.The size of union is the size of its largest field. The typedef keyword can be used tosimplify the declaration of union variables. The difference between structure & union isthat in case of union, one can store information in one field at any one time.Example :# include <studio.h>typedef struct Pr{int x, y;};typedef union Pq{int x;int y;};main (){Pr P1 = {2, 3);|| Pq P2 = {4, 5}; Illegal with unionPq P2;P2.x = 4P2.y = 5;Printff (‘‘%d%d’’, P1.x, P1.y);

Page 6: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Printff (‘‘%d%d’’, P2.x, P2.y);return 0;}O/Pcoordinates of P1 are 2 & 3coordinates of P2 are 4 & 5

Q1. (e) Explain the use of 2-d array in C.Ans. 2-D arrays are used in C when me want to store data in the form of matricesor tables. A 2-D array is specified using two subscripts where one subscript denotes row& the other denotes column. A 2D array can be viewed as an array of arrays.Example :int marks [2] [3];Each dimension of the 2-D array is indexed from zero to its maximum size minus 1.The first index selects the row & the second selects the column.

A 2-D array is stored sequentially in memory and not as a rectangular grid form.

Write a program to print the elements of a 2D array.# include <stdio.h># include <conio.h>main (){int arr [2] [2] = {12, 34, 56, 32};int i, j;for (i = 0; i < 2; i++){printf (‘‘/n’’);for (j = 0; j < 2; j++)printf (‘‘%d/t’’, arr[i] [j]);}return 0;}Output12 3456 32

Q. 2. (a) Define Flow Chart. What are various symbols used in making flowchart? Ans. A flowchart is a pictorial representation of an algorithm. It is a program planning

Page 7: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

tool for visually organizing a sequence of steps necessary to solve a problem using a computer. It uses boxes of different shapes to denote different types of instructions.

The main advantage of using flowchart is that a programmer need not pay attentionto the details of the elements of the programming language. The process of drawing aflowchart for an algorithm is flowcharting.

Flowchart Symbols: A flowchart uses boxes of different shapes to denote differenttypes of instruction. The use of symbols makes it easier to communicate program logicthrough softwares.The ANSI (American National Standards Institute) has standardized these basicflowchart symbols.1. Terminal symbol.It indicates start, stop in a program’s logical flow.2. Input/Output symbol.It denotes any function of an i/p or o/p in a program.3. Processing SymbolIt represents arithmetic & data movement instructions. Hence all arithmeticoperations like addition, subtraction, multiplication, division are indicatedby a processing symbol in flowchart.4. Decision symbolThe decision symbol indicates a decision point, i.e., a point at which a branchto one of two or more alternative points is possible.5. Flow linesIt indicates the exact sequence in which instructions are executed.6. Connectors.Wherever a flowchart becomes so complex that the number & direction of flowlines.

Q.2. (b) Explain the concept of algorithm with the help of example. Ans. Planning a program involves defining its logic. Thus, the algorithm refers tothe logic of a program. It is a step by step description of how to arrive at a solution to agiven problem. It is defined as a sequence of instructions that when executed in thespecified sequence the desired results are obtained.The characteristics of on algorithm are :1. Each instruction should be precise & unambiguous.

Page 8: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

2. Each instruction should be executed in a finite time.3. One or more instructions should not be repeated infinitely. This ensures thatthe algorithm will ultimately terminate.4. After executing the instructions, desired results are obtained.

Example: Write an algorithm to print the total number of students who passed inFirst Division. Fifty students appeared for test in Final Exam.

Algo :Step 1 : Initialize Total_F_Div and Total_Mksheet to zeroStep 2 : Take the mark sheet of next student.Step 3 : Check the division column of the marksheet to see if it is FIRST. If no, GotoStep 5.Step 4 : Add 1 to Total_F_DivStep 5 : Add 1 to Total_MksheetStep 6 : Is total_Mksheet = 50? If no, goto step 2Step 7 : Print Total_F_DivStep 8 : Stop.

Q. 3. (a) What is the difference between algorithm and flow chart? Draw aflow chart to calculage the simple interest and also write the algorithm for thisprogramAns. 1. Flowchart is the pictorial representation of an algorithm.2. Flowchart is a way to represent Algorithm.3. Algorithm refer to logic of a program whereas flowchart is a tool for visually Organizing a sequence of steps.4. Algorithm is step by step description of how to arrive at a solution to a given problem whereas flowchart shows the flow of operations in pictorial form.

Flowchart to calculate Simple Interest :StartRead P, R , T(P = Principal amountR = Rate of interestT = Time in yrs.)SI = P × R × T/ 100Print SIStop

Page 9: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Algorithm to calculate Simple Interest :1. Initialize Simple-Interest to Zero.2. Take are values of P, R, T from user.3. Calculate Simple Interest4. Simple-Interest = P * R * T/100.5. Print Simple-Interest.6. Stop.

Q. 4. (a) Explain the various Data types in C language. Ans. 1. Fundamental data TypeIntegerInteger data type is used to store numeric values without any decimal point e.g. 7,–101, 107, etc.A variable declared as ‘int’ must contain whole numbers e.g. age is always in number.Syntax:Int variable name;E.g. int roll, marks, age;FloatFloat data type is used to store numeric values with decimal point. In other words,float data type is used to store-real values, e.g. 3.14, 7.67 etc. A variable declared asfloat must contain decimal values e.g. percentage, price, area etc. may contain realvalues.Syntax:Float variable name;E.g. float per, area;CharacterChar (Character) data type is used to store single character, within single quotese.g. ‘a’, ‘z’, ‘e’ etc. A variable declared as ‘char’ can store only single character e.g. yes or NoChoice requires only ‘y’ or ‘n’ as an answer.Syntax:Char variable name;E.g. char chi=‘a’, cha;VoidVoid data type is used to represent an empty value. It is used as a return type if afunction does not return any value.

Page 10: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Type modifierType modifier is used to change the meaning of basic or fundamental data types sothat they can be used in different situations. Various type modifiers are-signed, unsigned,short and long.C language offers 3 different ‘int’ type modifiers-short, int, and long that representsthree different integer sizes. C language also offers 3 different floating point typemodifiers-float, double, and long double. Two special ‘char’type modifiers are-signed,unsigned.Type Size (in bytes) RangeInt (signed/short) 2-32768 to 32767In (unsigned) 2 0 to 65535Long (signed) 4-2, 147,483,648 to 2, 147, 483, 648Long (unsigned) 4 0 to 4,294,467,29510 Second Semester Introduction to Programming, May-June 2013Float 4 3.4*10-34 to 3.4*10-34Double (long float) 8 1.7*10-308 to 1.7*10-308-1Long double 10 3.4*10-4932 to 3.4*10-4932-1Char 1 –128 to 127Signed char 1 0 to 255Unsigned char 1 –128 to 1272. Derived Data TypesData types that are derived from fundamental data types are called derived datatypes. Derived data types don’t create a new data type; instead, they add somefunctionality to the basic data types. Two derived data type are - Array & Pointer.ArrayAn array is a collection of variables of same type i.e., collection of homogeneousdata referred by a common name. In memory, array elements are stored in a continuouslocation.Syntax:[ ];E.g. int a[10]; char ch [20];According to the rules of C language, 1st element of array is stored at location a[0],2nd at a[1] & so on.Pointer

Page 11: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

A pointer is a special variable that holds a memory address (location in memory) ofanother variable.E.g. if we want to store the address of an ‘int’ data type variable into a pointervariable, it is done in the following manner:

int a, *b;b=&a;In the above case, variable ‘b’ stores the address of variable ‘a’.

3. User Defined Data TypeUser defined data type is used to create new data types. The new data types formedare fundamental data types. Different user defined data types are; struct, union, enum,typedef.StructA struct is a user defined data type that stores multiple values of same or differentdata types under a single name. In memory, the entire structure variable is stored insequence.Syntax:Struct< structure name>{var1;Var2;..........};UnionA union is a user defined data type that stores multiple values of same or differentdata types under a single name. In memory, union variables are stored in a commonmemory location.Syntax:Union < tag name>{Var1;Var2;..........};Enum:An enumeration is a data type similar to a struct or a union. Its members are

Page 12: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

constants that are written as variables, though they have signed integer values. Theseconstant represent values that can be assigned to corresponding enumeration variables.Syntax:Enum {value1, value2, ......, value n};E.g. :Enum colors {black, blue, cyan, green, red, yellow};Color foreground, background;

Typedef:The ‘typedef’ allows the user to define new data-types that are equivalent to existingdata types. Once a user defined data type has been established, then new variables,array, structures, etc. can be declared in terms of this new data type.A new data type is defined as:Typedef type new-type;Type refers to an existing data type,

Q.4. (b) Why we use switch case statement in C? Write a program for selectionof Grading point for given students using switch case statement and break.

Ans. A switch case statement is a multi-way decision statement that is a simplifiedversion of an if-else block that evaluates only one variable. Switch-case statement in Cis used for :(a) When there is only one variable to evaluate in the expression.(b) When many conditions are being tested for.(c) Executes faster than its equivalent if-else construct.(d) Easy to read and understand.(e) Easy to debug.Syntax of Switch Statement :Switch (variable){case value 1;statement block 1;break;Case value 2 :Statement block 2;break;...

Page 13: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

.

.case value N:statement block N;break;default:statement block;break;}The switch case statement compares the value of the variable given in the switchstatement with the value of each case statement that follows. When the value of theswitch & the case statement matches, the statement block of that particular case isexecuted.Program for Selection of Grading Point# include <stdio.h>#include <conio.h>void main (){clrscr();int n;Printf (‘‘Enter the grade of the student’’);Scanf (‘‘%d’’, &n);Switch (n){Case 0 :If (n >=60)Printf (‘‘The student has got the first division’’);break;Case 1 :if ((n > = 45) && (n < 60))Printf (‘‘The student has got the second vision’’);break;Case 2 :if (n > 33) && (n < 45))printf (“The student has got the third division”);break;Default:Printf (‘‘The student got failed’’);getch ();}

Q. 5. (a) Write various characteristics of C programming

Page 14: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Ans. C is a robust language which combines the features of assembly language andhigh level language which makes it best suited for writing system software as well asbusiness package.Characteristics :1. C has only 32 keywords. This makes it relatively easy to learn as compared toother languages.2. It makes extensive use of function calls.3. C enables the programmer to concentrate on the problem at hand & not worryabout the machine code on which the program would be run.4. C supports pointer to refer computer memory, array, structures & functions.5. C is an extensible language as it enable the user to add his own function to the Clibrary.6. C is a portable language i.e., a C program written for one computer can be run onanother computer with little or no modification.7. C facilitates low level (bitwise) programming.8. C is the core language & many other programming languages (like C++, perl etc.)are based on C.Thus, learning other languages becomes easy if you have the knowledge of C.

Q.5. (b) Explain the Logical operators with the help of suitable example?Ans. Logical operators are used in C programming. The available logical operatorsare: (1) Logical AND (&&) (2) Logical OR (¦¦) (3) Logical NOT (!)The logical expressions are evaluated from left to right.Logical AND: It is used to simultaneously evaluate two conditions or expressionswith relational operators. If expressions on both the sides (left and right side) of thelogical operator is true then the whole expression is true.Truth Table of Logical ANDA B A & &B0 0 00 1 01 0 01 1 1

Page 15: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Logical OR: Logical OR operator is used to simultaneously evaluate two conditionsor expressions with relational operators. If one or both the expression on the left side &right side of the logical operator is true then the whole expression is true. The truthtable is as follows:A B A B0 0 00 1 11 0 11 1 1Logical NOT: The logical NOT operator takes a single expression and negates thevalue of the expression. That is, logical NOT produces a zero if the expression evaluatesto a non-zero value & produces a 1 if the expression produces a zero.The Truth Table is as follows :A !A0 11 0Example:#include<stdio.h>main(){int A=1, B=0, C=1, S, X, Y, Z, F=1;S=AB;H=!B;X=A && !B;Y = !(A! B && !C F);I.P. University–B.Tech–Akash Books 15Printf (‘‘S = %d \t H = %d/n’’, S, H);Printf (‘‘X = %d\ty=%d/n’’, X, Y);}OutputS = 1H = 1X = 1Y = 0

Q. 6. What is Array in C? Write a program to find the subtractions of any twomatrices. (12.5)Ans. An array is a collection of similar data items or elements. These data elementshave the same data tape.

Page 16: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Declaration of an array: An array can be declared using :Data type, Name & SizeSyntax is :data type name [size];e.g., int arr 1 [30]; / / This declare an array of 30 items whose data type is integer.float arr 1 [30]; / / This defines an array of 30 items whose data type is float.Char arr 1 [20] / / character array is equivalent to string

Accessing Element of the Array : To access the elements of the array, FOR loopis used. Once an array is declared, then it can be referred with subscript. The number inthe bracket following the array name is the subscript.Syntax for entering data into an arrayfor (i=0, i<29; i++){Printf (‘‘Enter marks’’);Scanf (‘‘%d’’, & marks [i]);}Program to find subtraction of any two matrices :#include <stdio.h>#include <conio.h>void main (){int i, j;int m1 [2] [2], m2[2] [2], sub ([2] [2];clescr ();Printf (‘‘Enter the elements of 1st Matrix’’);Printf (‘‘\n........’’);for (i=0; i<2; i++){for (j=0; j<2; j+=){Scanf (‘‘%d’’, & m1 [i] [j];Printf (‘‘Enter the elements of IInd Matrix’’);Printf (‘‘\n.........’’);for (i=0; i<2; i++){for (j=0, j<2; j++){Scanf (‘‘%d’’, &m2 [i] [j]);}}for (i = 0; i < 2; i++){

Page 17: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

for (j=0; j<2; J++){sub [i] [j] = m1 [i] [j] – m2 [i] [j]}}Printf (‘‘\n the elements of the Resultant Matrix are’’);Printf (‘‘\n........’’);for (i = 0; i < 2; i++){for (j = 0; j < 2; j++){Printf (‘‘\n’’);Printf (‘‘\t%d’’, sub [i] [j]);}}return 0;}OutputEnter elements of Ist Matrix5 67 8Enter elements of IInd Matrix1 23 4The elements of Resultant Matrix are4 44 4

Q. 7. (a) What is Function in C? Explain the ‘‘Call by value’’ and ‘‘Call byreference’’ in detail.Ans. A function is a group of statements that together perform a task. Every Cprogram has at least one function, which is main(), and all the most trivial programscan define additional functions.You can divide up your code into separate functions. How you divide up your codeamong different functions is up to you, but logically the division usually is so eachfunction performs a specific task.A function declaration tells the compiler about a function’s name, return type, andparameters. A function definition provides the actual body of the function.

Page 18: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

The call by value method of passing arguments to a function copies the actual valueof an argument into the formal parameter of the function. In this case, changes made tothe parameter inside the function have no effect on the argument.By default, C programming language uses call by value method to pass arguments.In general, this means that code within a function cannot alter the arguments used tocall the function. Consider the function swap() definition as follows.

/* function definition to swap the values */void swap(int x, int y){int temp;Temp = x; /* save the value of x */x = y; /* put y into x */y = temp; /* put temp into y */return;}The call by reference method of passing arguments to a function copies the addressof an argument into the formal parameter. Inside the function, the address is used toaccess the actual argument used in the call. This means that changes made to theparameter affect the passed argument.To pass the value by reference, argument pointers are passed to the functions justlike any other value. So accordingly you need to declare the function parameters aspointer types as in the following function swap( ), which exchanges the values of the twointeger variables pointed to by its arguments.

/* function definition to swap the values */void swap(int *x, int *y){int temp;temp = *x; /* save the value at address x */*x = *y; /* put y into x */*y = temp; /* put temp into y */return;}

Q. 8. (a) Explain the Pointer and Write a program for ‘‘Call by Reference’’.

Page 19: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Ans. A pointer is a variable which can point to something or in a certain direction.Consider the declaration :

int i = 3;This declaration tells the C compiler to :(a) Reserve space in memory to hold the integer value.(b) Associate the name i with this memory location.(c) Store the value 3 at this location. Where ,i refers to location name3 is the value at location6281 location number6281 is the memory location in computer where value of i = 3 has been stored.To print the address of imain (){int i = 3;Printf (‘‘\n Address of i = % u’’, & i);Printf (‘‘\n value of i = %d’’, i);3O/P :Address of i = 6281Value of i = 3& ‘Address of ’ operator.The expression &i would return the address of the variable i.The expression ‘*’ is called ‘value at Address’ operator & it gives the value stored ata particular address. It is also called ‘Indirection operator’.

Program for Call by Reference: In call by Reference the address of actualarguments in the calling function are copied into formal arguments of the called function.Program# include <stdio.h># include <conio.h>main (){int a = 10, b = 20;Swapr (&a, &b);Printf (‘‘\n a=%d b=%d’’, a, b);}Swapr (int *x, int *y){int t;t = *x;

Page 20: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

*x = *y;*y = t;}O/P :a=20 b = 10

Q. 8. (b) Explain passing pointers as function Arguments with example?Ans. While using pointers to pass arguments to a function, the calling functionmust pass addressess of the variable as arguments & the called function mustdereference the arguments to use them in the function body for processing.To use pointers for passing arguments to a function the programmer mustdo the following :1. Declare the function parameter as pointers.2. Using the Dereferenced pointers in the function body.3. Pass the addresses as the actual argument when the function is called.

Program: WAP to add two integers using functions.#include <stdio.h>void sum (int *a, int *b, int *t);int main (){int num1, num2, total;Printf (‘‘\n Enter the first number?’’);Scanf (‘‘%d’’, & num1);Printf (‘‘\n Enter the second number’’);Scanf (‘‘%d’’, & num 2);sum (&num1, &num2, & total);return 0;}void sum (int *a, int*b, int *t){*t = *a + *b;}OutputEnter the first number : 2Enter the second number : 3Total = 5.

Q. 9. Write a program for structure to store the 10 students’ information

Page 21: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

such as Name, Father’s, Name, Roll Number and Brach? Ans.#include <stdio.h>#include <conio.h>int main (){strut student{int roll-no;Char name [20];Char Father_Name [20];Char Branch [20];}Struct student stud [10];int i;Clrscr();for (i = 1; i ≤10; i++){Printf (‘‘\n Enter the Roll Number’’);Scanf (‘‘%d’’, & Stud [i].roll-no);fflush (stdin);Printf (‘‘/n Enter the Name:’’);gets (stud [i].name);fflush (stdin);20 Second Semester Introduction to Programming, May-June 2013Printf (‘‘\n Enter the Father’s Name:’’);gets (stud[i]. Father-Name);fflush (stdin);Printf (‘‘\n Enter Branch:’’);gets (stud[i]. Branch);}fflush (stdin);}for (i = 1; i < = 10; i++){Printf (‘‘\n *****store the student information %d’’, i + 1);Printf (‘‘\n Roll No = %d’’, stud [i].roll-no);Printf (‘‘\n NAME = %s’’ stud ([i].name);Printf (‘‘\n Father’s Name = %s’’, stud[i]. Father-name);Printf (‘‘\n Branch = %s’’, stud [i]. Branch);}getch ();return 0;}O/PEnter the roll no : 1Enter the name : Maya

Page 22: Introduction To Programming - ITP MODEL · Web viewINTRODUCTION TO PROGRAMMING(ETCS 108) Model Question Paper Q.1. Short answer type: (5×5 = 25) Q 1.(a) Define data flow diagrams.

Enter Father Name : SubhashEnter Branch : Mechanical...Enter the roll no : 10Enter the name : ManishEnter Father Name : SunilEnter Branch : CSEPrintf (‘‘|n Total = %d’’, total);getch ();}