Vous êtes sur la page 1sur 61

UNIT-I

Objective:

 To review basic computer systems concepts

 To be able to understand the different computing environments and their components

 To review the history of computer languages

 To be able to list and describe the classifications of computer languages

 To understand the steps in the development of a computer program

 To review the system development life cycle

Outcome:

 In this Chapter the student will come to know how Computer Works.

 In this Chapter the student will come to know what are the various Hardware and Software
components of computers and there functionality.

 In this Chapter the student will learn different types of programming languages like(Machine
level language, High level language, Middle level language).

 In this Chapter the student will learn the internal process of the program execution
like(Linker,Loader,Complier)

 In this Chapter the student will learn the software development methodology

 In this Chapter the student will learn the Flowcharts,Algorithims,pseudocode

Syllabus:-

Introduction to computers, Computer Systems,Programing Languages,Programing


Environment, Creating and Running Programs, Software Development Method,Applaying Software
Development Method, Problem Sloving,Algorithms,FlowCharts,Pseudocode,Problem Solving
Examples.

Points:

1. Software:-Set of instructions is called as software


Two Types of Software:-
System Software, Application Software
Application software is designed for user task
System software is designed for system task
2. Hardware:-The electronic devices of computer are called as hardware
3. Input Devices:-keyboard,mouse,Scanner
4. CPU:-Motherboard,Ram,vodeoCard,SoundCard
5. Output devices:-Monitor,Printer,Speaker,Plotter
6. Memory:-
Primary memory:-
ROM, RAM, Cache memory
Secondary memory:-
Magnetic storage device (magnetic tapes), Optical storage device(CD-ROM,CD-RW,DVD-
ROM), Magneto-optical storage device, Universal serial bus(USB)
7. Machine Language:-the Machine understandable code i.e. 0’s and 1’s code
8. Assembly Language:-It consist of series of instructions and mnemonics that correspond to a
stream of executable instructions. It consist of OPCODE
9. High-Level language:-User understandable code .the programming languages
COBAL,Pascal,FORTAN,C comes under High level language
10. Procedure-Oriented code:-High level language designed to solve general purpose problems
are called procedural language
11. Problem-oriented Languages:-Specific problems can be solved by using the Problem
Oriented code
12. Natural Languages:-are designed to make a computer to behave like an expert and solve
problems .the programmer just needs to specify the problem and the constraints for
problem solving.
13. Structured Programing :-It is a key programming paradigms It approach mainly focuses on
the order of execution of the statements with in a program
14. Assembler:-It is a computer program that translate assembly language statements into
machine language codes.
15. Compiler:-which is used to compile the program and generate the object file. during
compilation the errors are checked
16. Interpreter: The instructions are executed line by line and if any line has error then it wont
go the other line
17. Debuggers:-It is a software that is used to detect the errors and bugs present in the
program
18. Editors:-where the user write the program and compile it and execute it
19. SDM(Software Development Method)
20. Analyzing Requirements:-Getting the Requirements from the client
21. Feasibility Analysis:-Determining development alternatives,Analysing economic feasibility
Assessing technical feasibility
22. Creating the design:-creating the architecture and design of the new software. This step
involves developing a logical model or basic structure of the new software
23. Developing Code:-The code for different modules is developed according to the design
specifications of the each module
24. Testing the Software:-It is basically performed to detect the prevalence of any errors in the
new software and rectify those errors
25. Deploying the software:-Installation of the project is called as deployment
26. Maintaining the software:-Continuous support is provided to it for ensuring its full-time
availability
27. Algorithm:-It helps the programmer in breaking down thesoultion of problem into a
number of sequential steps.Corresponding to each step a statement is written in a
programming language,all these statements are collectively termed as a program
28. FlowChart:-It can be defined as a pictorial representation of process which describes the
sequences and flow of the control and information within the process.
29. Pseudocodes:-Analyzing a detailed algorithm before developing a program is very time
consuming .hence there arises a need of specification that only focuses on the logic of the
program.Pseudocodes serve this purpose by specifying only the logic which is used by the
programmer for developing a computer program.

Multiple choice questions:

1. Which of the following is a component(s) of a computer system? [ ]

a) Hardware
b) Software
c) Both Hardware and Software
d) none

2. Which of the following computing Environment? [ ]


a) Time-Sharing
b) Personal Computing Environment
c) Distributed environment
d) All the above

3. SDLC stands for ? [ ]


a) Software Development Life cycle
b) Software Development location Center
c) Software Detection life cycle
d) None of the above
4. Which of the following is the printer [ ]
a) Laser
b) Dot-matrix
c) Inkjet
d) All of the above

5. Which of the following is used to link the libraries to our program. [ ]


a) Loader
b) Compiler
c) Linker
d) Debugger

6. In SDLC what is the first phase [ ]


a) Requirement Analysis
b) Testing
c) Designing
d) Implementation

7. By which of the following the program Execution is represented with symbols. [ ]


a) Flowchart
b) Algorithm
c) Pseudo code
d) All the above

8. Which of the following is Volatile memory? [ ]


a) ROM
b) RAM
c) Hard Disk
d) CD

9. The Repeated Instructions are stored in which memory.


[ ]
a) RAM
b) Cache
c) ROM
d) None of the above

10. Which of the following is the system software [ ]


a) C
b) MS-Office
c) OS
d) None of the above
Multiple Choice Answers:-

1) C 2) D 3) A 4)D 5) C 6) A 7)A 8)B 9)B 10) C

Fill in the blanks:

1. C is ______Language .

2. Secondary storage is used for_____________.

3. EPROM stands for__________.

4. _________operations are perform by ALU.

5. When C program Compiled ___________ is Generated.

6. In Flow chart Conditions are reprented by _________Sysmbols.

7. _____________ is used to Load the Program.

8. Pseudo code is ____________.

9. Machine Language program consist of instructions written in ____________.

Fill in the blanks answers:

1) Structured Programming 2) Permanent Storage 3) Erasable Programmable ROM 4) Arithmetic


and Logical 5) Object code 6) Rhombus 7) Loader 8) Input 9) Language Independent 10) 0’s and 1’s

True or False:

1. SDLC is a Programming Technique?

2. Flowchart consist of symbols?

3. Computer can understand human understandable language?

4. Loader and Linker are not used to compile the program?

5. OS is a programming Language?

6. C programming is a Structured programming Language?


7. Scanner is an input Device?

8. Algorithm and Pseudo code are same?

9. CRT stands for Cathode Ray Tube?

10. Object code is user understandable code?

Answers:

1) False 2) True 3) False 4)True 5) False 6)True 7) True 8) False 9) True 10) False

Match the following:

1) SDLC a) Erasable Programmable ROM

2) CD b) Checking the errors

3) DVD c) Integrated Chip

4) EPROM d) Bus

5)Debugger e) Read Only Memory

6)IC f)Digital Versatile Disk

7)ROM g) Compact Disk

8) Plotter h) Disk Surface Area

9) Data cable is called i) Random Access Memory

10) RAM j)SoftWare Development Life Cycle

Matching answers:

1) J 2)G 3)F 4)A 5) B 6)C 7)E 8)H 9)D 10)I


Assignment:-

1. Design a Flow chat to find whether a given number is prime or not and also find it is even or
odd?

2 Design and develop the solution for finding the roots of quadratic equation by using algorithm
and flow chart

3. Design and develop the solution for finding the area of circle?

4. Design and develop the solution for determining whether there is profit or loss during the
selling of an item

5. Develop Pseudo code to determine whether the given year is leap year or not

Tutorial Problems

1. What are the some input devices? Briefly explain some popular input devices.

2. What is a flow? Chart? How it is different from algorithm and pseudo code?

3. Design an algorithm to compute 1/n! for a given n(n>0).

4. Design an algorithm to compute xn/n!

5. Design an algorithm to determine whether or not a number n is factorial number

6. Design an Algorithm to simulate multiplication by addition. The program should accept as input
two integers (they may be zero, positive, or negative)

Questions from previous exams.


1. Write an algorithm to read ten positive integers and find out how many are perfect squares
(such as 49,81).You may assume that the input values read are in the range 1 to 10000.

2. List various steps in Software Development.

3. Write an Algorithm to find out all the factors of given positive integer.

4. What is Flowchart? Draw a flow chart to read ten integers and print the sum of squares of all
ten values.

5. Draw a flowchart to Read ten positive integers and how many are multiples of 7.
UNIT II

Objective:

 The student will be able to learn basic C concepts

 Students can learn different data types.

 Students can create their own basic C Programs

 Concepts of Operators.

 Process of statements

 Use typedef statement

 Students can learn different C Control statements

Outcome:

 In this unit students will gain the basics of C programming concepts

 The students will get the complete information about operators, expressions,
statements. And control statements

Syllabus:

Unit-II Introduction to C language-background, simple C programs, identifiers, basic data


types, variables,constants,input/output,operators,expressions,precedence and
associativity,expression evaluation, type conversions, bit-wise operators,statements,simple C
programming examples.

Selection statements-if and switch statements, repetition statements-while,for,do-while


statements, Loop examples, other statements related to looping-break,continue,goto, simple C
programming examples.
Points:

1. C is a Structured programming language which born at “AT & T’s Bell Laboratories” of
USA in 1972. It was written by Dennis Ritchie.

2. This language was created for a specific purpose: to design the UNIX operating
system (which is used on many computers). From the beginning, C was intended to
be useful--to allow busy programmers to get things done.

3. Because C is such a powerful, dominant and supple language, its use quickly spread
beyond Bell Labs.

4. In the late 70’s C began to replace widespread well-known languages of that time like
PL/I, ALGOL etc.

5. Programmers everywhere began using it to write all sorts of programs. Soon,


however, different organizations began applying their own versions of C with a subtle
difference.

6. This posed a serious problem for system developers. To solve this problem, the
American National Standards Institute (ANSI) formed a committee in 1983 to
establish a standard definition of C.

7. This committee approved a version of C in 1989 which is known as ANSI C. With few
exceptions, every modern C compiler has the ability to adhere to this standard. ANSI
C was then approved by the International Standards Organization (ISO) in 1990.

8. The C language is named because its predecessor was called BCPL (Basic computer
programming language). The BCPL language was developed by Ken Thompson of Bell
Labs.
9. The basic characteristics of C are:
o small in size
o loose typing (lots of freedom)
o structured (extensive use of functions)
o designed for systems programming (i.e., low level programming of the type
needed to implement an operating system)
o C is higher level than assembler but still close to the hardware and allows
direct manipulation of many system aspects: pointers, memory allocation,
bitwise manipulation ...

Simple C Pragramme:

C Basic Program Structure:

1. Documentation Section:
The documentation section consists of a set of comment lines giving the name of
the program, the name and other details, which the programmer would like to use later.

2. Preprocessor Statements
The preprocessor statement begin with # symbol and are also called the preprocessor
directive. These statements instruct the compiler to include C preprocessors such as
header files and symbolic constants before compiling the C program. Some of the
preprocessor statements are listed below.

3. Global Declarations
The variables are declared before the main ( ) function as well as user
defined functions is called global variables. These global variables can be accessed by all
the user defined functions including main ( ) function.
4. The main ( ) function

Each and Every C program should contain only one main ( ). The C
program execution starts with main ( ) function. No C program is executed without the main
function. The main ( ) function should be written in small (lowercase) letters and it should not
be terminated by semicolon. Main ( ) executes user defined program statements, library
functions and user defined functions and all these statements should be enclosed within left
andrightbraces.

Braces({})
Every C program should have a pair of curly braces ({, }). The left braces indicates the
beginning of the main ( ) function and the right braces indicates the end of the main ( ) function.
These braces can also be used to indicate the user-defined functions beginning and ending.
These two braces can also be used in compound statements.

LocalDeclarations

The variable declaration is a part of C program and all the variables are used in main ( )
function should be declared in the local declaration section is called local variables. Not only
variables, we can also declare arrays, functions, pointers etc. These variables can also be
initialized with basic data types. For examples.

Code:
Main ( )
{
int sum = 0;
int x;
float y;
}
Here, the variable sum is declared as integer variable and it is initialized to zero. Other variables
declared as int and float and these variables inside any function are called local variables.

Program statements

These statements are building blocks of a program. They represent instructions to the
computer to perform a specific task (operations). An instruction may contain an input-output
statements, arithmetic statements, control statements, simple assignment statements and any
other statements and it also includes comments that are enclosed within /* and */ . The
comment statements are not compiled and executed and each executable statement should be
terminated with semicolon.

User defined functions


These are subprograms, generally, a subprogram is a function and these functions are written by
the user are called user ; defined functions. These functions are performed by user specific tasks
and this also contains set of program statements. They may be written before or after a main ()
function and called within main () function. This is an optional to the programmer.
Now, let us write a small program to display some message shown below.
Code:

# include <stdio.h>
main()
{
printf ("welcome to sbit /n");
}

Identifiers:

C Identifiers allow us to name data(variable) and other objects(functions) in the


program. Each identified object in the computer is stored at a unique address.

Rules:

o Note: An identifier must start with a letter or underscore:


it may not have a space or a hyphen.
o C is a case-sensitive language.
Examples:
 Data types:

C 'data types' which are used to define a variable before its use. The definition of a
variable will assign storage for the variable and define the type of data that will be held in the
location.

 The value of a variable can be changed any time.

 C has the following basic built-in data types.

 int  integer numbers

 float  floating point numbers

 double  BIG floating point numbers

 char  characters

 The C data types explained above have the following modifiers.

 short

 long

 signed

 unsigned

 The modifiers define the amount of storage allocated to the variable. The amount of
storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int


float <= double <= long double
Variables:

 A variable is an identifier which is used to store the data or values in memory location

1. We can store data or values in variables.

2. Each variable is identified by a variable name.

3. Each variable has a variable type.

4. Variable names start with a letter or underscore (_), followed by any number of
letters, digits, or underscores.

5. Uppercase is different from lowercase, so the names sam, Sam, and SAM specify
three different variables.

6. The variables are defined at the beginning of the main block.

Example: int a, float count, int rev, int rem, char ch

 Constants:

Constants are data values that cannot be changed during the execution of a
program

Example int i=5( 5 is constant)


char ch=’a’ (a is character constant)
 Constant types :

 Numeric constants

o Integer constant

 Decimal Constants(0-9)

 Octal Constants(0-7) -starts with 0 Ex: 012, 017, 027,019

 Hexa Decimal Constants(0-9, A-F) –starts with 0x Ex:0x1A,0x29,A12B

o Real Constants

 Floating point Constants- must contain a decimal point end with f Ex:
1.24f,5.67f,0.3f

 Character Constants

o Single Character Constants – should enclosed in ‘ ‘ – Ex: ‘a’,’0’.

o Escape Characters – starts with \ - Ex: ‘\n’,’\t’…

o String Constants – should enclosed in “” Ex: “123”, “hello”,”@dld”.

 Input/output:

A terminal keyboard and monitor can be associated to perform input and output
operations. A keyboard is a source for a text stream to read the dada; a monitor is a destination
for a text stream to print the data.
Output:

Input:
Keywords:
The following are the C language supported key words

 Operators:

Operators are Used to perform different operations between operands

• Arithmetic Operators (+,-,*, /, %)


• Relational Operators (<,>, <=,>=,==,! =)
• Logical Operators (&&, ||,!)
• Bitwise Operators (&,|,^,~)
• Assignment Operators (=, +=,*=,-=,/=,%=,&=,|=)
• Bit wise Shift Operators (right shift >>, left shift <<)

Expression:

Collection of operators and operands is an expression. Operators are symbols which take
one or more operands or expressions and perform arithmetic or logical computations.
Ex: a+b+c+d,(a+b)*(c/d)
Precedence and Associativity:

Precedence table:
Operator precedence is why the expression 5 + 3 * 2 is calculated as 5 + (3 * 2), giving
11, and not as (5 + 3) * 2, giving 16.

We say that the multiplication operator (*) has higher "precedence" or "priority" than the
addition operator (+), so the multiplication must be performed first.

Associativity table:
Operator Associativity is why the expression 8 - 3 - 2 is calculated as (8 - 3) - 2, giving 3,
and not as 8 - (3 - 2), giving 7.

We say that the subtraction operator (-) is "left associative", so the left subtraction must
be performed first. When we can't decide by operator precedence alone in which order to
calculate an expression, we must use associativity.

Since the operators + and - have the same precedence, and are left-associative, the
following expressions are all equivalent:

x+3-y+5
(x + 3) - y + 5
((x + 3) - y) + 5
((x + 3) - y + 5)

Since the operators =, += and -= have the same precedence, and are right-associative,
the following expressions are all equivalent:

x = y += z -= 4
x = y += (z -= 4)
x = (y += (z -= 4))
(x = y += (z -= 4))
Expression Evaluation:

Expression evaluation is done from left to right or right to left and based on operator
precedence.
Here precedence of ++ is higher than + - / * operators. And, precedence of * / is higher than + -
operators. So, the expression must be computed as:
g = f / 4 + f * 2 / ++f;
g = (f / 4) + ((f * 2) / ++f);
........1....5......2....4...3....
and the result must be:
g = 2.6

Type Conversion:

There are two ways of casting data types in C:

· Automatic type casting (implicit)

· Manual type casting (explicit)

Implicit Casting (automatic transformation) works in a way that a variable (operand) of


data type that is smaller in length (than data type of second variable) (operand), transforming
internally to variable of data type with longer number length. It may sound mixed up, but here
is an example:

short int -> int -> unsigned int ->long int -> unsigned long int -> float -> double -> long double

Explicit Casting (given transformation) of data types has higher priority then automatic
transformation. General declaration of explicit (given) cast (cast operator):

(data type) operand

Operand can be variable or phrase.

Example:

a = (int) c;

b = (double) d + c;

Bit wise Operators:


• Bitwise "and" operator &

• Bitwise "or" operator |

• Bitwise "exclusive or" operator ^

• Bitwise "ones complement" operator ~

• Shift left <<

• Shift right >>

C Statements:

C statements consist of constants, variables, identifiers, expressions, and other


statements. A statement that forms a component of another statement is called the "body" of
the enclosing statement. Each statement type given by the following syntax .

Statement types :
1. Labeled-statement
a. go to statement
2. Expression-statement
3. Selection-statement
a. if statement
i. simple if statement
ii. if else statement
iii. else if ladder statement
b. switch statement
4. Iteration-statement
a. While loop statement
b. do while loop statement
c. for loop statement
5. Jump-statement
a. Break statement
b. Continue statement
6. Selection statements:
If Statement:

If the expression is true then the statement or block of statements gets


executed otherwise these statements are skipped.

Syntax:

Simple if statement If else statement Nested if or else if ladder

if (expression) if (expression) if (expression)


statement; { {
Block of statements; Block of statements;
or } }
else else if(expression)
if (expression) { {
{ Block of statements; Block of statements;
Block of statements; } }
} else
{
Block of statements;
}

7. Switch statement:

A switch statement is a type of selection control mechanism.

Syntax:

switch( expression )
{
case constant-expression1:
statements1;
break;
case constant-expression2:
statements2;
break;
case constant-expression3:
statements3;
break;
default:
statement 4;
}

8. Break statement:

If a condition is matched in switch case then execution continues on into the next case
also if it is not explicitly specified that the execution should exit the switch statement. This is
achieved by using break keyword.

Syntax:

break;

9. Default Statement:

If none of the listed conditions is met then default condition executed.

Syntax

default:
statements;

10. Loop Statements:

Loops provide a way to repeat statements and control how many times they are
repeated.

while loop

if the test condition is true: the statements get executed., the test condition is checked
again. If it is still true the statements get executed again. This cycle repeats until the test
condition evaluates to false.

syntax:

while ( test condition )


{
Statements;
}
do...while loop:

The do While statement is similar to the while statement except that its termination
condition is at the end. the body of the loop executes at least once, regardless of the condition.

Syntax:

do
{
Statements;
}while(expression);

for loop:

for loop is similar to while..

 expression1 - Initializes variables.

 expression2 - Conditional expression, as long as this condition is true, loop will keep
executing.

 expression3 - expression3 is the modifier which may be simple increment of a variable.

Syatax

for( expression1; expression2; expression3)


{
Statements;
}

11. Continue Statement :

The continue statement provides an immediate jump to the loop control statement.

Syntax:

Continue;

12. Labeled Statement:

go to statement:
The goto statement is a jump statement which jumps from one point to
another point within a function. The goto statement is marked by label statement. Label
statement can be used anywhere in the function.

Syntax;

A:
statements
if(condition)
goto A;

MULTIPLE CHOICE QUESTIONS

1. What is the result of 16>>2? [ ]


1) 4
2) 8
3) 3
4) 0
2. What is the output of the following code? [ ]

#include<stdio.h>
void main()
{
char letter =`Z`;
printf("\n%c",letter);
}

1) Z
2) 90
3) Error
4) Garbage Value
3. What is the output of the following code? [ ]

#include<stdio.h>
void main()
{
int s=0;
while(s++<10)
{
if(s<4 && s<9)
continue;
printf("\n%d\t",s);
}
}

1) 1 2 3 4 5 6 7 8 9
2) 1 2 3 10
3) 4 5 6 7 8 9 10
4) 4 5 6 7 8 9

4. Find output [ ]

#include <stdio.h>
void main()
{
int a; printf("%d",a^a);
}
1) 1
2) 0
3) Unexpected
4) Runtime Error
UNIT-III

Objective:

1. The student will be able to learn the importance of functions and structured
programming.
2. The student will be able to understand various types of types of pre-defined and user
defined functions.
3. The student will be able to understand the concept of arrays along with their types.
4. Applications of arrays, various types of storage classes and inter function
communication.
5. The student will be able to get capability of implementing programs in functions and
arrays.

Outcome:

1. After completion of this chapter the student will come to know the concepts of
structured programming.
2. The student will come to know types of functions and recursion concepts along with
programming.
3. The student will come to know the definition and types of arrays along with the
advantages of arrays.
4. The student will come to know the types of arrays and application of arrays.

Points:

1. In software development, functions play an important role since they allow dividing
of the program into subprograms.
2. Functions are vital to C programming; main is a specially recognized function in C.
3. Every program must have a main function to indicate where the program has to
begin its execution.
4. While it is possible to code any program utilizing only main function, it leads to a
number of problems.
5. If a program is divided into functional parts, then each part may be independently
coded and later combined into a single unit.
6. These independently coded programs are called subprograms that are much easier
to understand, debug and test. In C such subprograms are referred to as ‘functions’.
7. A Function is an independent, reusable module of statements, that specified by a
name. This module (sub program) can be called by its name to do a specific task.
8. We can call the function, for any number of times and from anywhere in the
program.
9. The purpose of a function is to receive zero or more pieces of data, operate on them,
and return at most one piece of data.
10. A Called Function receives control from a Calling Function. When the called function
completes its task, it returns control to the calling function. It may or may not return
a value to the caller.
11. The function main () is called by the operating system; main () calls other functions.
When main () is complete, control returns to the operating system.
12. Function prototypes: functions should be declared before they are used. ANSI
provides a new function declaration called the function prototype.
13. A function prototype tells the compiler the number and type of arguments that are
to be passed to function and the type of value that is to be returned by the function.
14.

P
reprocessor Directives
#define - Define a macro substitution
#undef - Undefines a macro
#ifdef - Test for a macro definition
#ifndef - Tests whether a macro is not defined
#include - Specifies the files to be included
#if - Test a compile-time condition
#else - Specifies alternatives when #if test fails
#elif - Provides alternative test facility
#endif - Specifies the end of #if
#pragma - Specifies certain instructions
#error - Stops compilation when an error occurs
# - String zing operator
## - Token-pasting operator

Objective type questions:

1. The default return type of a function is [ ]


a. Void b. int c. float d.char
2. Which of the following are the correct function declarations [ ]
a. Int funct(int a,b;); b. int funct(int a,int b); c. int funct(int,int);d. int funct(int,);
3. Which of the following is not feasible [ ]
a. Functions with no return values and no arguments
b. Functions with no return values and arguments
c. Functions with return values and no arguments
d. All the above are feasible
4. ‘call by reference’ function call uses the following type of parameter [ ]
a. Pointer variables
b. Integer variables
c. Address variables
d. Memory variables
5. Recursion is a situation [ ]
a. A function calls the main()
b. A function calls any of the system functions
c. A function calls itself
d. None of the above
6. Which of the following is not a variable storage class [ ]
a. Automatic b. external c. static d. dynamic
7. Which of the following keywords is used for declaring external variable [ ]
a. External b. extern c. auto extern d. ext
8. Which of the following refers to the region of a program where a variable is available for
use
a. Scope b. visibility life time none of the above [ ]
9. The formal arguments in the function header must be prefixed by which of the following
indirection operator [ ]
a. * b. + c. - d. /
10. Which of the following types of variables remain alive for the entire lifetime of a
program [ ]
a. Extern b. auto c. static d. register
11. Which of the following is optional in a function definition [ ]
a. Function name b. function type c. local variable declaration d. return statement
12. Array is an example of which of the following? [ ]
a. Derived types b. fundamental types c. user-defined types d. none
13. Which of the following is not a data structure [ ]
a. Linked list b. stack c. tree d. pointer
14. Int a[n] will reserve how many locations in the memory [ ]
a. N b. n-1 c. n+1 d. none
15. Which of the following is not the name of a sorting technique? [ ]
a. Bubble b. selection c. binary d. insertion
16. Which of the following is not the name of a searching technique? [ ]
a. sequential b. selection c. binary d. all
17. Which of the following is the correct expression for retrieving the last-row-last-column
value of a 5×4 array matrix A.? [ ]
a. A [4, 5] b. A[5,4] c. A[5]->A[4] d. A[4]-> A[5]

Answers: 1-a, 2-c, 3-d, 4-c, 5-c, 6-d, 7-b, 8-a, 9-a, 10-a, 11-d, 12- a, 13-d, 14- a, 15-c, 16-b, 17b.-

Fill in the blanks:

1. The parameters used in function call are called------------------


2. A variable declared inside a function is called--------------------
3. By default, ----------------------is the return type of C function.
4. In passing by pointers the variables if the formal parameters must be prefixed with
in-----------their declaration.
5. In prototype declaration specifying is----------------optional.
6. ----------------refers to the region where a variable is actually available for use.
7. A function that calls itself is known as a ------------------function.
8. If a local variable has to retain its value between calls to the function it must be declared
as-----------
9. A-----------------aids the compiler to check the matching between the actual and formal
arguments.
10. A variable declared inside a function by default assumes--------storage class.
11. An array created using malloc function at run time is referred to as------array.
12. -----------is the process of arranging the elements of an array in order.
13. An array variable can be initialized either at compile time or at --------------.
14. An array that uses more than two subscript is referred to as----------------array.
15. Array name itself acts like a---------------.

Answers: 1. Actual parameters 2. Local variable 3. Void 4. * 5.

6. scope 7. Recursion 8. Return type 9. Prototype declaration 10. Auto 11. dynamic

12. Sorting 13. Run-time 14. Multidimensional array. 15. Pointer.

True/False:

1. C functions can return only one value under their function name. [true]
2. A function in C should have at least one argument. [false]
3. A function can be defined and placed before the main (). [true]
4. A function can be defined within the main (). [false]
5. A user-defined function must be called once; otherwise a warning message will be
issued. [true]
6. Any name can be used as a function name [true]
7. Only a void type function can have void as its argument [false]
8. A function can call itself. [true]
9. A function without a return statement is illegal. [false]
10. The return type of a function is int by default. [false]
11. The type all elements in an array must be same. [true]
12. When an array is declared C automatically initializes its elements to zero. [false]
13. In C by default the first subscript is zero. [true]
14. An unsigned long int type can be used as a subscript in an array. [false]
15. Accessing an array outside its range is a compile time error. [true]

Assignment questions:

1. Write a function exchange to interchange the values of two variables. Say x and y.
illustrate the use of this function, in a calling function. Assume that x and y are defined
as global variables.
2. Write a function prime that returns 1 if its argument is a prime number and returns zero
otherwise.
3. Write function that will scan a character string passed as an argument and convert all
lowercase letters into their uppercase equivalents.
4. Write a function that will generate and print the first n Fibonacci numbers.
5. Write a function that demonstrates recursion.
UNIT-IV

Syllabus:

Pointers-Introduction,pointers for inter function communication,pointers to pointers,array


of pointers, programming applications, pointers to void, pointers to functions,command-line-
arguments. Strings-concepts,C strings, String I/O functions,array of strings,string manipulation
functions,String data conversion Cprogramming examples.

Objective:

1. The student will be able to learn the importance of pointers.


2. The student will be able to understand advantages of pointers.
3. The student will be able to understand the concept of one dimensional character
arrays i.e. strings.
4. The student will be able to understand the various String handling functions defined
in the header file String.h.

Outcome:

1. After completion of this chapter the student will come to know the concepts of
pointers and strings.
2. The student will come to know the types of string handling functions along with
programming.
3. The student will come to know the pointers and multidimensional arrays.
4. The student will come to know the concept of dynamic memory allocation and their
types.

Points:
1. Pointers provide a powerful and flexible method of manipulating data in the
programs.
2. The advantages of pointers are
1. Things can be done better with pointers than without.
2. Things that can be done only with pointers.
3. C provides the power and flexibility in dealing with pointers which serve to set apart
from other programming languages.
4. Pointer is a variable which stores the address of another variable.
4901 4902 4903 4904 4905

25
alpha

fig: Variable is stored at a specific memory address.


5. Int alpha=25 the memory address of the variable alpha is a number and can be
treated like any other number in C.
4901 4902 4903 4904 4905
4905 25

Int *aptr; aptr alpha

aptr=&alpha; fig: aptr contains the address of variable alpha (aptr is a pointer to
alpha)

6. Pointer is a variable which stores the address of another variable. Pointers are much
used in C.
7. A pointer declaration takes the following form:
1. <data-type> *<pointer-name>;

Where data type is any valid data type and indicates the type of variable that
points to. The * (asterisk) is the indirection operatoe and it indicates that pointer-name is a
pointer variable to type data-type.

8. When a pointer is passed as an argument to a function the address of a variable is


passed to the function. The contents of that address can be accessed, either within
the function or within the calling function.
9. Any change that is made to the contents of the address will be reflected in both the
functions-calling and called functions.
10. In function definition an * (asterisk) must precede each one of the formal
arguments.
11. There is a strong relationship between pointers and arrays. Any operation can also be
done with pointers.
12. Even though pointers and arrays work alike and strongly related, they are not
synonymous. When an array is assigned with pointer, the address of first element of
the array is copied into the pointer.
13. Pointer is an address variable, having no initialized value by default. The address
stored in the pointer can be changed time to time in the program.
14. Array name is an address constant, initialized with the address of the first element
(base address )in the array. The address stored in array name cannot be changed in
the program.
15. #include<stdio.h>
int main()

int a[3] = { 12, 5 ,7}, b[3];


int *p ,*q;
p = a;
printf("%d %d\n", *p, *a);
q = p;
printf("%d %d",*p,*q);
b = a; /* error */
}
16. ‘void’ type pointer is a generic pointer, which can be assigned to any data type
without cast during compilation or runtime. ‘void’ pointer cannot be dereferenced
unless it is cast.
int main( ) {
void* p;
int x = 7;
float y = 23.5;
p = &x;
printf(“x contains : %d\n”, *( ( int *)p) );
p = &y;
printf(“y contains : %f\n”, *( ( float *)p) );
}
17. Function pointers are pointers, which point to the address of a function.
Declaration :
<return type> (* function_pointer)
(type1 arg1, type2 arg2, ……. );
18. Static memory allocation means allocating memory by compiler. When using
address operator, the address of a variable is assigned to a pointer. Ex : int a = 20 ;
int *p = &a ;
19. Dynamic memory allocation means allocating memory using functions like malloc()
and calloc(). The values returned by these functions are assigned to pointer variables
only after execution of these functions. Memory is assigned at run time.

20.
UNIT-V

Syllabus:

Derived types- strucrures-declaration, definition and initialization of structures, accessing


structures, nested structures, arrays of structures, structures and functions, pointers to structures,
self referential structures, unions, typedef, bit fields, enumerated types, c programming examples.

Objective:

 The student will be able to learn basic Derived types and the use of each derived
type and how to create the structures, unions and specify the different formats to
the structures and union.

 Students can learn different types of accessing structure variables, they can create
own data types using typedef.

 Students can create their own applications using structures and unions

 Explain the basic concepts of structures

 Process of structures

 Use typedef statement

 Explain the relationship between structures and pointers

 Relate structures to functions

 Explain the concept of unions.

Outcome:
 In this unit students will gain the basics of structures and union creations, accessing
the variables and usage of structure using other c concepts like arrays, pointers and
functions.

 The students will get the complete information about typedef, bit fields.

Points:

21. STRUCTURE: A structure is a collection of variables under a single name. These variables can be
of different types, and each has a name which is used to select it from the structure.
22. A structure is a convenient way of grouping several pieces of related information together.
struct mystruct
{
int numb;
char ch;
}
23. Structure has name mystruct and it contains two variables: an integer named numb and a
character named ch.
struct mystruct s1;
24. Accessing Member Variables
s1.numb=12;
s1.ch=’b’;
printf(“\ns1.numb=%d”,s1.numb);
printf(“\ns1.ch=%c”,s1.ch);
25. typedef can also be used with structures. The following creates a new type sb which is of type
struct chk and can be initialised as usual:
typedef struct chk
{
char name[50];
int magazinesize;
float calibre;
} sb;
ab arnies={"adam",30,7};
26. Unions:
A union is an object that can hold any one of a set of named members. The members of the
named set can be of any data type. Members are overlaid in storage.

27. The storage allocated for a union is the storage required for the largest member of the union,
plus any padding required for the union to end at a natural boundary of its strictest member.

union {
char n;
int age;
float weight;
} people;

people.n='g';
people.age=26;
people.weight=64;
28. PROCESSING A STRUCTURE:
The members of a structure are usually processed individually, as separate entities.
Therefore, we must be able to access the individual structure members. A structure member
can be accessed by writing variable.member name.This period (.) is an operator, it is a member
of the highest precedencegroup, and its associativity is left-to-right.
29. USER-DEFINED DATA TYPES (Typedef): The typedef feature allows users to define new data
types that are equivalent to existing data types. Once a user-defined data type has been
established, then new variables, arrays, structure and so on, can be declared in terms of this
new data type. In general terms, a new data type is defined as
typedef type new type;
30. STRUCTURES AND POINTERS: The beginning address of a structure can be accessed in the
same manner as any other address, through the use of the address (&) operator. Thus, if
variable represents a structure type variable, then & variable represents the starting address of
that variable. We can declare a pointer variable for a structure by writing
type *ptr;
31. PASSING STRUCTURES TO A FUNCTION: There are several different ways to pass structure–
type information to or from a function. Structure member can be transferred individually, or
entire structure can be transferred.
32. The individual structures members can be passed to a function as arguments in the function
call; and a single structure member can be returned via the return statement. To do so, each
structure member is treated the same way as ordinary, single- valued variables.

33. UNIONS:
Union, like structures, contain members whose individual data types may differ from one
another. However, the members that compose a union all share the same storage area within
the computer’s memory.
Whereas each member within a structure is assigned its own unique storage area. Thus, unions
are used to conserve memory. In general terms, the composition of a union may be defined as
union tag{
member1;
member 2;
---
member m
};

Multiple choice Questions

1. How will you free the allocated memory ? ans: B [ ]


A
remove(var-name); B.free(var-name);
.
C.delete(var-name); D.dalloc(var-name);

2. What is the similarity between a structure, union and enumeration? Ans: B [ ]


A.All of them let you define new values
B.All of them let you define new data types
C.All of them let you define new pointers
D.All of them let you define new structures

3. #include<stdio.h> [ ]

int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
A. 3, 2, 515 B. 515, 2, 3 C. 3, 2, 5 D. 515, 515, 4
Ans : A

4.what is the size of following structure. [ ]


Struct student

Int a;

Char ch[10];

Float f;

}s;

a. 2 bytes b. 10 bytes c. 16 Bytes d. 4 bytes

Ans. C

5. which stores the different type of elements in memory [ ]


a. array b. structure c. union d. b and c

Ans. D

6. which is the following syntax referred the structure returning. [ ]


a. struct abc a() b. int a() c. struct int a() 4. None

ans. A

7.which of the following keyword is used to create user defined datatype [ ]


a. typedef b. struct c. union d. none

Ans. A

8. which operator is used for accessing structure to pointer variables. [ ]


a. * b. -> c. dot d. none

Ans. b

9. Which of the following data structure shares same memory location [ ]


a. union b. structure c.array d. none
Ans. a

10. What is the self referential structure [ ]


a. structure object accessing structure variable

b. structure pointer variable referring the another structure object

c. a and b

d. none

Ans. B

True/False Questions and Answers

1. A union cannot be nested in a structure. (False)

2. Nested unions are allowed. (True)


3. Bit fields CANNOT be used in union.(False)
4. one of elements of a structure can be a pointer to the same structure.(True)
5. A structure can be nested inside another structure.(True)
6. size of union is size of the longest element in the union (True)
7. The elements of union are always accessed using & operator (False)
8. A pointer union CANNOT be created (False)
9. Is there easy way to print enumeration values symbolically? (False)
10. By default structure variable will be of auto storage class (True)

11. Is it necessary that the size of all elements in a union should be same? (False)

12. Can we have an array of bit fields? (False)

Match the following:


1. Structure a. create user defined data type
2. Union b. collection of different data types different
Memory allocation
3. Bit fields C. collection of different data types same
Memory allocation
4. Typedef d. accessing the structure variables
5. Dot Operator e interpreted as an integral type.
Answers: 1-b, 2-c, 3-e, 4-a, 5-d.

Assignment questions.
1. What is the main reason for using structure?
2. What special keyword is used in defining a structure?
3. Define structure tag and what is its purpose?
4. In what two ways can a structure variable be declared?
5. What rules govern the use of the period (.) operator?
6. What is meant by an array of structure?
7. What characteristic must a structure have in order to be initialized Within its declaration?
8. What is the meaning of the arrow operator?

UNIT-VI

Unit-VI

Input and output-concept of a file, Streams, standard input/output functions, formatted


input/output functions, text files and binary files, file input/output operations, file status
functions (error handling), c programming examples.

Objective:

 Use of File handling functions


 Input and output functions
 File modes
 File types
 Error handling
 Applications

Outcome:

 In this unit students will gain good knowledge on file handling techniques on text and
binary files.
 Students will learn about all file handling functions with programming examples

Points:

1. File Handling in C: We frequently use files for storing information which can be processed by
our programs. In order to store information permanently and retrieve it we need to use files.
2. Files are not only used for data. Our programs are also stored in files.
3. In order to use files we have to learn about File I/O i.e. how to write information to a file and
how to read information from a file.
4. Specifying the file you wish to use is referred to as opening the file.
5. When you open a file you must also specify what you wish to do with it i.e. Read from the file,
Write to the file, or both.
6. Because you may use a number of different files in your program, you must specify when
reading or writing which file you wish to use. This is accomplished by using a variable called a
file pointer.
7. Every file you open has its own file pointer variable. When you wish to write to a file you
specify the file by using its file pointer variable.
8. You declare these file pointer variables as follows:

FILE *fopen (), *fp1, *fp2, *fp3;

9. The variables fp1, fp2, fp3 are file pointers. You may use any name you wish.
10. The file <stdio.h> contains declarations for the Standard I/O library and should always be
included at the very beginning of C programs using files.
11. Constants such as FILE, EOF and NULL are defined in <stdio.h>.
12. You should note that a file pointer is simply a variable like an integer or character.
13. It does not point to a file or the data in a file. It is simply used to indicate which file your I/O
operation refers to.
14. A file number is used in the Basic language and a unit number is used in Fortran for the same
purpose.
15. The function fopen is one of the Standard Library functions and returns a file pointer which you
use to refer to the file you have opened e.g.

fp = fopen( “prog.c”, “r”) ;

16. The above statement opens a file called prog.c for reading and associates the file pointer fp
with the file.
17. When we wish to access this file for I/O, we use the file pointer variable fp to refer to it.

18. File I/O

The Standard I/O Library provides similar routines for file I/O to those used for standard I/O.

The routine getc(fp) is similar to getchar() and putc(c,fp) is similar to putchar(c).

Thus the statement

c = getc(fp);

reads the next character from the file referenced by fp and the statement
putc(c,fp); writes the character c into file referenced by fp.

Objective Questions

1. If a file is opened in r+ mode then [ ]


a. reading is possible
b. writing is possible
c. it will be created if it does not exist
d. appending is possible

2. If a file is opened in w+ mode then [ ]


a. reading is possible
b. writing is possible
c. it will be created if it does not exist
d. appending is possible
3. If a file is opened in r mode then [ ]
a. reading is possible
b. writing is possible
c. it will be created if it does not exist
d. appending is possible
4. If a file is opened in a mode then [ ]
a. reading is possible
b. writing is possible
c. it will be created if it does not exist
d. appending is possible
5. ftell () [ ]
a. is a function
b. gives the current file position indicator
c. can be used to find the size of a file
d. none of the above
6. The fseek function [ ]
a. needs 2 arguments
b. makes rewind function unnecessary
c. takes 3 arguments
d. none of the above
7. rewind function takes __ number of arguments [ ]
a. 1
b. 2
c. 3
d. 0
8. fseek(fptr,0,0) is equivalent to [ ]
a. ftell
b. rewind
c. a & b
d. none of the above
9. ferror function is used to find _________________ errors. [ ]
a) logical
b) file opening
c)data
d)all the above
10. The contents of the file are lost if it is opened in _______ mode. [ ]
a) a b)w c) w+ d)a+
11. The contents of the file are safe if it is opened in _________ mode. [ ]
a) a b)r c) a+b d)all the above
12. The valid binary modes of operation are [ ]
a) ab b)rb+ c) wb+ d)ab+
13. rewind function is used to [ ]
a) Reset the file pointer
b) Point it to the end of the file
c) Stay at current position
d) none of the above
14. feof function checks for [ ]
a) file opening error
b) data error
c) end of file
d) file closing error
15. The value returned by fopen() function when the file is not opened [ ]
a) 0 b) garbage value c) NULL d) none of the above
16. The fcloseall() function performs [ ]
a) Closing of all the files
b) Closes all the files that are opened by that program
c) Closes only specified files
d) None of the above
17. The function that is not used for random access to files is [ ]
a) rewind b)ftell c)fseek d)fprintf

Assignment Questions:

1. Write a program that appends one file at the end of another.


2. Write a program that reads a file containing integers and appends at its end the sum of all
the integers.
3. Write a program to copy the contents of one file into another.
4. Explain the entire file handling functions.
5. Write a c program for error handling.
6. Explain the different modes that can be provided as a parameter to the fopen() function.
7. Explain different modes of opening a file.
8. Explain the I/O operations on files.
9. Distinguish between getc and getchar ,Rewind and fseek.
10. Explain the way of defining, opening and closing a file.

True/False Questions:

1.File is a variable. [True/False]


2.A file pointer is a pointer to FILE data type. [True/False]
3.The structure FILE is defined in stdio.h header file. [True/False]
4.The action of disconnecting a file from a program is obtained by the function fclose().
[True/False]
5.If a file is opened in “r+” mode ,then reading and writing are possible. [True/False]
6.The fseek() function needs 3 arguments: [True/False]
7.The rewind() function sets the position indicator to the beginning of the file. [True/False]
8.argc contains number of arguments. [True/False]
9.argv[0] contains name of the program. [True/False]
10.To write strings in files we use fputs() function. [True/False]

Match the following:

1. argc a.name of the program.


2. argv [0] b. number of arguments.
3. fseek () c. needs 3 arguments.
4.”r+” d.Only reading possible.
5. r e. reading and writing are possible.
6. A file pointer f. data type and a region of storage.
7. File g. pointer to FILE data type.
8. fclose() h.sets the position indicator to the beginning of the file.
9. rewind() i.Disconnects a file from a program.
10.EOF value j.-1
UNIT-VII

SORTING AND SEARCHING TECHNIQUES

Objective:

 The student will be able to learn the related activities of sorting and searching
which are very important to many computer applications.
 The student will be able to understand the concept of searching and their types
along with algorithms.
 And also student will be able to know about different types of sorting techniques
along with algorithms.

Outcome:

 After completion of this chapter the student will come to know the concept of
searching and types of searching along with the algorithms and programs.
 After completion of this chapter the student will come to know the concept of
sorting and different types of sorting techniques along with the algorithms and
programs.
 The students will come to know the time complexities of various searching and
sorting techniques.

Syllabus: Searching and sorting-selection sort, bubble sort, insertion sort, merge sort.
Searching-Linear search and binary search methods.

Points:

1. Information retrieval is one of the key features expected out of a


computerized system.
2. The term sorting means bringing some orderliness in the data which are
otherwise not ordered.
3. Many sorting methods are advocated. Each method has its own advantages
and disadvantages.
4. The efficiency of the type of sorting could be measured in terms of memory
usage, time taken to sort, CPU usage, etc.
5. The efficiency of the sorting algorithms is measured using the O-notation
(called Big O). The letter O stands for “order of”.
6. O-notation analysis:
a. O (n): This means that an algorithm will run for a constant amount of time
irrespective of the size of the input it receives. Whatever the size of the
input you give the algorithm will run for the same amount of time to give
the result.
b. O(n): This means that the amount of time the algorithm will take to
complete is directly proportional to the size of the input. If the size of the
input is large the time taken to complete the process will increase.
c. O(n2): This means that the amount of time the algorithm will take to
complete increases by n2 if you increase the size of the input by n.
d. O(2n): This means that the amount of time the algorithm will take to
complete doubles each time you increase the input.
7. The algorithms which we will discuss ,are:
 O(n)--bubble sort, insertion sort and selection sort.
 O(n log n)—merge sort and quick sort.
8. Bubble sort: The familiar sorting procedure is the bubble sort or exchange
sort. The simplest and oldest sorting technique is the bubble sort. However
this is the most inefficient algorithm.
9. The method takes two elements at a time. It compares these two elements.
If the first element is less than the second element, they are left undisturbed.
10. If the first element is greater than the second element then they are
swapped.
11. The procedure continues with the next two elements goes and ends when all
elements are sorted.
12. Selection sort: one of the easiest ways to sort a list is by Selection sort.
Beginning with the first element in the array a search is performed to locate
the smallest element.
13. When this is found it is exchanged with the first element. This interchange
places the smallest element in the first position of the array.
14. The following is the selection sort algorithm: selection sort(a,n)
a. N: size of the array, a
b. Min: location (index) of smallest element come across in a particular pass,
c. Pass index and position of the first element in the array, a.
d. I: index to traverse the array for a pass.
1. Repeat steps 2 and 3 , for pass= 0,1,2,…..n-2
2. Initialize min index with pass: min=pass.
a. Repeat step b, for i=pass+1,pass+2………..n-1
b. If(a[i]<a[min],then min=i

{end of for loop}

3. If min!=pass then swap a[min] and a[pass] [end of outer for loop]
4. Exit
15. Insertion sort: insertion sort works very fast on small size arrays. The
insertion sort procedure scans array, a from a[0] to a[n-1], inserting each
element a[j] into its proper position in the previously sorted sub array
a[0],a[1],……….a[n-1].
16. Merge sort: the merge sort technique sorts a given set of values by
combining two sorted arrays into one larger sorted array.
17. The total number of comparisons in the merge sort technique to sort n data-
items of an array is log n.
18. So the complexity of merge sort O(n log n).
19. Quick sort: sorting method that performs very well on larger arrays and lists.
At each step in the method the goal is to place a particular element in its final
position within the array.
20. In so doing the elements which precede the element are smaller while the
elements that follow it are larger. This technique essentially partitions the
array into two sub-arrays.
21. The same process can then be applied to these sub-arrays and repeated until
all the elements are placed in their final positions.
22. The quick sort method of sorting is also called as partition exchange
procedure.
23. A general algorithm based on a recursive method follows:
a. Partition the current array into two sub-arrays.
b. Invoke quick sort to sort the left sub-array
c. Invoke quick sort to sort the right sub-array.
24. Linear Search: the simplest form of a search is the linear (sequential) search.
This technique is meant for searching a particular item in an unsorted data in
a sequential manner until the desired data item is found.
25. Linear search is easy to write and efficient for short lists, but a failure for long
ones.
26. Binary search: binary search is a simple methods of accessing a particular
item with a certain key value resembles the search for a name.
27. The approximate middle item of the data set is located, and its key value is
examined. If its value is too high, then the key of middle element of the first
half of the set is examined and procedure is repeated on the first half until
the required item is found.
28. If the value is too low, then the key of the middle element of the second half
of the dataset is tried and the procedure is repeated on the second half. This
process continuous until the desired key is found or search interval becomes
empty.
Objective questions:

1) Merge sort is an application of [ ]

a) Greedy method b) Divide & conquer c) a & b d) none.

2) Quick sort is an application of [ ]

a) Greedy method b) Divide & conquer c) a & b d) none.

3) If the elements are sorted using linked list, which Sorting is more efficient [ ]

a) Selection sort b) insertion sort c) bubble sort d) none.

4) The worst case complexity of linear search is [ ]

a) O (n) b) O(log n) c) O (1) d) O(n log n).

5) The average complexity of binary search is [ ]

a) O (n) b) O (log n) c) O (n2) d) O (n log n).

6) The average case complexity of quick sort is [ ]

a) O (n2) b) O (log n) c) O (n) d) O (n log n).

7) The worst case complexity of bubble sort is [ ]

a) O (n log n) b) O (n2) c) O (log n) d) O (n).

8) The worst case complexity of quick sort is [ ]

a) O (n2) b) O (n log n) c) O (n) d) O (log n)

9) The average case complexity of binary search is [ ]

a) O (n2) b) O (n log n) c) O (n) d) O (log n).

10) For the list of only few elements which search Techniques is efficient [ ]

a) Binary search b) linear search c) Fibonacci search d) none.

11) For the list of large number of elements which search technique is efficient [ ]

a) binary search b) linear search c) Fibonacci search d) none.


12) Quick sort is same as selection sort when [ ]

a) Elements are already sorted b) Elements are reverse order

c) Elements are randomly ordered d) None.

13) Which of the sorting techniques is more efficient [ ]

a) Selection sort b) bubble sort c) insertion sort d) quick sort.

14) Data movement is minimized in [ ]

a) Insertion sort b) selection sort c) Both a&b d) none.

15) For randomly ordered elements, which algorithm Works better [ ]

a) Selection sort b) quick sort c) insertion sort d) bubble sort.

16) The binary search is effective only when the elements are in [ ]

a) Ascending order b) descending order c) a & b d) jumbled order.

Answers: 1. B 2. B 3. B 4. c

Fill in the blanks:

17) The process of arranging records in ordered manner is ____________________.

18) The process of finding a particular element in a record is _____________________.

19) The records must be sorted in _______________.

20) The list is divided into sorted in _____________.

21) The formula for pivot (key) in quick sort is _______________.

Answers: 17. Sorting 18. Searching 19. Binary search 20. Merge sort 21. (Left+right)/2

True or false:

22) Quick sort efficiency relies on the pivot (false).

23) Sorting and searching are always tightly iterated (True).

24) Bubble sort even though simple is very efficient (False).


25) Sorting is mandatory for linear searching (False).

26) Binary search can be done on an ordered list only. (True)

Match the following:

1. Linear search Time complexity a. O(n log n)


2. Worst case time complexity of linear search b. O(log n)
3. Binary search Time complexity c. O(n)
4. Bubble sort time complexity d.O(log2n)
5. Quick sort time complexity e. O(n2)

Lab program to be done in class:

1. Example Program on Selection sort.

2. Example Program on Bubble sort.

3. Example Program on Insertion sort.

4. Example Program on Quick sort.

5. Example Program on Merge sort.

6. Example Program on Linear search.

7. Example Program on Binary search.

Assignment questions:

1. Write a ‘c’ program to sort the given integers using partition Exchange sort?

2. Derive the Time complexity of Partition Exchange sort?

3. Write a ‘c’ program to search for a given element in the integer array using binary search.

4. Write a ‘c’ program to sort the given integers using Merge sort.

5. Explain Quick sort by taking a suitable example?

6. Explain Quick sort algorithm with a suitable example.

7. Analyze the worst-case performance of Quick sort and compare with Selection sort.

8. Explain linear search method with an example.


9. Explain Binary search method with an example.

10. Write a recursive binary search function to find the location of the given element in the
array.

11. Formulate non-recursive algorithm for binary search with its timing analysis.

12. What is the difference between sorting and searching?

13. Derive the time complexity of Binary search?

14. Suppose that the list contains the integers 1, 2, 8 in this order. Trace out through steps of
binary search to determine what comparisons of key are done in searching.

15. Compare the different sorting algorithms with respect to the time and space complexities.

16. Explain the time complexity of any two sorting algorithms.

17. Write an algorithm or ‘c’ program for insertion sort.

18. Describe Quick sort algorithm for the input:

20, 5, 100, 15, 30,10,80,25.

19. Explain the algorithm for exchange sort with a suitable example.

20. What are the best and worst case time complexities for quick sort?

21. Compare sort and exchange sort?

22. Write an algorithm for two way merge sort. Analyze the Time complexity.

23. Explain linear search procedure with suitable example?

Tutorial Problems:

1. Write a ‘c’ program to sort the given list of integers in ascending order by using Bubble sort.

2. Write a ‘c’ program to sort the given list of integers in ascending order by using Selection
sort.
3. Write a ‘c’ program to sort the given list of integers in ascending order by using Quick sort.

4. Write a ‘c’ program to sort the given list of integers in ascending order by using Merge sort.

5. Write a ‘c’ program to sort the given list of integers in ascending order by using Insertion
sort.

6. Write a ‘c’ program that use both recursive and non-recursive function to perform the
following searching operations for a key in a given list.

a) Linear search b) Binary search

Questions from previous exam papers:

1. Explain bubble sort with the algorithm or a C program.

2. Illustrate the results of bubble sort for each pass, for the following initial array of elements.
68 67 99 33 122 200

3. Write a C program or algorithm to sort an array of integers in ascending order using insertion
sort.

4. Illustrate the results of insertion sort for each pass, for the following initial array of elements.
68 57 99 33 122 200

5. Write a C program or algorithm to sort an array of integers in ascending order using selection
sort.

6. Illustrate the results of insertion sort for each pass, for the following initial array of elements.
23 78 45 8 32 56

7. Write an algorithm or program for binary search to find a given integer in an array of integers.

8. Illustrate the results of bubble sort for each pass, for the following initial array of elements
44 36 57 19 25 89 28

9. Write an algorithm or program for linear search to find a given integer in an array of integers.

UNIT - VIII

Objective:

 The student will be able to learn basics of various data structures.

Outcome:
 The student can create linear data structures using linear lists and linked lists.
The student can convert infix expression to prefix and postfix expressions.

Syllabus: Data structures – Introduction to DS, Abstract data types, linear list, Singly Linked List
– implementation, insertion, deletion and searching operations on linked list, Stacks –
operations, Array and linked representation of stacks, stack application infix to post fix
conversion, postfix expression evaluation, recursion, implementation, Queues – operations,
array and linked representations.

Points:

Abstract Data Type: The Abstract Data Type is a specification for the type of values that a
data type can store and the operation that can be performed on those values.

Data Structure: A data structure is a group of memory locations used to represent the
information used by the algorithm.

Types of Data Structure: Two types 1. Linear Data Structure and 2. Non-linear Data Structure

- Linear Data Structure: Stacks, Queues and Linked Lists.


- Non-Linear Data Structures: Trees, Graphs.

Stack: A stack is a linear structure containing homogeneous elements in which all insertions and
deletions are made at one end, called top of the stack. A stack is a Last-in-first-out (LIFO) list.

Operations of Stack:

1. Push: Insertion of an element at the top of the stack.


2. Pop: Deletion of an element from the top of the stack.
3. Peek: Returns top most element of the stack.
4. Stackful: Returns true if stack is filled with the data otherwise returns false.
5. Stackemplty: Returns true if stack is empty otherwise returns false.
6. Display: Display the contents of stack.

Applications of Stack: 1. Used in conversion of infix expression into prefix and postfix.

2. Used in evaluation of postfix expression.

3. Used in evaluation of recursive programs.

Queue: A queue is an ordered list in which all insertions take place at one end, the rear,
whereas all deletions take place at the other end, the front. A queue is a first-in-first-out(FIFO)
linear data structure.
Operations of Queues:

1. Insert: Inserts an element at rear side of the Queue.


2. Remove: Deletes an element from the front side of the Queue.
3. Display: Displays the contents of the Queue.
4. Qfull: Returns true if Queue is filled with the data otherwise returns false.
5. Qempty: Returns true if Queue is empty otherwise returns false.

Types of Queues: 1. Circular Queues

2. Linked Queues

3. DeQueues

Linked Lists: A linked list is a list or chain of items where each item points to the next one in the
list. Each item in a linked list is called a node.

Types of Linked Lists: 1. Singly Linked Lists

2. Doubly Linked Lists

3. Circularly Linked Lists

Operations of Linked Lists:

1. Create: creates a new singly linked list


2. Insert: inserts a new node at the starting, ending or at given position.
3. Delete: deletes a node after the specified position or data.
4. Display: displays the contents of existing singly linked list.
5. Search: Searches for a given data, if exists return true otherwise
returns false.

Application of Linked Lists:


1. Polynomial manipulation.
2. Linked dictionary
3. Multiple-precision arithmetic
4. Representation of sparse matrices.
5.
Multiple choice questions:
1. Queue follows algorithm
A) LIFO B) FIFO C) FILO D) LILO
2. In the queue the items are inserted from the ------------ end.
A) Front B) top C) rear D) first
3. If top is initialized to -1 and the position of top is at 4, number of elements in stack is
A) 4 B) 5 C) 3 D) 0
4. If tope is initialized to 0 and we want to add 5 to the stack, the first step is
A) POP B) PUSH 5 C)increment TOP D) decrement TOP
5. In implementation of stack using linked list, malloc() function is used in
A) Push or pop operations B) only pop operation
B) Push and pop operations D) only push operation
6. Linked list mostly used in implementation of stack.
A) adjacent B) singly C) circular D) doubly
7. The best data structure to check whether an arithmetic expression has balanced
parentheses is
A) A queue B) a stack C) tree D) list
8. Depth of recursion is given by
A) Number of times recursive function is called
B) Total number of variables stored on stack
C) Position of stack pointer
D) Exit condition of recursive function
9. Inserting an element in a linked list at the end is called as
A) inserting B) appending C) pre-pending D) adding
10. If head node contains NULL, then number of nodes in singly linked list is
A) 1 B) 3 C) 2 D) 0

Fill in the blanks:


1. Deletion of a node from a singly linked list involves the alteration of -------------------.
2. Head node is modified if the ----------------- node is to be deleted.
3. --------------- field of the first node contains address of second node in a singly linked list.
4. --------------- contains address of first node in a singly linked list.
5. Program A calls subprogram B, internally ------------- action will take place.
6. ---------------- linked list Is mostly used in implementation of queue.
7. In ----------------- notation the operator follows the two operands
8. To allocate multiple blocks of memory ------------- function is used
9. Generally, ----------------- condition will not arise in implementation of stack using linked
list.
10. Deletions and insertions in a stack are carried from ------------.
True/False questions:

1. Stacks follows RANDOM algorithm. [true/false]


2. Stack data structure is needed to convert infix notation to postfix notation. [true/false]
3. In singly linked list, head node is modified if the last node is deleted. [true/false]
4. Address field of the first node contains address of second node in a singly linked list.
[true/false]
5. Searching an element in singly linked list is sequential. [true/false]
6. In a queue, the items are deleted from rear end. [true/false]
7. In a dequeue the insertions and deletions happen from both the ends. [true/false]
8. In a circular queue insertion takes at both ends and deletion occurs at one end.
[true/false]
9. Singly linked list is mostly used in implementation of queue. [true/false]
10. Stacks and Queues are non-linear data structures. [true/false]

Match the following:

1. Stack (a) pointers


2. Queue (b) dynamic memory allocation
3. Linked list contains (c) tree
4. malloc() (d) FIFO
5. non-linear Data structure (e) LIFO

Answers: 1-e, 2-d, 3- a, 4-b, 5-c.

Assignment:

1. Write a C program to implement various queue operations.


2. Write a C program to insert an element after the specified position in a singly linked list.
3. Write a C program to implement various queue operations using singly linked list.
Tutorial problems:

1. What is a data structure? Explain about various types of data structures with examples.
2. Write and explain stack ADT.
3. Explain various queues operations with suitable examples in detail.

Questions from previous exam papers:


1. What are the operations of linear list? Differentiate between using arrays and linked list
for implementation of linear lists.
2. Write structure for implementing linked list of integers. Write C function for insertion
operation in linked list.
3. Write an algorithm for evaluating postfix expression. Demonstrate your algorithm with
stack contents for each operation using the postfix expression 235+*.
4. Write structure for linked list implementation of stack and a function for pop operation.
5. Write an algorithm that convert an infix expression into postfix. Demonstrate your
algorithm for the expression a+b*c.
6. Write C-structures for implementing queues using linked lists. Write C structure for
queue operation.
7. Using recursion function for factorial, explain the execution of the function call factorial
(5) using stack.
8. Write C-structure for implementing stack using an array. Using this structure write
function for push and pop operation.

Vous aimerez peut-être aussi