<p> INTRODUCTION TO PROGRAMMING(ETCS 108)</p><p>Model Question Paper</p><p>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.</p><p>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 } Function () { Local Declarations Statements } Function N() { Local Declarations Statements }</p><p>Example To print ‘Hello’ in C #include<stdio.h> int main () { printf (‘‘Hello’’); return 0; } Output Hello</p><p>Classification of Programming Languages 1. Machine language 2. Assembly language 3. High level language (also known as Third Generation Language) 4. Very high level language</p><p>1. Machine Language: It is the only language that computer understands. All the commands & 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 directly executed by CPU.</p><p>2. Assembly Language: These are symbolic programming language that use symbolic notation to represent machine languae instruction. It uses symbolic codes also known as Mnemonic codes that are easy to remember abbreviations. An assembly language statement consists of a label, an operation code & one or more operands. Advantage: The code will again execute very fast. Disadvantage: 1. Less portable, 2. Code not machine independent.</p><p>3. High Level Language: Programs were written in English like statements, making them more convenient to use & giving the programmer more time to address a clients problem. Example: COBOL, FORTRAN, BASIC, C++, C. But C & FORTRAN combine some of the flexibility of assembly language with the power 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.</p><p>Q1. (c) Explains (i) break (ii) continue. Ans. (i) The break statement is used to terminate the execution of the nearest enclosing 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 the statement that follows the loop in which the break statement appears.</p><p>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; } O/P: The code is meant to print first 10 numbers using a while loop, but it will actually print only numbers from 0 to 4. As soon as ‘i’ becomes equal to 5, the break statement is executed & the control jumps to the statement following the while loop.</p><p>(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 unconditionally transferred 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 10 The code given here is meant to print nos from 0 to 10. But as soon as i becomes equal to 5, the continue strnt. is encountered, so rest of the statements in the for loop are skipped & control passes to the expression that increments the value of i.</p><p>Q1. (d) Explain the concept of Union in C langue? Ans. A union is a collection of variables of different data types. It is a chink of memory 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 new data. The unions are used to save memory. They are useful for applications that involve multiple members, where values need not be assigned to all members at any one time. Declaring a Union Syntax : 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 data replaces any existing dta. The size of union is the size of its largest field. The typedef keyword can be used to simplify the declaration of union variables. The difference between structure & union is that 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 union Pq P2; P2.x = 4 P2.y = 5; Printff (‘‘%d%d’’, P1.x, P1.y); Printff (‘‘%d%d’’, P2.x, P2.y); return 0; } O/P coordinates of P1 are 2 & 3 coordinates of P2 are 4 & 5</p><p>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 matrices or 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.</p><p>A 2-D array is stored sequentially in memory and not as a rectangular grid form.</p><p>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; } Output 12 34 56 32</p><p>Q. 2. (a) Define Flow Chart. What are various symbols used in making flow chart? Ans. A flowchart is a pictorial representation of an algorithm. It is a program planning 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.</p><p>The main advantage of using flowchart is that a programmer need not pay attention to the details of the elements of the programming language. The process of drawing a flowchart for an algorithm is flowcharting.</p><p>Flowchart Symbols: A flowchart uses boxes of different shapes to denote different types of instruction. The use of symbols makes it easier to communicate program logic through softwares. The ANSI (American National Standards Institute) has standardized these basic flowchart 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 Symbol It represents arithmetic & data movement instructions. Hence all arithmetic operations like addition, subtraction, multiplication, division are indicated by a processing symbol in flowchart. 4. Decision symbol The decision symbol indicates a decision point, i.e., a point at which a branch to one of two or more alternative points is possible. 5. Flow lines It indicates the exact sequence in which instructions are executed. 6. Connectors. Wherever a flowchart becomes so complex that the number & direction of flow lines.</p><p>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 to the logic of a program. It is a step by step description of how to arrive at a solution to a given problem. It is defined as a sequence of instructions that when executed in the specified sequence the desired results are obtained. The characteristics of on algorithm are : 1. Each instruction should be precise & unambiguous. 2. Each instruction should be executed in a finite time. 3. One or more instructions should not be repeated infinitely. This ensures that the 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 in First Division. Fifty students appeared for test in Final Exam.</p><p>Algo : Step 1 : Initialize Total_F_Div and Total_Mksheet to zero Step 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, Goto Step 5. Step 4 : Add 1 to Total_F_Div Step 5 : Add 1 to Total_Mksheet Step 6 : Is total_Mksheet = 50? If no, goto step 2 Step 7 : Print Total_F_Div Step 8 : Stop.</p><p>Q. 3. (a) What is the difference between algorithm and flow chart? Draw a flow chart to calculage the simple interest and also write the algorithm for this program Ans. 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.</p><p>Flowchart to calculate Simple Interest : Start Read P, R , T (P = Principal amount R = Rate of interest T = Time in yrs.) SI = P × R × T/ 100 Print SI Stop</p><p>Algorithm to calculate Simple Interest : 1. Initialize Simple-Interest to Zero. 2. Take are values of P, R, T from user. 3. Calculate Simple Interest 4. Simple-Interest = P * R * T/100. 5. Print Simple-Interest. 6. Stop.</p><p>Q. 4. (a) Explain the various Data types in C language. Ans. 1. Fundamental data Type Integer Integer 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; Float Float 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 as float must contain decimal values e.g. percentage, price, area etc. may contain real values. Syntax: Float variable name; E.g. float per, area; Character Char (Character) data type is used to store single character, within single quotes e.g. ‘a’, ‘z’, ‘e’ etc. A variable declared as ‘char’ can store only single character e.g. yes or No Choice requires only ‘y’ or ‘n’ as an answer. Syntax: Char variable name; E.g. char chi=‘a’, cha; Void Void data type is used to represent an empty value. It is used as a return type if a function does not return any value. Type modifier Type modifier is used to change the meaning of basic or fundamental data types so that 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 represents three different integer sizes. C language also offers 3 different floating point type modifiers-float, double, and long double. Two special ‘char’type modifiers are-signed, unsigned. Type Size (in bytes) Range Int (signed/short) 2-32768 to 32767 In (unsigned) 2 0 to 65535 Long (signed) 4-2, 147,483,648 to 2, 147, 483, 648 Long (unsigned) 4 0 to 4,294,467,295 10 Second Semester Introduction to Programming, May-June 2013 Float 4 3.4*10-34 to 3.4*10-34 Double (long float) 8 1.7*10-308 to 1.7*10-308-1 Long double 10 3.4*10-4932 to 3.4*10-4932-1 Char 1 –128 to 127 Signed char 1 0 to 255 Unsigned char 1 –128 to 127 2. Derived Data Types Data types that are derived from fundamental data types are called derived data types. Derived data types don’t create a new data type; instead, they add some functionality to the basic data types. Two derived data type are - Array & Pointer. Array An array is a collection of variables of same type i.e., collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location. 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 A pointer is a special variable that holds a memory address (location in memory) of another variable. E.g. if we want to store the address of an ‘int’ data type variable into a pointer variable, it is done in the following manner: int a, *b; b=&a; In the above case, variable ‘b’ stores the address of variable ‘a’.</p><p>3. User Defined Data Type User defined data type is used to create new data types. The new data types formed are fundamental data types. Different user defined data types are; struct, union, enum, typedef. Struct A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence. Syntax: Struct< structure name> { var1; Var2; ...... }; Union A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location. Syntax: Union < tag name> { Var1; Var2; ...... }; Enum: An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant 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;</p><p>Typedef: The ‘typedef’ allows the user to define new data-types that are equivalent to existing data 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,</p><p>Q.4. (b) Why we use switch case statement in C? Write a program for selection of Grading point for given students using switch case statement and break. </p><p>Ans. A switch case statement is a multi-way decision statement that is a simplified version of an if-else block that evaluates only one variable. Switch-case statement in C is 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; . . . . . case value N: statement block N; break; default: statement block; break; } The switch case statement compares the value of the variable given in the switch statement with the value of each case statement that follows. When the value of the switch & the case statement matches, the statement block of that particular case is executed. 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 (); }</p><p>Q. 5. (a) Write various characteristics of C programming Ans. C is a robust language which combines the features of assembly language and high level language which makes it best suited for writing system software as well as business package. Characteristics : 1. C has only 32 keywords. This makes it relatively easy to learn as compared to other languages. 2. It makes extensive use of function calls. 3. C enables the programmer to concentrate on the problem at hand & not worry about 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 C library. 6. C is a portable language i.e., a C program written for one computer can be run on another 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.</p><p>Q.5. (b) Explain the Logical operators with the help of suitable example? Ans. Logical operators are used in C programming. The available logical operators are: (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 expressions with relational operators. If expressions on both the sides (left and right side) of the logical operator is true then the whole expression is true. Truth Table of Logical AND A B A & &B 0 0 0 0 1 0 1 0 0 1 1 1 Logical OR: Logical OR operator is used to simultaneously evaluate two conditions or 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 truth table is as follows: A B A B 0 0 0 0 1 1 1 0 1 1 1 1 Logical NOT: The logical NOT operator takes a single expression and negates the value of the expression. That is, logical NOT produces a zero if the expression evaluates to a non-zero value & produces a 1 if the expression produces a zero. The Truth Table is as follows : A !A 0 1 1 0 Example: #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 15 Printf (‘‘S = %d \t H = %d/n’’, S, H); Printf (‘‘X = %d\ty=%d/n’’, X, Y); } Output S = 1 H = 1 X = 1 Y = 0</p><p>Q. 6. What is Array in C? Write a program to find the subtractions of any two matrices. (12.5) Ans. An array is a collection of similar data items or elements. These data elements have the same data tape. Declaration of an array: An array can be declared using : Data type, Name & Size Syntax 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</p><p>Accessing Element of the Array : To access the elements of the array, FOR loop is used. Once an array is declared, then it can be referred with subscript. The number in the bracket following the array name is the subscript. Syntax for entering data into an array for (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++) { 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; } Output Enter elements of Ist Matrix 5 6 7 8 Enter elements of IInd Matrix 1 2 3 4 The elements of Resultant Matrix are 4 4 4 4 Q. 7. (a) What is Function in C? Explain the ‘‘Call by value’’ and ‘‘Call by reference’’ in detail. Ans. A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task. A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function. The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the 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 to call the function. Consider the function swap() definition as follows.</p><p>/* 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 address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap( ), which exchanges the values of the two integer variables pointed to by its arguments.</p><p>/* 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’’. 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 name 3 is the value at location 6281 location number 6281 is the memory location in computer where value of i = 3 has been stored. To print the address of i main () { int i = 3; Printf (‘‘\n Address of i = % u’’, & i); Printf (‘‘\n value of i = %d’’, i); 3 O/P : Address of i = 6281 Value 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 at a particular address. It is also called ‘Indirection operator’.</p><p>Program for Call by Reference: In call by Reference the address of actual arguments 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; *x = *y; *y = t; } O/P : a=20 b = 10</p><p>Q. 8. (b) Explain passing pointers as function Arguments with example? Ans. While using pointers to pass arguments to a function, the calling function must pass addressess of the variable as arguments & the called function must dereference the arguments to use them in the function body for processing. To use pointers for passing arguments to a function the programmer must do 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.</p><p>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; } Output Enter the first number : 2 Enter the second number : 3 Total = 5.</p><p>Q. 9. Write a program for structure to store the 10 students’ information 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 2013 Printf (‘‘\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/P Enter the roll no : 1 Enter the name : Maya Enter Father Name : Subhash Enter Branch : Mechanical . . . Enter the roll no : 10 Enter the name : Manish Enter Father Name : Sunil Enter Branch : CSE Printf (‘‘|n Total = %d’’, total); getch (); } </p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages19 Page
-
File Size-