O LEVEL THEORY MOST
IMPORTANT QUESTION WITH
SOLUTION
Algorithms
In programming, algorithm is a set of well defined instructions in sequence to solve the problem.
Write an algorithm to add two numbers entered by user.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Flow-charts
Flowchart is a diagrammatic representation of an algorithm. Flowchart are very helpful
in writing program and explaining program to others.
Symbols Used In Flowchart
Different symbols are used for different states in flowchart, For example: Input/Output
and decision making has different symbols. The table below describes all the symbols
that are used in making flowchart
Symbol Purpose Description
Flow line Used to indicate the flow of logic by connecting symbols.
Terminal(Stop/Start) Used to represent start and end of flowchart.
Input/Output Used for input and output operation.
Processing Used for airthmetic operations and data-manipulations.
Used to represent the operation in which there are two
Desicion alternatives, true and false.
On-page Connector Used to join different flowline
Off-page Connector Used to connect flowchart portion on different page.
Predefined Used to represent a group of statements performing one
Process/Function processing task.
Draw a flowchart to add two numbers entered by user.
Draw flowchart to find the largest among three different numbers entered by user.
Draw a flowchart to find the Fibonacci series till term≤1000
Programming Languages,
A programming language is an artificial language that can be used to write programs which control the behavior
of a machine, particularly a computer.
Programming languages are defined by rules which describe their structure and meaning respectively.
Many programming languages have some form of written specification of their syntax.
There are two level of language.
1. High level programming language
2. Low-level programming language
High-level programming language
These languages are normal, English like. Easy to understand statements to pass instruction to the computer.
The languages are problem oriented. It offers:
● Readability ● Easy Debugging ● Portability ● Easy software Development
Example: – BASIC, COBOL, FORTRAN, PASCAL, and C.
Low-level programming language
Low-level programming languages are sometimes divided into two categories:
1. Machine Language
This is the only language understood directly by the computer. It the language of 0’ and 1’ i.e . Binary number.
It is machine dependent. It is difficult to learn and write program in.
2. Assembly Language
it is uses only letters and symbols. Programming is simpler and less time consuming than machine language
programming. It is easy to locate and correct errors in Assembly language. It is also machine dependent.
Programmer must have knowledge of the machine on which the program will run. An assembler is a program
that translates an assembly language program into a
machine language program.
Compiler
The process of translating source code written in high level to low level machine code is called
as Compilation. The compilation is done by a special software known as compiler.
The compiler checks source code for any syntactical or structural errors and generates object code with
extension .
Testing and Debugging
Documentation
Any written text, illustrations or video that describe a software or program to its users is called program or software
document.. User can be anyone from a programmer,
programmer, system analyst and administrator to end user. At various stages
of development multiple documents may be created for different users. In fact, software documentation is a critical
process in the overall software development process.
Advantages of Documentation
Keeps track of all parts of a software or program
Maintenance is easier
Programmers other than the developer can understand all aspects of software
Improves overall qualityy of the software
Assists in user training
Ensures knowledge de-centralization,
centralization, cutting costs and effort if people leave the system abruptly
Character set: The character set is the fundamental raw material of any language and they are used to
represent information.
formation. Like natural languages, computer language will also have well defined character
set, which is useful to build the p
programs.
Built-in Data Types vs Derived Data Types
In C programming, data types are declarations for variables. This determines the type and size of data
associated with variables.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division
etc on numerical values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)
Simple assignment statement and equality operator
The differences can be shown in tabular form as follows:
= ==
It is an assignment operator. It is a relational or comparison operator.
It is used for assigning the value to a It is used for comparing two values. It returns 1 if both the values are
variable. equal otherwise returns 0.
Constant term cannot be placed on left
hand side. Constant term can be placed in the left hand side.
Example: 1=x; is invalid. Example: 1==1 is valid and returns 1.
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1;
if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true
or false. Logical operators are commonly used in decision making in C programming.
Operator Meaning Example
Logical AND. True only if all operands If c = 5 and d = 2 then, expression ((c==5) &&
&& are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
|| operand is true (d>5)) equals to 1.
Logical NOT. True only if the operand is
! 0 If c = 5 then, expression !(c==5) equals to 0.
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Arrays
Advantages-
collection of similar types of data.
if we want to store the marks of all students it will easy to store in array
array otherwise we have to store marks in different
different location, which is not easy to memorise.
we have to only remember the first index of array.
used to implement other data structure like linked lists,stack,queue, trees, graph etc.
2 Dimensional arrayy is used to represent a matrix.
Disadvantages
Time complexity increase in insertion and deletion operation.
Wastage of memory because arrays are fixed in size.
If there is enough space present in the memory bt not in contiguous form , in this case you wil
willl not able initialize your
array.
it is not possible to increase the size of the array, once you had declared the array
Function
A function is a block of code that performs a specific task.
task
The advantages of using functions are:
Avoid repetition of codes.
Increases program readability.
Divide
vide a complex problem into simpler ones.
Reduces chances of error.
Modifying a program becomes easier by using function.
Local Variables
Variables that are declared inside a function or block are called local variables. They
can be used only by statements that are inside that function or block of code. Local
variables are not known to functions outside their own. The following example shows
how local variables are used. Here all the variables a, b, and c are local to main()
function.
Global Variables
Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of yyour
our program and they can be
accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration. The following
program show how global variables are used in a program.
A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several
sever source files.. There are two types of header files:
the files that the programmer writes and the files that come with your compiler.
Structures and Unions
Pointers
C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate
memory dynamically i.e. at run time.
Benefits(use) of pointers in c:
Pointers provide direct access to memory
Pointers provide a way to return more than one value to the functions
Reduces the storage space and complexity of the program
Reduces the execution time of the program
Provides an alternate way to access array elements
Pointers can be used to pass information back and forth between the calling function and called function.
Pointers allows us to perform dynamic memory allocation and deallocation.
Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
Pointers allows us to resize the dynamically allocated memory block.
Addresses of objects can be extracted using pointers
Drawbacks of pointers in c:
Uninitialized pointers might cause segmentation fault.
Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak.
Pointers are slower than normal variables.
If pointers are updated with incorrect values, it might lead to memory corruption.
Basically, pointer bugs are difficult to debug. Its programmers responsibility to use pointers effectively and correctly.
Dynamic memory allocation Static Memory Allocation
Study from here :- [Link]
Macros in C
A macro is a fragment of code which has been given a name. Whenever the name is
used, it is replaced by the contents of the macro. There are two kinds of macros.
They differ mostly in what they look like when they are used. Object-like macros
resemble data objects when used, function-like macros resemble function calls.
Object-like Macros
An object-like macro is a simple identifier which will be replaced by a code fragment.
It is called object-like because it looks like a data object in code that uses it. They are
most commonly used to give symbolic names to numeric constants.
#define BUFFER_SIZE 1024
Function-like Macros
You can also define macros whose use looks like a function call. These are
called function-like macros. To define a function-like macro, you use the same
‘#define’ directive, but you put a pair of parentheses immediately after the macro
name.
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
x = min(a, b); → x = ((a) < (b) ? (a) : (b));
y = min(1, 2); → y = ((1) < (2) ? (1) : (2));
z = min(a + 28, *p); → z = ((a + 28) < (*p) ? (a +
28) : (*p));