Programming for Problem Solving
Unit-1
Computer Engineering
V.V.P College,Rajkot
Department of Computer Engineering Unit -1 Programming for Problem Solving
Que1: Explain block diagram of computer with its components.
Ans:
Components of computer system are as follow:
1. Input Devices
2. Central Processing Unit (CPU)
3. Primary Memory
4. Secondary Memory
5. Output Devices
1. Input Devices:
The input device is used to enter data into a computer.
The devices like keyboard, mouse and scanner are commonly used
input devices.
2|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
2. Central Processing Unit:
It is the brain of the computer.
Its main function is to execute programs stored in the main
memory.
CPU has a set of registers which are temporary storage areas for holding
data, and instructions
It consists of two functional units: ALU and CU.
1. Arithmetic and Logic Unit (ALU):
It performs arithmetic and logical operations on the data.
2. Control Unit (CU):
It instructs the complete computer system to perform a particular
task. It controls and coordinates the functioning of all parts of the
computer.
3. Primary Storage/Main Memory:
This is the place where the data is stored temporarily during
processing.
CPU directly stores and retrieves information from it.
Primary memory (main memory) is available in two forms:
1. RAM
2. ROM
1. RAM (Random Access Memory):
It is a temporary storage unit in a computer.
All the data from input device or from secondary
memory must come into RAM.
RAM is a volatile memory.
2. ROM (Read Only Memory) :
As name we can’t write any data into ROM, we can
just read the data from ROM.
It is a permanent storage medium which stores start
up programs.
These programs are loaded when computer is
switched on.
ROM is non-volatile memory.
3|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
4. Secondary Memory:
Secondary memory is also called Auxiliary memory.
It is used to store data permanently.
It can be modified easily.
It can store large data compared to primary memory.
E.g. HDD – Hard Disk Drive, Flash Drive, CD, DVD etc.
Operating System is also stored into secondary memory.
5. Output Devices:
It is used to display or print the result.
Monitor, printer and speakers are commonly used output
devices.
Que2: what is software? Explain different types of software.
Ans:
Software:
Software is collection of programs that allows users to perform a
specified task.
Program is set of instructions to perform a specific task by the
computer.
It is used to handle the hardware of computer.
Mainly two type of software are available.
1. System Software
2. Application Software
4|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
1. System Software:
System software is designed to operate the computer hardware.
It provides platform for running application software.
Three Types of system software:
1. Operating system
2. System support software
3. System development software
1. Operating System:
Operating System (OS) is an interface between computer user and
computer hardware; helps the user to interact with the computer
system.
It manages all the resources such as memory, CPU,
printer, hard disk, etc.,
E.g. Microsoft Windows, LINUX, UNIX
2. System Support Software
It makes working of hardware more efficiently.
E.g. Drivers of the I/O devices, Antivirus software.
3. System development software
It provides programming development environment to
programmers.
E.g. Editor, pre-processor, compiler, interpreter, loader.
2. Application Software
Application software designed to handle specific tasks for users.
There are two categories of application software:
1. General-purpose software
2. Specific-purpose software
1. General Purpose software
It is used widely by many people for some common task,
E.g. MS-Word, MS-Excel, PowerPoint, etc.
It is designed on vast concept so many people can use it.
2. Specific-purpose software
5|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
It is used by limited people for some specific task like
accounting software, tax calculation software, banking
software etc.
It is designed as per user’s special requirement.
Que3: What is Hardware?
Ans:
Hardware is the physical component which can be seen and
touched.
E.g. CPU, Motherboard, Monitor, Keyboard
Que4: Explain Types of Computer Languages Ans:
There are Two types of Computer Languages:
1. Low Level Language (LLL)
2. High Level Language (HLL)
1. Low Level Language
Low level language is divided into two types:
1. Machine level language
2. Assembly language
1. Machine Level Language
The Machine language is written using binary language
(0’s and 1’s).
Computer can understand only low level language. So there
is no need to convert this language.
Machine Level language is also known as Binary
Language and Low Level language.
Advantages:
Machine Level language is faster as compared to any other
language.
There is no need of translator.
Disadvantages:
As all the instruction and data are in 0’s and 1’s form, so it is very
difficult to remember as compared to all other language.
2. Assembly Language
6|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
As Machine level language is difficult to remember,
assembly language is introduced.
In this language all the instruction of 1’s and 0’s form are
converted into symbols, this symbols are known as Mnemonics.
But computer cannot understand assembly language, so we need to
convert this language into binary language.
The translator which is used to convert Assembly language into
Machine level language is known as Assembler.
Advantages:
It is easy to remember as compared to binary language.
Disadvantages:
We need to convert this language into binary, so speed is slow as
compared to binary language.
It is platform dependent.
2. High Level Language
As assembly language is platform dependent, to overcome this
problem high level language is introduce.
High level language is made up with A-Z, a-z, 0-9 and
mathematical symbols.
But computer can’t understand high level language; need to convert
this language into binary language.
To convert high level language into binary language two types of
translator are used.
1. Compiler
2. Interpreter
1. Compiler
It is used to convert high level language into binary
language.
It converts whole source code into binary language at a time.
It is faster than interpreter.
Error detection is difficult to identify as compared to
interpreter.
7|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
2. Interpreter
It is also used to convert high level language into binary
language.
It converts source code into object code (binary
language) as line by line.
It is slower as compared to compiler.
Error detection is easy as compared to compiler.
Que5: what is flow chart? Explain different symbol of flow chart.
Ans:
A flowchart is a graphical representation of sequence of operations to
be performed to get the solution of the problem.
It uses various symbols to show the operations and decisions to be
followed in a program.
It flows in sequential order.
Once the flowchart is drawn, it becomes easy to write the
computer program using any high level language like C.
8|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
Que6: Draw a flowchart to check whether number is Even or Odd.
Ans:
9|P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
Que7: what is algorithm? Explain with example.
Ans:
Algorithm is a finite sequence of well-defined steps for solving a
problem.
Algorithms are generally created independent of underlying
languages, i.e. an algorithm can be implemented in more than one
programming language.
Features of Algorithm are as follow:
1. Finiteness:
Algorithm must have finite steps.
2. Definiteness:
All instructions in an algorithm must be clear, precise, and easy to
interpret.
3. Effectiveness:
For an algorithm to be effective, it means that all those steps
that are required to get to output must be feasible with the
available resources.
It should not contain any unnecessary and redundant steps which
could make an algorithm ineffective.
4. Input:
10 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
An algorithm can have multiple inputs.
5. Output:
An algorithm produces at least one output.
E.g. Write an algorithm to find multiplication of three
numbers.
Step 1: Read the value of no1
Step 2: Read the value of no2
Step 3: Read the value of no3
Step 4: Calculate multiplication of three numbers by
answer = no1 * no2 * no3
Step 5: Print the answer
Step 6: Stop
Que8: Draw flowchart and write algorithm to find Largest of
three numbers.
Ans:
Algorithm:
Step 1: Read a, b, c
Step 2: If a>b, go to step 5
Step 3: If b>c, go to step 8
Step 4: Print c is largest number, go to step 9
Step 5: If a>c, go to step 7
Step 6: Print c is largest number, go to step 9
Step 7: Print a is largest number, go to step 9
11 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
Step 8: Print b is largest number
Step 9: Stop
Flowchart:
12 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
Que9: Explain Preprocessors and its types in C.
Ans:
Preprocessors are programs that process the source code before
compilation.
A number of steps are involved between writing a program and
executing a program in C.
All of these preprocessor directives begin with a ‘#’ (hash) symbol.
The ‘#’ symbol indicates that whatever statement starts with‘#’ will go
to the preprocessor program to get executed.
E.g. #include, #define, #undef
There are 3 Main Types of Preprocessor Directives:
1) Macros
2) File Inclusion
3) Conditional Compilation
13 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
1) Macros
• Macros are pieces of code in a program that is given some name.
• Whenever this name is encountered by the compiler, the
compiler replaces the name with the actual piece of code.
• The ‘#define’ directive is used to define a macro.
• Syntax:
#define token value
• There are two types of macros:
1. Object-like Macros
2. Function-like Macros
1. Object Like Macros:
The object-like macro is an identifier that is replaced by value.
It is widely used to represent numeric constants.
For example,
#define PI 3.14
Here, PI is the macro name which will be replaced by the value 3.14.
Example:
#include<stdio.h>
#define PI 3.14 void
main() {
int r=2; float a;
a=PI*r*r;
printf("%f", a);
}
Output:
12.56
14 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
2. Function-like Macros:
The function-like macro looks like function call.
It is used to represent function.
For Example
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
Example Program:
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main() {
printf("%d", MIN(2, 5));
}
Output:
2
2) File Inclusion Preprocessor
This type of preprocessor directive tells the compiler to include a file in
the source code program.
The #include preprocessor directive is used to include the header
files in the C program.
There are two types of files that can be included by the user in the
program:
1. Standard Header Files
2. User-defined Header Files
1. Standard Header Files
The standard header files contain definitions of pre-
defined functions like printf(), scanf(), etc.
These files must be included to work with these functions.
Different functions are declared in different header files.
Syntax:
#include <file_name>
2. User-defined Header Files
When a program becomes very large, it is a good practice to
divide it into smaller files and include them whenever needed.
These types of files are user-defined header files.
15 | P a g e Nirali Gondalia
Department of Computer Engineering Unit -1 Programming for Problem Solving
Syntax:
#include "filename"
3) Conditional Compilation
Conditional Compilation in C directives is a type of directive that
helps to compile a specific portion of the program or to skip the
compilation of some specific part of the program based on some
conditions.
For example:
1. #if Directive
Tests if a compile time condition is true.
2. #endif Directive
Ends preprocessor conditional.
3. #else Directive
The alternative for #if.
16 | P a g e Nirali Gondalia