CCP-UNIT1
CCP-UNIT1
m
UNIT-1
co
ce.
Module – 1: (Introduction to Programming)
Idea of Algorithm:
Representation of Algorithm
efe
Programming Basics:
tur
Structure of C program
Writing and executing the first C program.
Syntax and logical errors in compilation, Object and executable code.
ak
m
Q2.What is the working of Loader System Utility?
Ans: This is an OS utility that copies programs from a storage device to RAM(Main Memory) , where
they can be executed. Types of loaders are like Absolute loader, bootstrap loader
co
Q3.What is the difference between low level language and high level language?
ce.
Ans: Low level language is machine language in form of 0 and 1 or Assembly language in Mnemonics
(e.g : ADD , SUB ,DIV, STR, LOAD)which can be understand by computer hardware. It is very
difficult to understand by the user. Each type of CPU has its own machine language depending
on the hardware.
ren
High level language is those which are used by programmers to create computer instructions, coding
the programs and to develop the software’s. These are closer to human languages and are combination of
natural language like English & some mathematical expressions and Language Syntax and rules. These are
easier to understand, write and maintain as compared to low level languages. Example: Ada, Pascal,
efe
Ans: Internal commands are stored in the system memory. These are the command frequently used by
user. Example: cd, copy, md , rd, Time, Ver. , Del , Type, Echo, Rename, cls, Path, Erase etc.
External commands are those that require large requirements and/or not commonly used.
Many of the external commands are located in the Windows\System 32 directories.
ak
Example: Append, Attrib, Chkdsk, Deltree, DOSshell, MSbackup, Ping, Net, Telnet, Xcopy, Defrag,
Doskey, Format, Fast help etc.
Ans: ALU performs two types of operations—Arithmetic & logic. Arithmetic operations are like addition,
subtraction, division trigonometrically functions (Sine, Cosine, etc.), square roots.
Logical operations involve Boolean Logic like: AND, OR, NOT.
Q6. What is ann Operating System(OS) in computer terminology ? Mention any five types of
Operating System.
Ans: (i) An operating system is a system software which works as an interface between the user and hardware
m
of computer system.
(ii) OS is responsible for managing and coordinating various activities and resource sharing within the given
co
limit of computer. Example : Microsoft windows, Linux, Android, Mac OS, Symbian etc.
Various types of OS are as follows :
(i) Real time OS (ii) Multi user and single user OS (iii) Multitasking OS (iv) Multi
ce.
Programming OS (v) Distributed OS (vi) Network OS
(vii) Embedded OS.
Q7. What are language translators? Give examples.
ren
Ans: Translator is a program that converts a code written in one language to another language.
Example : Compiler , Assembler , Interpreter. It also checks and points the errors in a
program. Compiler resides in the main memory during compilation of a code. It has a separate
editor in which user types the program.
efe
1. Entire program is checked for errors and Errors are checked statement by statement and
statement in a one go, lists all the error interpreter converts into machine code and
statements, converts into machine code. executes it.
2. Does not stop when an erroneous statements is Stops when an erroneous statements is
encountered. encountered
ak
3. Separate command is issued to execute Executes the statement after converting a non
Statement. erroneous data of program.
4. Generally a separate editor is used to enter the A built in editor is available.
program.
5. Compiler resides in the main memory Interpreter should be raised in main memory
during the process of compilation only. throughout the process of checking for errors.
Hence storage is not wasted.
Ques 9. Define Software. Differentiate between system software and application software.
Ans : Software is basically a collection of programs , where as programs are set of instructions which are
stored electronically in computer system. Example : Adobe photo shop, railway reservation software, anti virus
software, gaming softwares, operating system.
m
interact with computer at very basic problems
level.
2. Mostly written in assembly language, C These are written in C , C++, Java, Dot Net, Android
3. Some utility programs like device These are portable to any type of platform/Operating
co
drivers are also system softwares. These system
are generally hardware specific.
4. Example : Operating system, Device MS Office, Adobe photo shop, Gaming softwares,
drivers, compilers, linker , loader. Android apps, reservation sites.
ce.
Ques 10. What are the different data types?
Ans: In computer programming, a data type is a classification identifying one of various types of data, such as
ren
floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be
done on that type; and the way the values of that type are stored.
(a) Integer : In more common parlance, whole number; a number that has no fractional part.
e.g : 23, 678, -18 etc.
efe
(b) Floating-point : A number with a decimal point. For example, 3 is an integer, but 3.5 is a floating-
point number. Another representation of floating point constant is 3.5 x 10-3 . Here 3.5 is a mantissa
and 10-3 is exponent.
tur
Ans : (i) Speed : Computers can perform millions of operations per second. Speed is given in nanoseconds
ak
(iii) Versatile : Computer systems are flexible towards the changing technology, hardware and
softwares. They also can be used as personal computers, for scientific purpose, fr weather
forecasting , for military operations and business analysis too.
(iv) Memory : Like human beings computers can also store large set of information and data
Fetched from real world . Memory is used to store information. Primary and secondary
memory are designed inside the system.
m
Long Questions and Answers
co
Ques 12. What are tokens in C programming language? Describe with examples.Ans: In C programming
tokens are defined as the smallest unit of a program which is indivisible. Program is combination of these C
ce.
tokens. There are following six types of token categories :
(i) Keywords: These are set of fixed or reserved words that can not be used as an identifier or variable.
ren
E.g: printf, scanf, gets, stdio, clrscr, int , float , char, signed, break, for , while etc.
(ii) Identifiers: These are used to identify the data and other objects in a program. These are names
given to variables, array, functions consisting of alphabets, numerals and underscore.
Identifiers never start with a number. E.g : a , ab, akash_cse, ab123.
Constants : Those data objects which are fixed and values can’t be changed.
efe
(iii)
E.g : Integer constant : 1 , 34, 257, -675 etc.
Floating constant : 0.02, 3.145159, 2.14E-3 etc.
Char constant : A single character inclosed in a single quotes. E.g : ‘a’, ‘&’, ‘x’ etc. In
tur
computer characters are stored using machine’s character sets in ASCII form.
(iv) String constants : Sequence of characters stored within double quotes. E.g : “abc”, “ xy67”,
“computer” etc.
(v) Special characters : @ , $ , &, !, }, % etc.
ak
(vi) Operators : this is a symbol that specifies the mathematical, logical, or relational operation to be
performed .e.g : = , -, &&, /, =, = = etc.
Ques 13. What is an algorithm? What is its purpose? Mention the characteristics of algorithm. Write an
algorithm to print the first 10 natural numbers. Also draw its flow chart.
Ans : Algorithm is a formally defined procedure for performing calculation. If a procedure is formally defined
then it must be represented in a formal way i.e programming language. The algorithm gives step by step
solution to reach some goal of a problem or to find a solution of existing problem. Purpose of algorithms is to
implement re-usability in software applications. Once we have designed the overview of solution to any
problem in algorithmic form it can be coded in any language like C , C++,Java etc.
m
Psuedo Code : Algorithms can be represented in the fofm of pseudo code and flow chart both. Psuedo code is
the way to write any algorithm using some formal notations of mathematics, programming syntax and English
words step by step.
co
Characteristics of algorithm are as follows:
ce.
(iii) Algorithm must be written in finite number of steps.
(iv) Algorithm must be unambiguous. It should not represent more than one meaning or for same input
there should not be different output.
ren
(v) Algorithm must be effective and should be terminated easily in minimum time .
(i) Sequence : Each step of algorithm is executes in specific order one after another.
efe
(ii) Decision : When the output of a process depends on certain decision criterion, then Decision
statements are used. E.g : if x = y, then print “ EQUAL”. Decision statements may either evaluate to
TRUE or FALSE.
(iii) Repetition: This involves execution of one or more steps for a number of times repeatedly.
tur
Programming constructs such as while, do-while and for loops are used.
ak
Step 3 : Print I
Step 4 : Set I = I + 1
[ End of loop]
Step 5 : End
m
co
ce.
ren
efe
tur
ak
m
co
ce.
ren
efe
tur
Ques 15.Draw the block diagram of computer systems .What are major functions of a computer. Also
ak
m
co
ce.
ren
Major functions of a computer are as following :
efe
(e) Controlling and coordinating all the operations inside the computer.
(i) Input : This is the process of entering data and instructions /programs into the computer system
ak
with the help of input devices (e.g : Keyboard, Mouse, Microphone etc.)Input device converts the
input data into binary code ( 0 or 1) for further processing.
(ii) Storage : Storage is the process of saving and storing the data and information in computer
memory. It also stores intermediate results.
(a) Primary Storage : This is aslo called main memory, which is directly accessible by the CPU
at very high speeds.It is used to store the data and parts of programs, the intermediate resultsof
processing and current job results.
Primary memeory is volatile in nature i.e. as sson as computer is switched off, data is
erased/lost from the memory. It is also expensive E.g : RAM(random access Memory)
m
(b) Secondary storage : Also known as auxiliary memory. This is non-volatile memory
And data is permanently stored in this. E.g : magnetic Disks, CD ,DVD, Hard Disk,
Pen Drives, Memory Cards etc.
co
Central Processing Unit (CPU) : This is also called processor which is chip inside the
Cabinet of computer system . It is referred as the brain of computer. It carries out instructions of a computer
ce.
program by performing basic arithmetic , logical , control and I/O operations specified by instruction. Speed of
CPU is measured in Giga Hertz (Ghz).
(iii) Control Unit : A control unit handles all the processor signals. It directs all the input and output
ren
flow, fetches instructions from microprograms and directs it to other units of computer by timing
and control signals.Control unit takes care about correct execution of instructions.
(iv) ALU : Arithmetic and logic unit is used to perform arithmetic opeartions and logical operations.
efe
(v) Output : This consists of operation that provides the result regarding certain input
And sends data from computer to another device provided in the system.
ak
O/P peripheral devices are printers, speakers, monitors, projectors, headphones etc.
Ans : (i) Top down programming : In this approach of programming a system is break down into
smaller sub systems , so that we can see more detail concept of modules. In a top down approach an overview
of the system is firstly formulated , then specifying but not detailing any first level sub systems.
Each sub system is then refined into more primary and smaller sub systems. This is called modularization.
Modularization makes the program to understand , write and debug.
m
Structured Programming : This implements logical structure on the program to be written. Thus
program becomes more efficient, simple and easy. This allows program loaded into memory and to be reused
.Modules are coded separately and tested. After that all the modules are combined together to make larger sub
co
system. Language like C , Pascal support structured programming. Each module has its own local data
and performs specific task.
ce.
Syntax and logical errors : When compiler compiles the program then due certain wrong inputs and wrong
codes errors occur. Syntax Error occurs when code or program has grammatical mistakes due to mistyping.
Like missing parenthesis, not using ; etc.Syntax error leads to no output.
ren
Logical Error are those which give the output which is not required for certain inputs due to wrong
interpretation of algorithm and logic in code.
Loader : This is an Operating system utility that copies programs from a storage A storage device to main
memory, where they can be executed.
efe
Absolute Loader : Loads the object program from translation time address and transfers control to it.
Bootstrap loader : This loader is responsible for loading the OS when system is turned ON and transferring
tur
Linker : Also called link editor / binder. It is a program that combines object modules to form an executable
program. This simplifies the designing of large softwares because they can be divided into smaller modules
ak
Ques 17. What is the structure of C program ? What are header files and their uses. Explain formatted
input output functions.
2) Preprocessor directive
3 ) Global variable declaration
4) main function( )
m
{
Local variables;
Statements;
co
}
User defined function
}
ce.
}
Comment line
It indicates the purpose of the program. It is represented as
ren
/*……………………………..*/
Comment line is used for increasing the readability of the program. It is useful in explaining the program and
generally used for documentation. It is enclosed withi the decimeters. Comment line can be single or multiple
line but should not be nested. It can be anywhere in the program except inside string constant & character
efe
constant.
Preprocessor Directive:
#include<stdio.h> tells the compiler to include information about the standard input/output library. It is also
used in symbolic constant such as #define PI 3.14(value). The stdio.h (standard input output header file)
tur
contains definition &declaration of system defined function such as printf( ), scanf( ) etc. Generally printf()
function used to display and scanf() function used to read value
Global Declaration:
ak
This is the section where variable are declared globally so that it can be access by all the functions used in the
program. And it is generally declared outside the function.
main()
It is the user defined function and every function has one main() function from where actually program is
started and it is encloses within the pair of curly braces. The main( ) function can be anywhere in the program
m
……..
}
co
The main( ) function return value when it declared by data type as
int main( )
{ return 0}
ce.
The main function does not return any value when void (means null/empty) as
void main(void ) or void main()
{
printf (“C language”);
ren
}
Output: C language
Example : /*First c program with return statement*/
#include <stdio.h>
efe
return 0;
}
Output: welcome to c programming language
ak
Header Files : When we work with large projects, it is desirable to separate out some procedures from main()
function of the program. Some procedures need to be used several times in different programs. So one solution
is to copy the code of that procedure from one program to another several times , which results in waste of
time.
So another solution is to make procedures and store them in a different file called header files. System defined
Header files contain the definitions of Library functions like printf(), scanf(), getch() , puts(), pow(), clrscr()
etc.Conventionally , header files ends with a ‘ . h’ extension and names can use only letters, digits, dashes ,
and underscores. Header files can be system define in compiler it self or programmers can design their own
header files too.
Examples : stdio.h : standard input & output ( functions like printf() ,scanf() are defined.)
conio.h : console input & output. ( getch(), clrscr () are defined.)
m
string.h : for string handling functions. ( )
math.h : Math functions like pow(), sin(), log() , sqrt() are defined.
stdlib.h : Library functions like exit (),
co
Foramtted Input & output functions : C language supports two formatting functions
printf () and scanf (). Printf() is used to convert text data stored in the program onto text stream for
ce.
o/p to the monitor. Scanf is used to convert text stream coming from keyboard to data values and stores
them in program variables. These are called as formatted Input and output functions because they have
special format to read and write The text from streams and then converting them into binary stream.
ren
Syntax of printf() : printf ( “ control string” , var1, var2 …..var n).
Printf (“Hello welcome to C program”)
Printf ( “ %d %d %f ” a , b , c), where a and b are integer variables and c is float variable.
%d and %f are known as format specifiers.
efe
Syntax of scanf() : scanf ( “ Control string” , & var1, & var2, ……..,& var n).
Format specifiers : For integer variables ------ %d
For float variables ------- %f
For character variables ------- %c
tur
Ans : (a)
(b)
By: Mr. Anuj Khanna (Assistant Professor), KIOT,Kanpur
www.aktureference.com Programming for Problem Solving (UNIT 1)
(c)
(d)
m
(e)
co
(f)
ce.
ren
efe
tur
Ans : Storage class in C decides the part of storage to allocate memory for a variable.
ak
It also determines the scope of a variable. All variables defined in a C program get some
physical location in memory where variable's value is stored.
Memory and CPU registers are types of memory locations where a variable's value can be
stored.
The storage class of a variable in C determines the life time of the variable if this is 'global' or
'local'.
Along with the life time of a variable, storage class also determines variable's storage location
(memory or registers), the scope (visibility level) of the variable, and the initial value of the
variable.
There are four storage classes in C those are automatic, register, static,
and external.
m
(i) Automatic Storage Class:
This is the default storage class for all the variables declared inside a function or a
block.
co
Auto variables can be only accessed within the block/function they have been
declared and not outside them (which defines their scope).
They are assigned a garbage value by default whenever they are declared.
ce.
Variables having automatic storage class are local to the block which they are
defined in, and get destroyed on exit from the block.
#include <stdio.h> }
ren
int main( ) printf ( "%d ", i);
{
auto int i = 1; printf( "%d\n", i);
{ }
auto int i = 2;
{ OUTPUT
efe
Static variables have default initial value zero and initialized only once in their
lifetime.
m
static int i; staticDemo();
{ }
static int i = 1; OUTPUT
co
printf("%d ", i); ======
i++; 10
} 21
ce.
printf("%d\n", i);
i++;
ren
efe
When extern specifier is used with a variable declaration then no storage is allocated
to that variable .
Iit is assumed that the variable has already been defined elsewhere in the program.
ak
When we use extern specifier the variable cannot be initialized because with extern specifier variable is
declared, not defined. #include <stdio.h>
extern int x;
int main()
{
printf("x: %d\n", x);
}
int x = 10;
By: Mr. Anuj Khanna (Assistant Professor), KIOT,Kanpur