AIML CPDS Unit-1 - Unit-2
AIML CPDS Unit-1 - Unit-2
C Programming &
Data Structures
Unit-1 & Unit-2
Student Name:______________________________
Roll Number :______________________________
Branch :______________________________
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR
B.Tech -CSE – I Sem L T P C
3 0 0 3
(20A05201T) C-PROGRAMMING & DATA STRUCTURES
(Common to All Branches of Engineering)
Course Objectives:
UNIT-1
Introduction to C Language - C language elements, variable declarations and data types, operators
and expressions, decision statements - If and switch statements, loop control statements - while, for,
do-while statements, arrays.
Learning outcomes:
UNIT – 2
Functions, types of functions, Recursion and argument passing, pointers, storage allocation, pointers to
functions, expressions involving pointers, Storage classes – auto, register, static, extern, Structures,
Unions, Strings, string handling functions, and Command line arguments.
Learning outcomes:
Data Structures, Overview of data structures, stacks and queues, representation of a stack, stack
related terms, operations on a stack, implementation of a stack, evaluation of arithmetic expressions,
infix, prefix, and postfix notations, evaluation of postfix expression, conversion of expression from
infix to postfix, recursion, queues - various positions of queue, representation of queue, insertion,
deletion, searching operations.
Learning outcomes:
UNIT – 4
Linked Lists – Singly linked list, dynamically linked stacks and queues, polynomials using singly
linked lists, using circularly linked lists, insertion, deletion and searching operations, doubly linked
lists and its operations, circular linked lists and its operations.
Learning outcomes:
UNIT-5
Trees - Tree terminology, representation, Binary trees, representation, binary tree traversals. binary
tree operations, Graphs - graph terminology, graph representation, elementary graph operations,
Breadth First Search (BFS) and Depth First Search (DFS), connected components, spanning trees.
Searching and Sorting – sequential search, binary search, exchange (bubble) sort, selection sort,
insertion sort.
Learning outcomes:
Reference Books:
1. Pradip Dey and Manas Ghosh, Programming in C, Oxford University Press, 2nd Edition
2011.
2. E. Balaguruswamy, “C and Data Structures”, 4th Edition, Tata Mc Graw Hill.
3. A.K. Sharma, Computer Fundamentals and Programming in C, 2nd Edition, University
Press.
4. M.T. Somashekara, “Problem Solving Using C”, PHI, 2nd Edition 2009.
Course Outcomes:
Week l
Week 2
a) Write a C program to find both the largest and smallest number in a list of integers.
b) Write a C program that uses functions to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrices
Week 3
a) Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) To delete n characters from a given position in a given string.
Week 4
a) Write a C program that displays the position or index in the string S where the string T
begins, or – 1 if S doesn‘t contain T.
b) Write a C program to count the lines, words and characters in a given text.
Week 5
Week 7
i) Arrays
ii) Pointers
Week 8
Write C programs that implement Queue (its operations) using
i) Arrays
ii) Pointers
Week 9
Week 10
Write a C program that uses functions to perform the following operations on singly linked list.
Week 11
Write a C program that uses functions to perform the following operations on Doubly linkedlist.
Week 12
Write a C program that uses functions to perform the following operations on circular linkedlist.
ii) Traversing the above binary tree in preorder, inorder and postorder.
Week 14
Write C programs that use both recursive and non-recursive functions to perform the following
searching operations for a key value in a given list of integers:
i) Linear search
ii) Binary search
Week 15
Write a C program that implements the following sorting methods to sort a given list of integers in
ascending order
i) Bubble sort
ii) Selection sort
iii) Insertion sort
Text Books:
1. Programming in C and Data Structures, J.R.Hanly, Ashok N. Kamthane and A. Ananda Rao,
Pearson Education.
2. B.A. Forouzon and R.F. Gilberg, “COMPUTER SCIENCE: A Structured Programming
Approach Using C”, Third edition, CENGAGE Learning, 2016.
3. Richard F. Gilberg & Behrouz A. Forouzan, “Data Structures: A Pseudocode Approach with
C”, Second Edition, CENGAGE Learning, 2011.
Reference Books:
1. PradipDey and ManasGhosh, Programming in C, Oxford University Press, 2nd Edition 2011.
2. E.Balaguruswamy, “C and Data Structures”, 4 th Edition, Tata Mc Graw Hill.
3. A.K.Sharma, Computer Fundamentals and Programming in C, 2nd Edition, University Press.
4. M.T.Somashekara, “Problem Solving Using C”, PHI, 2nd Edition 2009.
Course Outcomes
Unit-2
1 Functions 2.1
2 Types of functions 2.5
3 Argument passing 2.8
4 Recursion 2.11
5 Pointers & storage allocation 2.12
6 Expressions involving pointers 2.15
7 Pointers to functions 2.17
8 Storage classes – auto, register, static, extern 2.21
9 Structures 2.25
10 Unions 2.32
11 Strings 2.34
12 String handling functions 2.35
13 Command line arguments 2.37
14 2 Marks Questions 2.40
Index
Lab Programs in C Programming
Page No.
1 Week-1 1– 6
2 Week-2 7 – 14
3 Week-3 15 – 17
4 Week-4 18 – 19
5 Week-5 20 – 22
6 Week-6 23 – 24
Unit 1
Introduction to C Language
1. Introduction to C Language
2. C language elements
3. Variable declarations and data types
4. Operators and expressions
5. Decision statements - If and switch statements
6. Loop control statements - while, for, do-while statements
7. Arrays
1. Introduction to C Language
1.2
Q1.12. What is C?
Answer:
C is a computer programming language used to design computer software and applications.
1.3
1.4
Documentation Section
1 (Used for Comments)
2 Pre-processing Commends
3 Global Declarations
int main( )
{
Local Declarations
Executable Statements
.
.
4 .
return 0;
}
5 User defined functions
1. Documentation Section:
This section is used to provide a small description of the program.
The comment lines are simply ignored by the compiler that means they are not executed.
In C, there are two types of comments.
1. Single Line Comments: Single line comment begins with // symbol. We can write any
number of single line comments.
2. Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends with */.
3. Global Declarations:
The global declaration is used to define the global variables, which are common for all the
functions after its declaration.
We also use the global declaration to declare functions.
4. Main Function:
Every C program must contain this main function.
Here, main ( ) is a user-defined function which tells the compiler that this is the starting point of the
program execution.
The open brace ({ ) indicates the beginning of the main function.
Local declaration contains local variables which are used within main ( ) function.
Executable statements perform tasks like reading data, displaying the result, calculations, etc.,
Closing Brace ( }) indicates the end of the main ( ) function.
The statement return 0 returns a value zero to the Operating System after completing the main ( )
execution.
If we don't want to return any value, we can use it as void.
5. User Defined Functions:
This is the place where we implement the user-defined functions.
1.6
2. Digits:
o Clanguagesupports10digitswhichareusedtoconstructnumericalvalues.
o Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3. Special Symbols:
o C language supports a rich set of special symbols that include symbols to perform mathematical
operations, to check conditions, white spaces, backspaces, and other special symbols.
Example: +, -, *, /, @, #, $, %, etc…,
2. C language elements
2. Identifiers
3. Special Symbols
4. Constants
5. Strings
6. Operators
1.7
C- Keywords
User
Condition Looping Storage
Data Types Defined Others
Statements Statements Classes
Data Types
1 int 10 struct 14 if 18 do 23 auto 27 const
2 char 11 union 15 else 19 while 24 static 28 default
3 double 12 enum 16 switch 20 for 25 register 29 goto
4 float 13 typedef 17 case 21 break 26 extern 30 return
5 long 22 continue 31 sizeof
6 short 32 volatile
7 signed
8 unsigned
9 void
2. Identifiers:
An identifier is a name used to identify a variable, function, structure, etc.
In C length of identifier is 31characters.
1.8
3. Special Symbols:
Some special characters are used in C, and they have a special meaning which cannot be used
for another purpose.
1. Square brackets [ ]: Used in Arrays
4. Constants:
A constant is a value assigned to the variable which will cannot be changed.
There are two ways of declaring constant:
1. Using const keyword
Types of constants in C:
Constant Example
Integer constant 10, 11, 34, etc.
Floating-point constant 45.6, 67.8, 11.2, etc.
Octal constant 011, 088, 022, etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
6. Operators:
Operator is a special symbol used to perform the operation.
The data items on which the operators are applied are known as operands.
Operators are applied between the operands.
Depending on the number of operands, operators are classified as follows:
1. Unary Operator:
o A unary operator is an operator applied to the single operand.
o Example: Unary Minus (-), not (!), increment operator (++), decrement operator (--),
sizeof etc…,
2. Binary Operator:
o The binary operator is an operator applied between two operands.
o Example: Addition (+), Subtraction (-), Multiplication (*), etc...,
3. Ternary Operator:
o The ternary operator is an operator applied between three operands.
o Ternary operator in c is conditional operator ( ?:)
1.10
1. Integer:
The integer data type is a set of whole numbers.
Every integer value does not have the decimal value.
We use
se the keyword "int" to represent integer data type in C.
The following table provides complete details about the integer data type according to the 32-
32
bit architecture.
Range
1.11
2. Floating Point:
Floating-point data types are a set of numbers with the decimal value.
We use the keyword "float" to represent floating-point data type and "double" to represent
double data type in C.
The following table provides complete details about floating-point data types.
Range
Type Size Precision
(-2N-1 to 2N-1 – 1,
Here N is number of
bits)
float 4 byte -232-1 to 232-1 -1 6 decimal places
3. Character:
The following table provides complete details about the character data type.
Range
(signed char)
unsigned char 1 byte 0 to 255
Declaration:
Declaration of a variable tells the compiler to allocate the required amount of memory with the
specified variable name and allows only specified data type values into that memory location.
We can declare a variable using below syntax.
1.13
Syntax:
Datatype Variable_name;
Example:
int number;
The above declaration tells to the compiler that allocates 2 bytes of memory with the name number and
allows only integer values into that memory location.
Initialization:
Initialization of a variable means assigning a value to the variable. We can initialize a variable with a
value using below syntax.
Syntax:
Variable_name = Value
Example:
int number;
number = 10;
In the above example, value 10 is assigned to the variable number.
2. A variable can start with a letter or an underscore ( _ ), but not with a digit.
Variable_1 is Valid
4. Special symbols like !, @, #, $, % etc. are not allowed in variables except one special symbol
underscore ( _ ).
Example: company#name, $name, email@id are invalid variables
1.14
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operator
6. Conditional Operator
8. Special Operators
1.15
1. Arithmetic Operators:
The arithmetic operators are used to perform basic mathematical operations like addition,
subtraction, multiplication, division and percentage modulo.
The following table provides information about arithmetic operators:
Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulo Division 5%2 1
(Gives the Remainder for
integer division)
2. Relational Operators:
The relational operators are used to check the relationship between two values.
Every relational operator has two results TRUE or FALSE.
In simple words, the relational operators are used to define conditions in a program.
The following table provides information about relational operators.
Operator Name Example Result
== Equal to 10 == 5 False
1.16
3. Logical Operators:
The logical operators are used to check logical relationship between two conditions.
The following table provides information about logical operators.
Operator Name Meaning Example Result
4. Assignment Operator:
+= A += 5 A=A+5 15
*= A *= 5 A=A*5 50
/= A /= 5 A=A/5 2
%= A %= 5 A=A%5 0
1.17
6. Conditional Operator:
The conditional operator is also called a ternary operator because it requires three operands.
This operator is used for decision making.
Syntax:
If the condition is TRUE the TRUE Part is performed, if the condition is FALSE the FALSE Part
is performed.
⇒ A value is 100
7. Bitwise Operators
The bitwise operators are used to perform bit-level operations in the C.
When we use the bitwise operators, the operations are performed based on the binary values.
The following table describes all the bitwise operators in the C.
1.18
Example (A = 25,
Operator Name Meaning B = 20 ) Result
1. sizeof operator:
This operator is used to find the size of the memory (in bytes) allocated for a variable.
Syntax:
sizeof(variableName)
1.19
Example:
int a;
2. Pointer operator(*):
This operator is used to define pointer variables.
3. Comma operator(,):
This operator is used to separate variables while they are declaring, separate the
expressions in function calls, etc.
C=A+B
Expression Types:
1. Infix Expression
2. Postfix Expression
1. Infix Expression:
When there is more than one operator in an expression the operator with higher precedence is evaluated
first and the operator with the least precedence is evaluated last.
Operator associativity is used to determine the order of operators with equal precedence evaluated in an
expression.
When an expression contains multiple operators with equal precedence, we use associativity to determine
the order of evaluation of those operators.
1.21
()
function call
[] array reference
-> structure member access
1 Left to Right
structure member access
.
!
~
negation
+ Bitwise Not
- Unary plus
Unary minus
++ increment operator
-- decrement operator
address of operator
& pointer
2 * returns size of a variable Right to Left
type conversion
sizeof
(type)
*
multiplication
/ division
3 remainder Left to Right
%
1.22
==
equal to not equalto
7 != Left to Right
An expression is evaluated based on the precedence and associativity of the operators in that expression.
Example:
10 + 4 * 3 / 2
In the above expression, there are three operators +, * and /. Among these three operators, both
multiplication and division have the same higher precedence and addition has lower precedence. So,
according to the operator precedence both multiplication and division are evaluated first and then the addition
is evaluated.
1.23
As multiplication and division have the same precedence they are evaluated based on the associativity.
Here, the associativity of multiplication and division is left to right. So, multiplication is performed first, then
division and finally addition. So, the above expression is evaluated in the order of * / and +. It is evaluated as
follows...
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
In a c programming language, the data conversion is performed in two different methods as follows...
1. Type Conversion
2. Type Casting
1. Type Conversion:
The type conversion is the process of converting a data value from one data type to another
data type automatically by the compiler.
Sometimes type conversion is also called implicit type conversion. The implicit type
conversion is automatically performed by the compiler.
Example:
int i = 10 ;
float x = 15.5 ;
2. Type Casting:
Typecasting is also called an explicit type conversion. Compiler converts data from one data
type to another data type implicitly. When compiler converts implicitly, there may be a data loss.
1.24
In such a case, we convert the data from one data type to another data type using explicit type
conversion. To perform this, we use the unary cast operator.
To convert data from one type to another type we specify the target data type in parenthesis as a
prefix to the data value that has to be converted.
Example:
int totalMarks = 450, maxMarks = 600 ;
float average ;
average = (float) totalMarks / maxMarks * 100 ;
In the above example code, both totalMarks and maxMarks are integer data values. When we
perform totalMarks / maxMarks the result is a float value, but the destination (average) datatype is a
float. So we use type casting to convert totalMarks and maxMarks into float data type.
A Statement is an instruction in a program that can form using C-Tokens. There are 3 types of statements.
1. Sequential Statements
3. Looping Statements.
1.25
Q5.2.
.2. Define null statement. What is the importance of it?
Answer:
Null statement means it executes nothing in the program that means it performs no operation.
Example:
; it is a null statement
Importance:
It is useful when the syntax of the language calls for a statement but no expression evaluation.
1. Simple if
2. if-else statement
3. if-else-if
if statement (else
(else-if ladder)
1. Simple if:
1.26
2. if-else statement:
In if-else-if
if statement, if the condition is true then True block statements are executed otherwise False
block statements are executed.
1.27
It is used in the scenario where there are multiple cases to be performed for different conditions.
In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be
executed, otherwise if some other condition is true then the statements defined in the else-if block will be
executed, at the last if none of the condition is true then the statements defined in the else block will be
executed.
Syntax:
1.28
Flowchart:
Example Program | Find the largest of three numbers using else-if ladder.
#include<stdio.h>
int main()
{
int a, b, c ;
Output:
Enter any three integer numbers: 35
65
41
Example Program | Write a program to find a largest number among 3 numbers using
nested if.
#include<stdio.h>
int main()
if (a > b)
if(a > c)
printf("%d is big", a) ;
1.30
else
printf("%d is big", c) ;
else
if(b > c)
printf("%d is big", b) ;
else
printf("%d is big", c) ;
return 0;
}
Output:
Enter 3 numbers:
25 20 14
25 is big
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple
Here, we can define various statements in the multiple cases for the different values of a single variable.
Syntax:
1.31
Flowchart:
1.32
scanf("%d", &n) ;
switch( n )
case 0:
printf("ZERO") ;
break;
case1:
printf("ONE") ;
break;
case 2:
printf("TWO") ;
break;
case 3:
printf("THREE") ;
case 4:
printf("FOUR") ;
break;
case 5:
printf("FIVE") ;
break;
1.33
case 6:
printf("SIX") ;
break;
case 7:
printf("SEVEN") ;
break ;
case 8:
printf("EIGHT") ;
break ;
case 9:
printf("NINE") ;
break ;
default:
printf("Not a Digit") ;
return 0;
Output:
1.34
Answer:
if-else switch
Definition Depending on the condition in the 'if' The user will decide which statement is to
statement, 'if' and 'else' blocks are be executed.
executed.
Expression It contains either logical or equality It contains a single expression which can be
expression. either a character or integer variable.
Evaluation It evaluates all types of data, such as It evaluates either an integer, or character.
integer, floating-point, character or
Boolean.
Sequence of First, the condition is checked. If the It executes one case after another till the
execution condition is true then 'if' block is executed break keyword is not found, or the default
otherwise 'else' block statement is executed.
Default If the condition is not true, then by default, If the value does not match with any case,
execution else block will be executed. then by default, default statement is
executed.
Editing Editing is not easy in the 'if-else' statement. Cases in a switch statement are easy to
maintain and modify. Therefore, we can say
that the removal or editing of any case will
not interrupt the execution of other cases.
Speed If there are multiple choices implemented If we have multiple choices then the switch
through 'if-else', then the speed of the statement is the best option as the speed of
execution will be slow. the execution will be much higher than 'if-
else'.
1.35
The looping statements are used to execute a single statement or block of statements repeatedly until the
given condition is FALSE.
1. do-while
2. while
3. for
1. do-while statement:
1.36
#include<stdio.h>
int main()
int n = 0;
do
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;
Output:
0 2 4 6 8 10
2. while statement:
1.37
#include<stdio.h>
int main()
int n = 0;
while( n <= 10 )
if( n%2 == 0)
printf("%d\t", n) ;
Output:
0 2 4 6 8 10
1.38
3. for statement:
The for statement is used to execute a single statement or block of statements repeatedly
until given condition is False.
1.39
#include<stdio.h>
int main()
int n ;
if( n%2 == 0)
printf("%d\t", n) ;
return 0;
Output:
0 2 4 6 8 10
1.40
6 while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) { do {
7 statement(s);
statement(s);
}while(condition);
}
2. break statement is also used to terminate looping statements like while, do- while and for.
1.41
#include<stdio.h>
int main()
int i;
if( i == 5)
break;
printf("%d\t",i);
return 0;
Output:
0 1 2 3 4
The continue statement is used to move the program execution control to the beginning of the looping
statement.
When we use continue statement with while and do-while statements the execution control directly
jumps to the condition.
When we use continue statement with for statement the execution control directly jumps to the
modification portion (increment/decrement/any modification) of the for loop.
1.42
1.43
7. Arrays
An array is a variable which can store more than one value of same datatype.
When we want to create an array we must know the data type of values to be stored in that array and
also the number of values to be stored in that array.
In the above syntax, the datatype specifies the type of values we store in that array and size specifies
the maximum number of values that can be stored in that array.
Example
int a [3] ;
Here, the compiler allocates 6 bytes of memory locations with a single name 'a' and tells the compiler
to store three different integer values (each in 2 bytes of memory).
1.44
Initialization of Array:
Example
Syntax for creating an array without size and with initial values:
Example
An element is accessed by indexing the array name. This is done by placing the indexof the element
Syntax:
arrayName [ indexValue ] ;
Example:
For the above example the individual elements can be denoted as follows:
1.45
printf(“%d”, a[2]);
a[1] = 100;
o Multi-Dimensional Array
Single dimensional arrays are used to store list of values of same data type.
Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply
1-DArrays.
1.46
o We use the following general syntax for declaring a single dimensional array.
o Example
Case-1:
o We use the following general syntax for declaring and initializing a single dimensional
array with size and initial values.
o Example
Case-2:
o We can also use the following general syntax to initialize a single dimensional array
without specifying size and with initial values.
o The array must be initialized if it is created without specifying any size. In this case, the
size of the array is decided based on the number of values initialized.
1.47
o Example:
o To access the elements of single dimensional array we use array name followed by index
value of the element that to be accessed.
o The index value of single dimensional array starts with zero (0) for first element and
incremented by one for each element.
arrayName [ indexValue ]
o Example
marks [2] = 99 ;
In the above statement, the third element of 'marks' array is assinged with
value '99'.
2. Multi-Dimensional Array:
Multi-dimensional array can be of two dimensional array or three dimensional array or four
dimensional array or more.
The 2-D arrays are used to store data in the form of table.
o We use the following general syntax for declaring a two dimensional array:
1.48
o Example
o We use the following general syntax for declaring and initializing a two dimensional
array with specific number of rows and columns with initial values.
o Example
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
{1, 2, 3},
{4, 5, 6}
};
o Here the row and column index values must be enclosed in separate square braces.
o We use the following general syntax to access the individual elements of a two-
dimensional array...
1.49
o Example
matrix_A [0][1] = 10 ;
In the above statement, the element with row index 0 and column index 1 of
matrix_A array is assigned with value 10.
Q7.4. Explain how memory is allocated for 1-D arrays, and how can we address of ith element.
Answer:
Whenever an array is declared in the program, contiguous memory to it elements are allocated.
Initial address or base address of the array – address of the first element of the array is called base
address of the array.
Each element will occupy the memory space required to accommodate the values for its type, i.e.;
depending on elements data type, 1, 2 or 4 bytes of memory is allocated for each elements.
Below diagram shows how memory is allocated to an integer array of N elements. Its base address –
address of its first element is10000.
Since it is an integer array, each of its element will occupy 2 bytes of space. Hence first element
occupies memory from 10000 to 10001. Second element of the array occupies immediate next
memory address in the memory, i.e.; 10002 to10003.
Example:
o If an array ‘P’ is created with 50 elements of integer type. The base address is 5050, then
calculate 28th element address.
1.50
Whenever an array is declared in the program, contiguous memory to it elements are allocated, but the
elements assigned to the memory location depend on the two different methods
o When we store the array elements in row major order, first we will store the elements of first
row followed by second row and so on as shown in below figure.
o Hence in the memory we can find the elements of first row followed by second row and soon.
o Example:
10 20 30
40 50 60
70 80 90
Row major order allocation as follows:
1.51
o Example:
o In this method all the first column elements are stored first, followed by second column
1.52
o Example:
10 40 70
20 50 80
30 60 90
o Example:
1.53
1. Arrays represent multiple data items of the same type using a single name.
2. In arrays, the elements can be accessed randomly by using the index number.
3. Arrays allocate memory in contiguous memory locations for all its elements. Hence there is no chance
of extra memory being allocated in case of arrays. This avoids memory overflow or shortage of
memory in arrays.
Disadvantages of Arrays:
2. An array is a static structure (which means the array is of fixed size). Once declared the size of the
array cannot be modified. The memory which is allocated to it cannot be increased or decreased.
3. Insertion and deletion are quite difficult in an array as the elements are stored in consecutive memory
locations and the shifting operation is costly.
4. Allocating more memory than the requirement leads to wastage of memory space and less allocation
of memory also leads to a problem.
6. Arrays are also used to implement other data structures like Stacks, Queues, Heaps, Hash tables etc.
1.54
2 Marks Questions
1. Introduction to C Language
1. Define computer hardware.
2. Why is C called the mother of all languages?
3. What is the use of the pre-processor directives?
(OR)
Discuss pre-processor directives in C.
(OR)
4. What is a pre-processor?
5. Define header file.
Answer:
The header files contain declarations relating to standard library functions and macros that are
available with C.
2. C language elements
6. What is a token? List out the C- Tokens
7. What is a keyword?
8. What is an identifier? What are the rules to define an identifier?
9. What is a constant?
3. Variable declarations and data types
10. What will be the output of the following program?
(a) #include <stdio.h>
int main()
{
int a=010;
printf("\n a=%d",a);
return0;
Explanation:
In (a), the integer constant 010 is taken to be octal as it is preceded by a zero (0). Here the
variable 'a' is printed with %d specifier. The decimal equivalent of the octal value 10, which is
8, will be printed. Whereas in (b) the same variable is printed with %o format specifier, so 10 is
printed on the screen.
(c) #include <stdi.h>
int main()
{
int a=010;
printf("\n a=%x",a);
return 0;
}
Output: a = 8
Explanation: In (c), the octal value 10 is printed with %x format specifi er. That is
hexadecimal equivalent of 10 which is 8 will be printed. Basics of C.
(d) #include<stdio.h>
int main()
{
inta=53;
printf("\n a=%o",a);
return0;
}
Output: a = 65
Explanation: In (d), an integer constant 53 is stored in the variable 'a' but is printed with
%o. The octal equivalent of 53, which is 65, will be printed.
(e) #include<stio.h>
Explanation: In (e), an integer constant 53 is stored in the variable 'a' but is printed with
%X. The hexadecimal equivalent of 53, which is 35, will be printed.
1.56
11. What are the basic data types supported in C programming language?
12. List the data types and their sizes of C language.
13. What is the size of integer data type?
14. Write a sample C program to demonstrate the control string of scanf() function.
15. What is the main difference between variable and constant? List any two constants used in C.
Answer:
Variables Constants
It is a variable that stores data type value in a It is similar to a variable and cannot be
program. changed during program execution.
It is a variable that can be changed after defining It is a fixed variable that cannot be
the variable in a program changed after defining the variable in a
program.
The value of a variable can change depending on In constants, the value cannot be
the conditions. changed.
Typically, it uses int, float, char, string, double, It can be express in two ways: #define
etc. data types in a program. pre-processor and the const keyword.
10+20+16-3*10/6-4
28. What is the output of the following expression?
8 >> 3
1.58
16<<3
30. Define null statement.
31. Define expression. List out its types.
1.60
Unit 2
1. Functions
2. Types of functions
3. Argument passing
4. Recursion
5. Pointers & storage allocation
6. Expressions involving pointers
7. Pointers to functions
8. Storage classes – auto, register, static, extern
9. Structures
10. Unions
11. Strings
12. String handling functions
13. Command line arguments
1. Functions
Q1.1. Define a function. How to define and call a function in C.
Answer:
A function is a block of statements which perform specific task.
Every function in C has the following:
1. Function Definition
2. Function Call
3. Function Declaration (Function Prototype)
1. Function Definition:
The function definition is also known as the body of the function.
Syntax Prepared By: M. Parthasaradhi, Asst. Prof.
Return_Type function_Name(parameters_List)
{
Actual code...
}
In the above syntax,
Return_Type specifies the value return by a function
function_Name is a user-defined name used to identify the function uniquely
parameters_List is the data values that are sent to the function definition
2.1
Example:
add(15, 20)
3. Function Declaration (Function Prototype):
The function declaration tells the compiler about function name, the data type of the return
value and parameters. The function declaration is also called a function prototype.
The function declaration is performed before the main function or inside the main function or
any other function.
Syntax:
Return_Type function_Name(parameters_List);
Example:
void add( int , int );
Prepared By: M. Parthasaradhi, Asst. Prof.
2.2
Q1.3. Write the differences between actual parameters and formal parameters.
Answer:
Actual Parameters Formal Parameters
These are specified in function calling. These are declared in function definition.
It may be variable or constant. It must be variable.
Example: Example:
void main( ) void sum(int x, int y)
{ {
int a=2;
sum(a, 40)
} }
Here a, 40 are actual parameters. Here x and y are formal parameters.
5. Increases readability.
6. Program development time, effort and cost can be reduced.
2.4
int main( )
{
addition( ) ; // function call
}
void addition( ) // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
printf("Sum = %d", num1+num2 ) ;
}
Output:
Enter any two integer numbers : 20 30
Sum = 50
This type of functions having the parameters and doesn’t have the return values.
Example Program
#include<stdio.h>
void addition(int, int) ; // function declaration
int main( )
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
2.6
2.7
3. Argument passing
Q3.1. Explain in detail about parameter passing techniques.
(Or)
Explain call by value and call by reference with example.
Answer:
There are two methods to pass parameters from calling function to called function and they are:
1. Call by Value
Prepared By: M. Parthasaradhi, Asst. Prof.
2. Call by Reference
1. Call by Value
In call by value parameter passing method, the copy of actual parameter values are copied
to formal parameters and these formal parameters are used in called function.
The changes made on the formal parameters do not affect the values of actual
parameters.
2.8
2. Call by reference:
In Call by Reference parameter passing method, the memory address of the actual
parameters is copied to formal parameters.
Prepared By: M. Parthasaradhi, Asst. Prof.
This address is used to access the memory locations of the actual parameters in called
function. In this method, the formal parameters must be pointer variables.
Any changes made on the formal parameters effects the values of actual parameters.
Example Program:
2.9
Output:
Enter two numbers: 10 20
Before swap: num1 = 10, num2 = 20
After swap: num1 = 20, num2 = 10
2.10
}
Output:
Enter any positive integer: 5
Factorial of 5 is 120
In the above example program, the factorial( ) function call is initiated from main( ) function with
the value 3. Inside the factorial( ) function, the function calls factorial(2), factorial(1) and factorial(0) are
called recursively.
The complete execution process of the above program is shown in the following figure: 2.11
Factorial (3)
6 3 * Factorial (2)
2 2 * Factorial (1)
Factorial (1) 1
1
Example:
int *ptr ;
In the above example declaration, the variable "ptr" is a pointer variable that can be used to store
any integer variable address.
2.12
a ptr
50 2020
2020 3510
Example Code
#include<stdio.h>
int main( )
{
int a = 10, *ptr ;
ptr = &a ;
printf("Address of variable a = %u\n", ptr) ;
printf("Value of variable a = %d\n", *ptr) ;
printf("Address of variable ptr = %u\n", &ptr) ;
return 0;
Prepared By: M. Parthasaradhi, Asst. Prof.
}
Output:
Address of variable a = 6487580
Value of variable a = 10
Address of variable ptr = 6487568
2.13
Answer:
A void pointer is a pointer variable used to store the address of a variable of any datatype.
That means single void pointer can be used to store the address of integer variable, float variable,
character variable, double variable or any structure variable. Prepared By: M. Parthasaradhi, Asst. Prof.
We use the keyword "void" to create void pointer.
Syntax:
void *pointerName ;
Example:
int a;
float b;
void *p1, *p2;
p1 = &a;
p2 = &b;
2.14
2. Subtraction
3. Increment
4. Decrement
5. Comparison
2.15
3. Increment:
The increment operation on pointer variable is calculated as follows:
Address_At_Pointer + Data_type_size
Example1:
int a, *iPtr ;
iPtr = &a ; // Asume address of a is 1000
Prepared By: M. Parthasaradhi, Asst. Prof.
2.16
5. Comparison:
The comparison operation is performing between the pointers of same data type only. In c
programming language, we can use all comparison operators (relational operators) with pointers.
Note:
We can't perform multiplication and division operations on pointers.
7. Pointers to functions
Q7.1. Explain about pointers to arrays?
Answer:
In c, when we declare an array the compiler allocates the required amount of memory and also
creates a constant pointer with array name and stores the base address of that pointer in it.
The address of the first element of an array is called as base address of that array.
The array name itself acts as a pointer to the first element of that array.
Consider the following example of array declaration...
int marks[6] ;
For the above declaration, the compiler allocates 12 bytes of memory and the address of first
memory location (i.e., marks[0]) is stored in a constant pointer called marks. That means in the
Prepared By: M. Parthasaradhi, Asst. Prof.
}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
2.18
o If malloc( ) function unable to allocate memory due to any reason it returns NULL pointer.
Syntax:
void* malloc(size_in_bytes)
Example:
char *title;
title = (char *) malloc(15);
2.19
3. realloc( ):
o realloc( ) is the pre-defined function used to modify the size of memory blocks that were
previously allocated using malloc( ) or calloc( ).
o realloc( ) function returns void pointer.
o If realloc( ) function unable to allocate memory due to any reason it returns NULL pointer.
Syntax
void* realloc(*pointer, new_size_of_each_block_in_bytes)
Example:
char *title;
title = (char *) malloc(15);
title = (char*) realloc(title, 30);
4. free( ):
o free( ) is the pre-defined function used to deallocate memory block that was previously
Prepared By: M. Parthasaradhi, Asst. Prof.
Example:
char *title;
title = (char *) malloc(15);
free(title);
2.20
Property Description
Keyword auto
Life time Till the control remains Within a block of function in which variable is
defined.
Example Code
#include<stdio.h>
void fun( )
{
auto int a = 1;
printf("%d", a);
Prepared By: M. Parthasaradhi, Asst. Prof.
}
int main( )
{
fun( );
fun( );
fun( );
}
Output:
1
1
1 2.21
Property Description
Keyword static
Life time The value persists between different function calls (i.e., Initialization
is done only once)
Example Code
#include<stdio.h>
void fun( )
{
static int a;
printf("%d\n",a);
a++;
}
int main( )
{
fun( );
fun( );
fun( ); Prepared By: M. Parthasaradhi, Asst. Prof.
}
Output:
0
1
2
2.22
Property Description
Keyword register
Life time Till the control remains within the block in which variable is defined
Example Code
#include<stdio.h>
void fun( )
{
register int a = 1;
printf("%d\n",a);
a++;
}
int main( )
{
fun( );
fun( );
fun( ); Prepared By: M. Parthasaradhi, Asst. Prof.
}
Output:
1
1
1
2.23
Property Description
Keyword extern
Life time As long as the program’s execution does not comes to end
Example:
Extern.c Main.c
#include<stdio.h> #include<stdio.h>
extern int a =10; #include "Extern.c"
int main( )
{
printf("%d", a);
return 0;
}
Output:
10
2.24
Example:
struct Student
{
// structure members
char stud_name[30];
int roll_number;
float percentage;
};
// structure variables
struct Student s1, s2;
Accessing Structure Members:
We use structure variables to access structure members with dot(.) operator.
Prepared By: M. Parthasaradhi, Asst. Prof.
Syntax:
Structure_varaible . structure_member
Example:
s1.stud_name
s1.roll_number
2.25
2.26
C-Programming & Data Structures (20A05201T):: Unit-2
Q9.3. Explain in detail about structure with arrays with example.
Answer:
An array of structures in C can be defined as the collection of multiple structures variables where
each variable contains information about different entities.
The arrays of structures in C are used to store information about multiple entities of different data
types.
The array of structures is also known as the collection of structures.
Example:
#include<stdio.h>
struct student
{
int rollno;
char name[10];
};
int main( )
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnterRollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
Prepared By: M. Parthasaradhi, Asst. Prof.
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
2.27
{
struct student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter age: ");
scanf("%d", &s1.age);
display(s1); // passing struct as an argument
return 0;
} 2.28
Output
Enter name: Bond
Enter age: 13
Displaying information
Name: Bond
Age: 13
Example:
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main( )
{
struct person *personPtr, person1;
2.29
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
2.30
return 0;
}
Output
struct node
int data1;
};
2.31
Example:
union Student
{
// union members
char stud_name[30];
int roll_number;
float percentage;
};
// union variables
union Student s1,s2;
Accessing Union Members:
We use union variables to access structure members with dot(.) operator.
Syntax: Prepared By: M. Parthasaradhi, Asst. Prof.
Union_varaible . Union_member
Example:
s1.stud_name
s1.roll_number
s1.percentage
2.32
Keyword The keyword struct is used to define a The keyword union is used to define a
structure. union.
The size of the structure is greater than
Size The size of the union is equal to the
or equal to the sum of size of its
size of largest member.
members.
Memory Each member in a structure is assigned Memory allocated is shared by
unique storage location. individual member of a union.
Value Altering value of a member will not Altering value of a member will alter
Altering affect other members of a structure. other members of a union.
Accessing Individual member can be accessed at a Only one member can be accessed at a
members time. time.
Initialization Several members of a structure can Only the first members of a union can
of Members initialize at once. be initialized.
Prepared By: M. Parthasaradhi, Asst. Prof.
2.33
char s[20];
printf("Enter a String: ");
gets(s);
printf("Your String is: ");
puts(s);
return 0;
}
Output:
Enter a String: hello
Your String is: hello 2.34
2.36
Output:
D:\C Programs>main.exe hello welcome to GATESIT
Total number of arguments are - 5 and they are
1 -- main.exe
2 -- hello
3 -- welcome
4 -- to
5 – GATESIT
2.37
In the above syntax, integral constant '0' is assigned to name1, integral constant '1' is assigned to
name2 and so on.
We can also assign our own integral constants as follows:
enumenum_name{name1 = 10, name2 = 30, name3 = 15, ... }
In the above syntax, integral constant '10' is assigned to name1, integral constant '30' is assigned to
name2 and so on.
Example Program for enum with default values
#include<stdio.h>
enum day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} ;
int main( )
{
printf("\nSunday = %d ", Sunday) ;
printf("\nMonday = %d ", Monday) ;
printf("\nTuesday = %d ", Tuesday) ;
printf("\nWednesday = %d ", Wednesday) ;
printf("\nThursday = %d ", Thursday) ;
printf("\nFriday = %d ", Friday) ;
printf("\nsaturday = %d ", Saturday) ;
return 0;
}
Output:
Prepared By: M. Parthasaradhi, Asst. Prof.
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
saturday = 6
2.38
3. Argument passing
8. Explain call by reference.
9. Distinguish between Call by value and Call by reference.
Prepared By: M. Parthasaradhi, Asst. Prof.
printf(“%d”, pi);
return 0;
}
2. Using #define:
#include<stdio.h>
#define pi = 3.14
int main( )
{
2.41
in the program explicitly but can be changed by any external device or hardware.
For example, the variable which is used to store system clock is defined as a
volatile variable. The value of this variable is not changed explicitly in the program but is
changed by the clock routine of the operating system.
9. Structures
29. Define structure. Write syntax to declare a structure.
30. What are self-referential structures?
2.42
2.43
1.i.A. Write C program that use non-recursive function, to find the factorial of a given integer.
#include<stdio.h>
int factorial(int);
int main()
{
int n;
printf("Enter a Number: ");
scanf("%d", &n);
printf("The Factorial of %d is %d", n, factorial(n));
return 0;
}
int factorial(int n)
{
int i, fact = 1;
for( i = 1; i <= n; i++)
fact = fact * i;
1
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
1.i.B. Write C program that use recursive function, to find the factorial of a given integer.
#include<stdio.h>
int factorial(int);
int main()
{
int n;
printf("Enter a Number: ");
scanf("%d", &n);
printf("The Factorial of %d is %d", n, factorial(n));
return 0;
}
int factorial(int n)
{
if( n == 0 || n == 1)
return 1;
return n * factorial(n-1);
}
Output:
Enter a Number: 5
The Factorial of 5 is 120
2
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
1.ii.A. Write C program that use non-recursive function, to find the GCD of a given two
integers.
#include<stdio.h>
int gcd(int , int);
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
printf("The GCD of %d and %d is %d", a, b, gcd(a,b));
return 0;
}
int gcd(int a, int b)
{
while( a != b)
{
if( a > b)
a = a - b;
else
b = b - a;
}
return a;
}
3
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
1.ii.B. Write C program that use recursive function, to find the GCD of a given two integers.
#include<stdio.h>
int gcd(int , int);
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
printf("The GCD of %d and %d is %d", a, b, gcd(a,b));
return 0;
}
int gcd(int a, int b)
{
if( b == 0)
return a;
return gcd(b, a % b);
}
Output:
Enter two numbers: 16 30
The GCD of 16 and 30 is 2
4
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
1.iii.A.Write C program that use non-recursive function, to solve Towers of Hanoi problem.
#include<stdio.h>
void TOH(int n)
{
int i, source, dest;
for( i = 1; i < (1 << n); i++)
{
source = (i & (i-1)) % 3;
dest = (((i | (i - 1))) + 1) % 3;
printf("\nMove one disk from %c to %c", source+65, dest+65);
}
}
int main()
{
int number;
printf("Enter number of Disks: ");
scanf("%d", &number);
TOH(number);
return 0;
}
Output:
Enter number of Disks: 3
5
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
1.iii.B. Write C program that use recursive function, to solve Towers of Hanoi problem.
#include<stdio.h>
void TOH(int n, char a, char b, char c)
{
if(n > 0)
{
TOH(n-1,a,c,b);
printf("\nMove Disk-%d from %c to %c", n, a,b);
TOH(n-1,c,b,a);
}
}
int main()
{
int number;
printf("Enter number of Disks: ");
scanf("%d", &number);
// Tower C is Destination
TOH(number, 'A', 'C', 'B');
return 0;
}
Output:
Enter number of Disks: 3
6
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Week 2
a) Write a C program to find both the largest and smallest number in a list of integers.
b) Write a C program that uses functions to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrices
2.A. Write a C program that uses functions to performs Multiplication of Two Matrices
#include<stdio.h>
void Enter_Elements(int a[10][10], int m, int n)
{
int i, j;
for( i=0; i<m; i++)
for( j=0; j<n; j++)
scanf("%d", &a[i][j]);
}
void Display_Elements(int a[10][10], int m, int n)
{
int i, j;
for( i=0; i<m; i++)
{
for( j=0; j<n; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
}
void Multiplication(int a[10][10], int b[10][10], int c[10][10], int m, int n, int q)
int main()
{
int a[10][10], b[10][10], c[10][10];
int m, n, p, q;
printf("Enter The Size of Matrix A: ");
scanf("%d%d", &m, &n);
printf("Enter The Size of Matrix B: ");
scanf("%d%d", &p, &q);
if( n != p)
{
printf("Matrix Multiplication is not possible!");
printf("Number of Columns First Matrix is eualto number of Rows in Second Matrix");
return 0;
}
printf("Matrix Multiplication is Possible\n");
Multiplication(a, b, c, m, n, q);
return 0;
} 8
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Output:
Enter The Size of Matrix A: 2 2
Enter The Size of Matrix B: 2 2
Matrix Multiplication is Possible
Enter 4 Integers into Matrix A:
12
34
Enter 4 Integers into Matrix B:
10
01
Matrix A Elements are:
1 2
3 4
Matrix B Elements are:
1 0
0 1
Multiplication of Matrix A and Matrix B is:
1 2
3 4
9
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
2.B.i. Write a C program that uses functions to performs Addition of Two Matrices
#include<stdio.h>
void Enter_Elements(int a[10][10], int m, int n)
{
int i, j;
for( i=0; i<m; i++)
for( j=0; j<n; j++)
scanf("%d", &a[i][j]);
}
void Display_Elements(int a[10][10], int m, int n)
{
int i, j;
for( i=0; i<m; i++)
{
for( j=0; j<n; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
}
void Addition(int a[10][10], int b[10][10], int c[10][10], int m, int n)
{
int i, j;
int main()
{
int a[10][10], b[10][10], c[10][10];
int m, n;
printf("Enter Matrix Size: "); 10
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Addition(a, b, c, m, n);
return 0;
}
Output:
Enter Matrix Size: 3
2
Enter 6 Integers into Matrix A: 1 2 3 4 5 6
Enter 6 Integers into Matrix B: 7 8 9 1 2 3
Matrix A Elements are:
1 2
2.B.ii. Write a C program that uses functions to performs Multiplication of Two Matrices
#include<stdio.h>
printf("\n");
}
}
void Multiplication(int a[10][10], int b[10][10], int c[10][10], int m, int n, int q)
int main()
{
int a[10][10], b[10][10], c[10][10];
int m, n, p, q;
printf("Enter The Size of Matrix A: ");
scanf("%d%d", &m, &n);
printf("Enter The Size of Matrix B: ");
scanf("%d%d", &p, &q);
if( n != p)
{
printf("Matrix Multiplication is not possible!");
printf("Number of Columns First Matrix is eualto number of Rows in Second Matrix");
return 0;
}
printf("Matrix Multiplication is Possible\n");
Multiplication(a, b, c, m, n, q);
return 0;
} 13
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Output:
Enter The Size of Matrix A: 2 2
Enter The Size of Matrix B: 2 2
Matrix Multiplication is Possible
Enter 4 Integers into Matrix A:
12
34
Enter 4 Integers into Matrix B:
10
01
Matrix A Elements are:
1 2
3 4
Matrix B Elements are:
1 0
0 1
Multiplication of Matrix A and Matrix B is:
1 2
3 4
14
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Week 3
Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) To delete n characters from a given position in a given string.
3.i. Write a C program that uses functions to insert a sub-string in to a given main string from a
given position.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
str3[i] = '\0';
strcpy(str1, str3);
}
15
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
int main()
{
char str1[20], str2[20];
int p;
printf("Enter the string1: ");
gets(str1);
printf("Enter the string2: ");
fflush(stdin);
gets(str2);
printf("Enter the position where the sub-string is to be inserted: ");
scanf("%d", &p);
insert_substring(str1, str2, p);
printf("After inserting the string is %s", str1);
}
Output:
Enter the string1: Good Morning
Enter the string2: Hello
Enter the position where the sub-string is to be inserted1
After inserting the string is HelloGood Morning
16
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
3.ii. Write a C program that uses functions to delete n characters from a given position in a given
string.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Delete_Chars(char str1[], int p, int n)
{
int i,j;
for(i=0,j=0;str1[i]!='\0';i++,j++){
if(i==(p-1)){
i=i+n;
}
str1[j]=str1[i];
}
str1[j]='\0';
}
int main()
{
char str1[20];
int p, n;
printf("Enter the string1: ");
gets(str1);
printf("Enter the number of characters you want to delete: ");
fflush(stdin);
scanf("%d", &n);
Week 4
a) Write a C program that displays the position or index in the string S where the
string T
begins, or – 1 if S doesn‘t contain T.
b) Write a C program to count the lines, words and characters in a given text.
4.A. Write a C program that displays the position or index in the string S where the string T begins,
or – 1 if S doesn‘t contain T.
#include<stdio.h>
#include<string.h>
int main()
{
char s[30], t[20];
char *found;
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
printf("Second String is found in the First String at %d position.\n", found - s);
else
printf("-1");
return 0;
}
18
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
4.B. Write a C program to count the lines, words and characters in a given text.
#include<stdio.h>
int main()
{
char str[200];
int i, line = 1, word = 0, ch = 0;
printf("Enter string terminated with ~ :\n");
scanf("%[^~]", str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]=='\n')
{
line++;
word++;
}
else
{
if(str[i]==' '||str[i]=='\t')
{
word++;
ch++;
}
else {
ch++;
}
}
}
Character counts = 71
Word counts = 11
Line counts = 3
19
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Week 5
a) Write a C Program to perform various arithmetic operations on pointer variables.
b) Write a C Program to demonstrate the following parameter passing mechanisms:
i) call-by-value ii) call-by-reference
5.A. Write a C Program to perform various arithmetic operations on pointer variables.
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Size of int = %d bytes \n", sizeof(int));
printf("Address of p variable is %u \n",p);
printf("p+2 = %u \n",p+2);
printf("p-2 = %u \n",p-2);
printf("p++ = %u \n",p++);
printf("p-- = %u \n",p--);
return 0;
}
Output:
Size of int = 4 bytes
Address of p variable is 6487704
p+2 = 6487712
p-2 = 6487696
20
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
21
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
22
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
Week 6
Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
6. Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, x, y, z;
struct complex a, b, c;
//Addition
c.real = a.real + b.real;
c.img = a.img + b.img;
printf("\nSum of the complex numbers = %d + %di", c.real, c.img);
23
Department of CSE :: ALTS :: ATP
C-Programming & Data Structures Lab (20A05201P):: Week l - Week 6
//Multiplication
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
printf("\nMultiplication of the complex numbers = %d + %di", c.real, c.img);
return 0;
}
Output:
Enter a and b where a + ib is the first complex number:2 5
Enter c and d where c + id is the second complex number:3 4
24
Department of CSE :: ALTS :: ATP
Code: 20A05201T R20
B.Tech I Year I Semester (R20) Regular Examinations August/September 2021
C-PROGRAMMING & DATA STRUCTURES
(Common to IT, ME, FT, CSE, AI&ML, DS, IoF, AI and AI&DS)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) What are the basic data types supported in C programming language?
(b) Why is C called the mother of all languages?
(c) What are static variables and functions?
(d) Differentiate between actual parameters and formal parameters.
(e) What are the operations that can be performed on stack?
(f) Write the applications of stack.
(g) What is singly linked list?
(h) What is a circular linked list? Give an example.
(i) Define BFS with an example.
(j) What is a spanning tree?
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) Explain different decision statements in C language.
(b) What is an array? What are the advantages of arrays over ordinary variables?
OR
3 (a) Explain how arrays are declared and initialized with suitable example.
(b) Write a c program to shift input data by two bits right.
UNIT – II
4 (a) Explain about defining the structure and accessing the structure members with examples.
(b) Explain various dynamic memory allocation functions with examples.
OR
5 (a) Explain the concept of pointer to functions with examples.
(b) Explain any five string handling functions with examples.
UNIT – III
6 (a) Explain the procedure to evaluate postfix expression:
623+-382/+*243+
(b) Explain the procedure to convert infix to postfix using stack.
OR
7 (a) Explain the various operations on a stack.
(b) Explain the basic operations of queue with pseudo code.
UNIT – IV
8 (a) List various operations of linked list and explain how to insert a node anywhere in the list.
(b) Write an algorithm to push and pop an element from linked stack.
OR
9 (a) Explain basic operations of circularly linked lists with suitable example.
(b) Describe how a node can be deleted at a user specified position in a doubly linked list.
UNIT – V
10 (a) Explain the inorder and preorder traversals of a binary tree with suitable example.
(b) Explain about depth first search with suitable examples.
OR
11 (a) Write a short note on connected components.
(b) Sort the elements using selection sort: 52, 38, 81, 22, 48, 13, 69, 93, 14, 45, 58, 79, 72.
***** Page 1
Code: 20A05201T R20
B.Tech I Year I Semester (R20) Regular Examinations November 2021
C-PROGRAMMING & DATA STRUCTURES
(Common to CE, EEE, ECE)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) Define one-dimensional array with suitable example.
(b) Define polynomial ADT?
(c) Explain command line arguments.
(d) Define functions.
(e) What are the disadvantages of representing a stack or queue by linked list?
(f) Convert the following expression into postfix A-B*C+D-E+F/G-H.
(g) What are the limitations of doubly linked lists?
(h) Write the advantages and disadvantages of double linked lists.
(i) Define a full binary tree.
(j) How do you find the degree of a graph?
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 What are the different types of arrays? Give example of each array type and how two-dimension
arrays are represented in memory.
OR
3 Explain in detail about elements of C language
UNIT – II
4 (a) What is recursion? Explain about argument passing techniques.
(b) List and explain string handling functions.
OR
5 (a) Explain different storage classes in detail.
(b) Define pointers. How to declare and initialize pointers, explain with example?
UNIT – III
6 (a) Explain about array representation of stack
OR
7 (a) Write a C program to implement multiple stacks using single array.
(b) Convert the infix expression a / b – c +d * e – a * c into postfix expression and trace that
postfix expression for given data a = 6, b = 3, c = 1, d = 2, e = 4.
UNIT – IV
8 With C-statements, explain how do you create a node, add and delete on a singly linked
list with proper message where each node is containing the details of employee in the form
of Empid, EmpName, Empaddr, and Empsalary as data fields.
OR
9 (a) W hat is a linked list? Explain the different types of linked list with neat diagram.
(b) Explain Circular linked list with example.
UNIT – V
10 With example, explain BFS and DFS.
OR
11 What is a binary tree? State its properties? How it is represented using array and linked
list give example
*****
Page 2
Code: 19A05101T R19
B.Tech I Year I Semester (R19) Supplementary Examinations August/September 2021
PROBLEM SOLVING & PROGRAMMING
(Common to all branches)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) Compare and contrast flowchart and algorithm.
(b) Write the pseudo code to find the given year is a leap year or not.
(c) What will happen if an algorithm does not stop after a finite number of steps?
(d) What is top-down design of problem solving?
(e) What is the difference between a constant and variable?
(f) Write a sample program for ternary operator.
(g) Distinguish between Call by value and Call by reference.
(h) Recall the possible arithmetic operations on pointers in C language?
(i) List some miscellaneous functions in C.
(j) What is the use of typedef?
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 Explain the fundamental units of a computer with a block diagram.
OR
3 (a) Discuss about the classification of computers.
(b) List out the way how algorithm may be represented.
UNIT – II
4 Write algorithm for.
(a) Generation of the Fibonacci sequence.
(b) Reversing the digits of an integer.
OR
5 (a) Illustrate the steps involved in problem solving techniques with neat sketch.
(b) Justify, why C program is called as top-down approach?
(c) Why is verification of an algorithm is important step in problem solving?
UNIT – III
6 (a) What are storage classes? Explain each with an example.
(b) What is recursion? Give an example program.
OR
7 (a) Explain in detail about the formatted and unformatted I/O functions in C.
(b) Compare: (i) Break and continue. (ii) While and Do while.
Contd. in page 2
Page 1 of 2 Page 3
Code: 19A05101T R19
UNIT – IV
8 (a) Write a C program to remove the duplicate elements from an order array.
(b) What are command line arguments? Explain with an example program.
OR
9 (a) Write C program to print the prime numbers less than 500.
(b) What is the output of the following program? Illustrate important steps in your answer.
main ()
{ int a=8, b=4, c;
*p1=&a, *p2=&b;
c=*p1**p2-*p1/ *p2+9;
printf (“%d”,c); }
UNIT – V
10 (a) Write a C program to search an element in a sorted array using binary search.
(b) How will you declare variable-length arguments in C? Explain with an example.
OR
11 (a) Write a C program to store the employee information using structure and search a particular
employee using employee number.
(b) Differentiate between structures and unions.
*****
Page 2 of 2 Page 4
Code: 19A05101T R19
B.Tech I Year I Semester (R19) Regular Examinations January 2020
PROBLEM SOLVING & PROGRAMMING
(Common to all branches)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) What is a compiler? Give example.
(b) Write the algorithm for finding the area of a given square.
(c) List the important steps in solving a problem.
(d) Give an example showing how redundant computations make an algorithm inefficient.
(e) What is type conversion? Give an example.
(f) Illustrate the use of break in loop statements with an example.
(g) What is a command line argument? How do you pass a command line arguments to a C-
program?
(h) Let int a[5] = { 1, 3, 5, 7, 9}. What is the value output of: printf("%d", *(a+2)). Justify your
answer.
(i) What is the use of typedef in C? Give an example.
(j) What are bit-fields? Give an example.
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) Explain in detail about the working of a computer.
(b) Write an algorithm to compute the roots of given quadratic equation.
OR
3 (a) Explain in detail about the strategy designing algorithms with suitable examples wherever for
necessary.
UNIT – II
4 (a) Explain the top-down approach for problem solving.
(b) How do you measure the efficiency of an algorithm9 Explain with an example.
OR
5 (a) Design an algorithm that reads a set of numbers from the user and makes a count of the
number of negative and the number of non-negative members in the set.
(b) Write an algorithm for computing the sum of all even numbers in the range 1 to n (including
both 1 an n). Trace you algorithm for n = 10.
UNIT – III
6 (a) Explain in detail about the arithmetic, relational and logical operators in C with suitable
examples.
OR
7 (a) What is function? What are the advantages of using functions in a C-program? Write C-
function for exchanging the values of two given variables.
(b) Write a C-program for computing the sum of first n terms of the following series using for loop.
S = 1 -3+7-9+11 -13+ …….. 'n' terms.
Contd. in page 2
Page 1 of 2 Page 5
Code: 19A05101T R19
UNIT – IV
8 Design and write algorithm for finding the square root of a given integer. Trace your
algorithm for input value 16.
OR
9 (a) What is a pointer and pointer to a pointer? Illustrate the declaration and use of pointers
and pointer to a pointer in C with suitable example program.
(b) Write an algorithm for reversing the array elements. Trace your algorithm for the
following array of size 10.
a[10] = { 12, 3, 4, 48 87, 54, 67, 43, 78, 19 }
UNIT – V
10 Design and write an algorithm to sort a given set of randomly ordered integers into non-descending
order using insertion method. Trace your algorithm for the following list of integers.
78 43 9 2 12 45 98 22
OR
11 (a) Explain the difference between structures and unions with the help of a C-program.
*****
Page 2 of 2 Page 6
Code: 15A05101
B.Tech I Year I Semester (R15) Regular & Supplementary Examinations December 2016
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) Define precedence and order of evaluation.
(b) List the importance of Flowchart.
(c) Write syntax of If-else statements.
(d) How one Dimensional array is initialized?
(e) Discuss the problems associated with pointers.
(f) Define type qualifiers.
(g) Define function prototype. Give the general syntax of function prototype.
(h) Why do we need structures?
(i) What is the problem with getchar ()?
(j) Define Stream. List different types of Streams.
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) Write an algorithm to find the distance between two points in plane.
(b) With an example, explain the structure of C program.
OR
3 (a) Explain the bitwise operators and relation operators available in C program.
(b) Evaluate the following expressions:
(i) a*(3+b)/2-c++ *b where a=3,b=4 and c=5
(ii) !(4+5*0>=6-4)
UNIT – II
4 (a) Explain switch case statement with an example program.
(b) Write a C program to check whether a given number is Palindrome or not.
OR
5 (a) Explain for loop statement with syntax with an example.
(b) Define an array. Write a program to find the largest and smallest element in a given array.
UNIT – III
6 (a) Define Pointer. Write a C program to find the sum of the all elements in given array using pointers.
(b) Define scope. Briefly explain the scope, life time and visibility of Identifier.
OR
7 (a) List the different storage classes in C and explain each one of them.
(b) Write a C program to exchange the value of two integers using call by reference.
Contd. in page 2
Page 1 of 2 Page 7
Code: 15A05101 R15
UNIT – IV
8 (a) Define structures. Write a C program using functions to return the sum of two complex numbers
passed as parameters.
(b) Explain enumerated data types with an example.
OR
9 (a) Define union. Differentiate between union and structures.
(b) With an example program, explain structure within structure.
UNIT – V
10 (a) Explain fprintf() and fscanf() functions with an example program.
(b) Define Preprocessor directives. Discuss Marco replacement with an example.
OR
11 (a) Write a C program that reads characters from the keyboard and writes them to a disk file until the
user types a dollar sign.
(b) What is Flushing a Stream? Discuss function like Macro With an example.
*****
Page 2 of 2 Page 8
Code: 15A05101 R15
B.Tech I Year I Semester (R15) Regular Examinations December/January 2015/2016
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) Find and remove the error in the following C statement.
a >b ? g = a : g = b;
(b) Define algorithm.
(c) What is the use of continue key word?
(d) Consider the following declaration of 2D-array in C:
char a[R][C];
Assume that char type require one bytes of memory and that array is stored starting from memory
address 2015, find the address of a[i][j].
(e) Write the syntax of dynamic allocation functions in C language.
(f) Give syntax to create a pointer to function.
(g) What is the role of stack in recursion?
(h) Why to use typedef?
(i) Define a file.
(j) What is the use of the preprocessor directives?
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) List and explain the various symbols used in flowchart with figures.
(b) Write a C Program to find maximum number among three numbers using conditional operator.
OR
3 (a) Write an algorithm to find product of two integers using repetitive addition.
(b) Evaluate the following expressions:
(i) 4-768/(6*^2).
(ii) 8^2*2-100+50/5(25-5).
UNIT – II
4 (a) There are four coins a, b, c, d out of which three coins are of equal weight and one coin is heavier.
Write a C program to find the heavier coin.
(b) Explain the ways in which we can pass a one dimension array to functions.
OR
5 (a) Give a note on iteration statements in C language.
(b) A program P reads in 500 integers in the range (0,100) representing the scores of 500 students. It
then prints the frequency of each score above 50. Implement program P in C language.
UNIT – III
6 Give a detailed note on pointer expressions.
OR
7 List and explain the storage classes with examples.
Contd. in page 2
Page 1 of 2 Page 9
Code: 15A05101 R15
UNIT – III
*****
Page 2 of 2
Page 10
Code: 15A05101 R15
B.Tech I Year I Semester (R15) Supplementary Examinations June 2016
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) What is the size of integer data type?
(b) Illustrate in which order any mathematical expression is evaluated.
(c) What is the size required to store any array of 25 integers?
(d) Explain call by reference.
(e) What does this statement indicate p = **a; where p and a are variables?
(f) How many maximum arguments can be passes by return ( )?
(g) Define command line arguments.
(h) Declare a struct name containing field’s first_name, middle_name, last_name within a struct
student.
(i) How to define a global constant?
(j) Write a statement to open a file in reading mode.
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 Write an algorithm to add a list of n elements and display them in ascending order.
OR
3 Write a program in ‘C’ language to print the Fibonacci series.
Fibonacci series is 0, 0, 1, 1, 2, 3, 5, 8, 13, 21
UNIT – II
4 What is the condition to multiply two matrices? Write a ‘C’ program to multiply two matrices.
OR
5 Write a program to print an array in reverse order.
UNIT – III
6 Write a function to swap two integer elements.
OR
7 Explain pointers and arrays with some example programs.
UNIT – IV
8 Differentiate between structure and unions.
OR
9 Write a program using command line to print the statement “I am proud of my country”.
UNIT – V
10 Explain FILE structure in detail.
OR
11 Describe formatted input output statements.
*****
Page 11
Code: 15A05101 R15
B.Tech I Year I Semester (R15) Regular & Supplementary Examinations December 2016
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) Define precedence and order of evaluation.
(b) List the importance of Flowchart.
(c) Write syntax of If-else statements.
(d) How one Dimensional array is initialized?
(e) Discuss the problems associated with pointers.
(f) Define type qualifiers.
(g) Define function prototype. Give the general syntax of function prototype.
(h) Why do we need structures?
(i) What is the problem with getchar ()?
(j) Define Stream. List different types of Streams.
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) Write an algorithm to find the distance between two points in plane.
(b) With an example, explain the structure of C program.
3 (a) Explain the bitwise operators and relation operators available in C program.
(b) Evaluate the following expressions:
(i) a*(3+b)/2-c++ *b where a=3,b=4 and c=5
(ii) !(4+5*0>=6-4)
UNIT – II
4 (a) Explain switch case statement with an example program.
(b) Write a C program to check whether a given number is Palindrome or not.
OR
5 (a) Explain for loop statement with syntax with an example.
(b) Define an array. Write a program to find the largest and smallest element in a given array.
UNIT – III
6 (a) Define Pointer. Write a C program to find the sum of the all elements in given array using pointers.
(b) Define scope. Briefly explain the scope, life time and visibility of Identifier.
OR
7 (a) List the different storage classes in C and explain each one of them.
(b) Write a C program to exchange the value of two integers using call by reference.
Contd. in page 2
Page 1 of 2
Page 12
Code: 15A05101 R15
UNIT – IV
8 (a) Define structures. Write a C program using functions to return the sum of two complex numbers
passed as parameters.
(b) Explain enumerated data types with an example.
OR
9 (a) Define union. Differentiate between union and structures.
(b) With an example program, explain structure within structure.
UNIT – V
10 (a) Explain fprintf() and fscanf() functions with an example program.
(b) Define Preprocessor directives. Discuss Marco replacement with an example.
OR
11 (a) Write a C program that reads characters from the keyboard and writes them to a disk file until the
user types a dollar sign.
(b) What is Flushing a Stream? Discuss function like Macro With an example.
*****
Page 2 of 2
Page 13
Code: 15A05101
R15
B.Tech I Year I Semester (R15) Supplementary Examinations June 2017
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
PART - B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT - I
2 (a) Write an algorithm to find the roots of a quadratic equation.
(b) List and explain the various symbols used in flowchart with figures.
OR
3 (a) Write an algorithm to check the given number is perfect number or not.
(b) Explain the bitwise operators and relation operators available in C program.
UNIT - II
4 Discuss selection statements with a suitable example for each.
OR
5 Write a C program for matrix multiplication.
UNIT - III
6 Explain dynamic memory allocation functions of C with a suitable example.
OR
7 Compare call by value with call by reference and explain using a suitable example.
UNIT - IV
8 Write a C program to demonstrate the use of array of structures.
OR
9 What is union? Write a C program to store information in a union and display it.
UNIT - V
10 Write a C program to read name and marks of N number of students from user and store them in a file.
OR
11 Write a C program to demonstrate the use of fscanf and fprintf functions.
Page 14
Code: 15A05101 R15
B.Tech I Year I Semester (R15) Regular & Supplementary Examinations December 2017
COMPUTER PROGRAMMING
(Common to CE, EEE, CSE, ECE, ME, EIE and IT)
Time: 3 hours Max. Marks: 70
PART - A
(Compulsory Question)
PART - B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT - I
2 What is an Algorithm? Write an algorithm to compute factorial of a number n where n>= 0.
OR
3 Explain the following operators: (i) Arithmetic operator. (ii) Relational operator. (iii)
Logical operator. (iv) Increment & decrement operator. (v) Conditional operator.
UNIT- II
4 What do you mean by a loop? Explain the difference between the do loop, while loop, and for
loop with the help of an example.
OR
5 (a) Write a C function for searching an element in an array of size N. Assume that elements
in array are stored in ascending order.
(b) Explain the declaration int (*p (char `a))(10].
UNIT - III
6 What is meant by the scope of variables and summarize types of storage class in C?
OR
7 Write a program to assign any number at random to an integer variable k and display the
same through pointer.
UNIT- IV
8 What is the difference between a structure declaration and a structure initialization?
OR
9 Define a function & explain why function prototype is essential.
UNIT- V
10 Write different built-in (library) functions provided by 'C' language for handling I/O
operations on files.
OR
11What are the features of C preprocessor? Give the differences between macros and functions.
Page 15
Code: 15A05101 R15
B.Tech I Year I Semester (R15) Supplementary Examinations June 2018
COMPUTER PROGRAMMING
(Common to all branches)
Time: 3 hours Max. Marks: 70
PART – A
(Compulsory Question)
*****
1 Answer the following: (10 X 02 = 20 Marks)
(a) What are the bitwise operators in C?
(b) Justify the significance of operator precedence with an example.
(c) List the string functions in C.
(d) Write a C program for call by reference.
(e) Illustrate with a suitable example the use of return statement.
(f) Explain malloc() function.
(g) Illustrate recursion with an example.
(h) What is the use of typedef in C?
(i) Discuss pre-processor directives in C.
(j) List the basic console I/O functions.
PART – B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT – I
2 (a) Discuss about procedural oriented languages.
(b) Explain in detail about the software development method.
OR
3 (a) Write an algorithm to find the distance between two points in plane.
(b) Explain the bitwise operators and relation operators available in C
UNIT – II
4 What are the different types of arrays in C? Explain with a suitable example, array
declaration,initialization and accessing of the elements for these different types.
OR
5 Write a C program to calculate the sum of all elements of an array using pointers as arguments.
UNIT – III
6 What is a pointer? Write a program in C to reflect the concept of pointers to functions.
OR
7 Illustrate with an example, the significance of argc and argv arguments passed to the main function.
UNIT – IV
8 Explain all the function prototypes with examples.
OR
9 Demonstrate with a suitable example, the use of structure within a structure.
UNIT – V
10 Write a program in C to illustrate the use of rewind and fseek functions.
OR
11 Write a program in C that reads files and displays them on the screen.
*****
Page 16
Code: 15A05101
R15
B.Tech I Year I Semester (R15) Regular & Supplementary Examinations November/December 2018
COMPUTER PROGRAMMING
(Common to all branches)
Time: 3 hours Max. Marks: 70
PART - A
(Compulsory Question)
1 Answer the following: (10 X 02 = 20 Marks)
(a) What is software? Give at least two examples for system software and application software.
(b) Write an a:gorithm to find the largest number among three numbers.
(c) Distinguish between continue and break statements.
(d) Write a program in C to find the sum of numbers present in an array.
(e) Li st the advant ages of functi on s.
(f) Predict the result for the fo: owing code
const int i = 5:
i++:
printf("i value = `Yod",i):
(g) D e f i n e r e c u r s i o n .
(h) In what way actual arguments differ from formal arguments.
(i) Write the syntax to read string from the keyboard. Give example.
(j) How can we read the character and set of characters? Give examples for each one .
PART - B
(Answer all five units, 5 X 10 = 50 Marks)
UNIT -1
2 (a) Using conditional operator write a C program to find the largest number from two numbers.
(b) Draw the typical b ock diagram for a computer. Explain it in detail.
OR
3 (a) What is a flow chart? List and explain the different notations used to represent a flowchart.
(b) Write a C program to find the sum of first six natural numbers.
UNIT - II
4 (a) How arrays differ from normal variables? Write a C program to find the largest number present
in an array.
(b) In what way if statements differ from switch case statements.
OR
5 Write a program in C language to perform the matrix multiplication.
UNIT - III
6 (a) Describe the pointer to pointer mechanism.
(b) Illustrate the mechanism of passing the pointer variables as arguments to the functions.
OR
7 How can we define the scope of a function? List the rules for defining the scope of a variable. Give
brief description about the differ storage specifiers.
UNIT – IV
8 (a) Distinguish between structures and unions.
(b) Explain with example structures within structures.
OR
9 (a) List the advantages of using functions. In what way a built - in function differs from user defined
(b) Write short notes on command line arguments.
UNIT - V
10 (a) Write and explain the different types of files.
(b) Describe the macros with suitable example.
OR
11 (a) Give brief description about the various modes of a file.
(b) Explain the role of files in C programming.
Page 17