unit 1
unit 1
UNIT 1
Q 1. Defini on of Computer?
• Computer is any device which aids humans in performing various kinds of computa ons or Calcula ons
Earliest Computer
• Originally calcula ons were computed by humans, whose job tle was computers.
• These human computers were typically engaged in the calcula on of a mathema cal expression.
• The calcula ons of this period were specialized and expensive, requiring years of training in mathema cs.
• The first use of the word "computer" was recorded in 1613, referring to a person who carried out calcula ons, or
computa ons, and the word con nued to be used in that sense un l the middle of the 20th century.
Tally S cks A tally s ck was an ancient memory aid device to record and document numbers, quan es, or
even messages.
Abacus
• An abacus is a mechanical device used to aid an individual in performing mathema cal calcula ons.
• The abacus in the form we are most familiar with was first used in China in around 500 B.C.
Napier’s Bones
• Allowed the operator to mul ply, divide and calculate square and cube roots by moving the rods around
and placing them in specially constructed boards.
Slide Rule
– mul plica on
– division
– roots
– logarithms
– Trigonometry
• Not normally used for addi on or subtrac on.
Pascaline
• It is too expensive.
Arithmometer
• The machine could perform the four basic mathema c func ons.
• In 1840, Augusta Ada Byron suggests to Babbage that he use the binary system.
ENIAC
• Completed in 1946.
UNIVAC 1
• The UNIVAC I (UNIVersal Automa c Computer 1) was the first commercial computer.
3
EDVAC
• The first computers used vacuum tubes for circuitry and magne c drums for memory, and were o en enormous,
taking up en re rooms.
• They were very expensive to operate and in addi on to using a great deal of electricity, generated a lot of heat,
which was o en the cause of malfunc ons.
• First genera on computers relied on machine language, the lowest-level programming language understood by
computers, to perform opera ons, and they could only solve one problem at a me.
• Input was based on punched cards and paper tape, and output was displayed on printouts.
• Transistors replaced vacuum tubes and ushered in the second genera on of computers.
• Allowing computers to become smaller, faster, cheaper, more energy-efficient and more reliable.
4
• Second-genera on computers moved from cryp c binary machine language to symbolic, or assembly, languages,
which allowed programmers to specify instruc ons in words.
• Second-genera on computers s ll relied on punched cards for input and printouts for output.
• These were also the first computers that stored their instruc ons in their memory, which moved from a magne c
drum to magne c core technology.
• The development of the integrated circuit was the hallmark of the third genera on of computers.
• Transistors were miniaturized and placed on silicon chips, called semiconductors, which dras cally increased the
speed and efficiency of computers.
• Users interacted with third genera on computers through keyboards and monitors and interfaced with an
opera ng system, which allowed the device to run many different applica ons at one me with a central program
that monitored the memory.
• Computers for the first me became accessible to a mass audience because they were smaller and cheaper than
their predecessors.
• The microprocessor brought the fourth genera on of computers, as thousands of integrated circuits
• As these small computers became more powerful, they could be linked together to form networks, which
• Fourth genera on computers also saw the development of GUIs, the mouse and handheld devices.
The Fi h Genera on
• S ll in development.
• The use of parallel processing and superconductors is helping to make ar ficial intelligence a reality.
• The goal is to develop devices that respond to natural language input and are capable of learning and self-
organiza on.
• There are some applica ons, such as voice recogni on, that are being used today.
5
A computer can be defined as a fast electronic calcula ng machine that accepts the (data) digi zed input informa on
process it as per the list of internally stored instruc ons and produces the resul ng informa on.
List of instruc ons are called programs & internal storage is called computer memory.
1. Personal computers: - This is the most common type found in homes, schools, Business offices etc., It is
the most common type of desk top computers with processing and storage units along with various input and
output devices.
3. Work sta ons: - These have high resolu on input/output (I/O) graphics capability, but with same
dimensions as that of desktop computer. These are used in engineering applica ons of interac ve design
work.
4. Enterprise systems: - These are used for business data processing in medium to large corpora ons that
require much more compu ng power and storage capacity than work sta ons. Internet associated with
servers have become a dominant worldwide source of all types of informa on.
5. Super computers: - These are used for large scale numerical calcula ons required in the applica ons like
weather forecas ng etc.,
Q 6. Explain Basic Organiza on of Computer? Describe briefly about Func onal units?
A computer consists of five func onally independent main parts input, memory, arithme c logic unit (ALU), output
and control unit.
ALU stands for arithmetic logic unit. It is responsible for performing all arithmetic and logical operations, such as
addition, subtraction, multiplication, division, AND, OR, and NOT.
Input-output units allow the computer to communicate with the outside world. They include devices such as the
keyboard, mouse, monitor, printer, and speakers.
Memory is where the computer stores data and instructions.
It is divided into two main types:
1. Random Access Memory (RAM) And
2. Read-Only Memory (ROM).
RAM is used to store data and instructions that are currently being used by the CPU.
ROM is used to store permanent data, such as the computer's startup instructions.
Program counter is a register that keeps track of the address of the next instruction to be executed by the CPU.
These four components work together to execute computer programs. The CPU first fetches the next instruction from
memory using the program counter. It then decodes the instruction and determines what operation needs to be
6
performed. The ALU then performs the operation and stores the result in memory. Finally, the program counter is
incremented to point to the next instruction.
Here is a simplified example of how the ALU, input-output units, memory, and program counter work together to
execute a simple program:
Program:
1. ADD 1 to A
2. PRINT A
Execution:
1. The CPU fetches the first instruction from memory (ADD 1 to A).
2. The CPU decodes the instruction and determines that it needs to perform an addition operation.
3. The CPU loads the operand A from memory.
4. The ALU adds 1 to A and stores the result back in memory.
5. The program counter is incremented to point to the next instruction.
6. The CPU fetches the next instruction from memory (PRINT A).
7. The CPU decodes the instruction and determines that it needs to print the value of A to the console.
8. The CPU loads the value of A from memory.
9. The CPU writes the value of A to the console.
10. The program counter is incremented to point to the next instruction, which is the end of the program.
A programming language is a set of symbols, grammars and rules with the help of which one is able to translate
algorithms to programs that will be executed by the computer.
The programmer communicates with a machine using programming languages.
Most of the programs have a highly structured set of rules.
1. Machine Languages
2. Assembly Languages
3. High level Languages.
Machine Language
Machine language is a collec on of binary digits or bits that the computer reads and interprets.
Machine language is the only language a computer is capable of understanding.
Machine level language is a language that supports the machine side of the programming or does not provide
human side of the programming.
It consists of (binary) zeros and ones.
Each instruc on in a program is represented by a numeric code, and numerical addresses are used
throughout the program to refer to memory loca ons in the computer’s memory.
Microcode allows for the expression of some of the more powerful machine level instruc ons in terms of a
set of basic machine instruc ons.
Assembly language
High level language is a language that supports the human and the applica on sides of the programming.
A language is a machine independent way to specify the sequence of opera ons necessary to accomplish a
task.
A line in a high level language can execute powerful opera ons, and correspond to tens, or hundreds, of
instruc ons at the machine level.
Consequently, more programming is now done in high level languages.
Examples of high level languages are BASIC, FORTRAN etc
Algorithm:
OR
A procedure for solving a mathema cal problem in a finite number of steps that frequently involves recursive
opera ons
Proper es of Algorithm:
5. Effec veness: Every step must be basic and easy to convert into program.
Example:
Algorithm:
Step-1: Start
Step-6: Stop
Q 9. What is FlowChart?
Q 10. Define flowchart. List the different symbols used to draw a flowchart ?
OR
9
1. Start
2. Input two numbers
3. Calculate the sum of the two numbers
4. Divide the sum by 2 to get the average
5. Display the average
6. End
The compiler program translates the instruc ons of a high level language to a machine level language.
A separate compiler is required for every high level language.
High level language is simply a programmer’s convenience and cannot be executed in their source.
The actual high - level program is called a source program.
It is compiled (translated) to machine level language program called object program for that machine by the
compiler.
Such compilers are called self-resident compilers.
Compiler compiles the full program and reports the errors at the end
Compila on Process
The compila on and execu on process of C can be divided in to mul ple steps:
10
Preprocessing Using a Preprocessor program to convert C source code in expanded source code. "#include"
and "#define" statements will be processed and replaced actually source codes in this step.
Compila on Using a Compiler program to convert C expanded source to assembly source code.
Assembly Using a Assembler program to convert assembly source code to object code.
Linking Using a Linker program to convert object code to executable code. Mul ple units of object codes are
linked to together in this step.
Loading Using a Loader program to load the executable code into CPU for execu on. Compila on
LINKING
A er all of the files are compiled, they must be "merged together" to produce a single executable file that the
user use to run the program.
In C, most compiled programs produce results only with the help of some standard programs, known as library
files that reside in the computer.
This process is called linking. The result obtained a er linking is called the executable file.
The linker′s primary func on is to bind symbolic names to memory addresses.
To do this, it first scans the files and concatenates the related file sec ons to form one large file.
Then, it makes a second pass on the resul ng file to bind symbol names to real memory addresses.
Loading is loading the executable into memory prior to execu on.
There are two types of linking: Sta c linking. Dynamic linking.
o Sta c linking occurs at compila on me; hence it occurs prior to loading a program.
o With sta c linking the external symbols that are used by the program (e.g. func on names) are resolved
at compile me.
o Dynamic linking occurs at run me, so it occurs a er or at the me of the loading of a program.
o With dynamic linking the symbols are resolved either at loading me, or at run me when the symbol
is accessed (lazy binding).
LOADING
A er the files are compiled and linked the executable file is loaded in the computer′s memory for execu ng by
the loader. This process is called Loading.
Program loading is basically copying a program from secondary storage into main memory so it ′s ready to run.
In some cases, loading us just not copying the data from disk to memory, but also se ng protec on bits, or
arranging for virtual memory map virtual addresses to disk pages.
11
The file which contains c program instruc ons in a high-level language is said to be source code.
Every c program source file is saved with .c extension, for example, Sample.c.
Whenever we press compila on bu on the source file is submi ed to the compiler.
Compiler checks for the errors, if there are any errors, it returns a list of errors, otherwise generates object
code in a file with name Sample.obj and submit it to the linker.
The linker combines the code from specified header file into an object file and generates executable file as
Sample.exe.
With this compila on process completes. Now, we need to run the executable file (Sample.exe).
To run a program, we press run bu on.
When we press run bu on the executable file is submi ed to the CPU.
Then CPU performs the task according to the instruc ons wri en in that program and place the result into
User Screen.
C is a structured programming language. Every C program and its statements must be in a par cular structure.
Every C program has the following general structure.
12
A data type specifies the type of data that a variable can store such as integer, floa ng, character, etc.
There are the following data types in C language.
13
1. Integer:
2. Floa ng Point:
Floa ng-point data types are a set of numbers with the decimal value.
We use the keyword "float" to represent floa ng-point data type and "double" to represent double data type
in C.
The following table provides complete details about floa ng-point data types.
3. Character:
The character data type is a set of characters enclosed in single quota ons.
The following table provides complete details about the character data type.
Q 18. Define variable. Explain how to declare and ini alize a variable in C.
Variable:
Declara on:
Declara on of a variable tells the compiler to allocate the required amount of memory with the specified
variable name and allows only specified datatype values into that memory loca on.
Syntax:
Example:
int number;
The above declara on tells to the compiler that allocates 2 bytes of memory with the name number and allows only
integer values into that memory loca on.
Variable_1 is Valid
16
4. Special symbols like !, @, #, $, % etc. are not allowed in variables except one special symbol underscore ( _ ).
1. Arithme c Operators
3. Logical Operators
4. Assignment Operator
18
6. Bitwise Operators
1. Arithme c Operators:
The arithme c operators are used to perform basic mathema cal opera ons like addi on, subtrac on,
mul plica on, division and percentage modulo.
The following table provides informa on about arithme c operators.
Example Program:
The rela onal operators are used to check the rela onship between two values.
Every rela onal operator has two results TRUE or FALSE.
In simple words, the rela onal operators are used to define condi ons in a program.
The following table provides informa on about rela onal operators.
Example Program:
Output:
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operators:
The logical operators are used to check logical rela onship between two condi ons.
The following table provides informa on about logical operators.
20
Example Program:
4. Assignment Operator:
Example Program:
Example Program:
Output:
+a = 25
-a = -25
a++ = 25
a-- = 26
6. Bitwise Operators:
The bitwise operators are used to perform bit-level opera ons in the C.
When we use the bitwise operators, the opera ons are performed based on the binary values.
Example Program:
The condi onal operator is also called a ternary operator because it requires three operands.
This operator is used for decision making.
o Syntax:
Condi on? TRUE Part : FALSE Part;
If the condi on is TRUE the TRUE Part is performed, if the condi on is FALSE the FALSE Part is performed.
Example:
int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;
Example:
int totalMarks = 450, maxMarks = 600 ;
float average ;
average = (float) totalMarks / maxMarks * 100 ;
Explaina on:
In the above example code, both totalMarks and maxMarks are integer data values. When we perform
totalMarks / maxMarks the result is a float value, but the des na on (average) datatype is a float. So we use
type cas ng to convert totalMarks and maxMarks into float data type.
Q 24. What is Top-Down Approach?
Top-Down Approach is an approach to design algorithms in which a bigger problem is broken down into smaller
parts.
Thus, it uses the decomposi on approach.
This approach is generally used by structured programming languages such as C, COBOL, FORTRAN.
The implementa on of algorithm using top-down approach depends on the programming language and
pla orm.
Top-down approach is generally used with documenta on of module and debugging code.
Drawbacks
The top-down approach is that it may have redundancy since every part of the code is developed separately.
Also, there is less interac on and communica on between the modules in this approach.
25
Bo om-Up Approach is one in which the smaller problems are solved, and then these solved problems are
integrated to find the solu on to a bigger problem.
Therefore, it uses composi on approach.
It requires a significant amount of communica on among different modules.
It is generally used with object oriented programming paradigm such as C++, Java, and Python.
Data encapsula on and data hiding is also implemented in this approach.
The bo om-up approach is generally used in tes ng modules.
1. In this approach, the problem is broken down into In this approach, the smaller problems are solved.
smaller parts.
4. It does not require communica on between It requires rela vely more communica on between
modules. modules.
5. It contains redundant informa on. It does not contain redundant informa on.
7. The implementa on depends on the programming Data encapsula on and data hiding is implemented
language and pla orm. in this approach.
26
Time complexity refers to the amount of me an algorithm takes to run as a func on of the size of its input.
It is usually expressed in terms of "big O" nota on, which gives an upper bound on the growth rate of the
algorithm's running me.
For example, an algorithm with a me complexity of O(n) means that its running me grows linearly with the
size of the input.
Space complexity, on the other hand, refers to the amount of memory an algorithm requires to run as a
func on of the size of its input.
It is also expressed in terms of big O nota on.
For example, an algorithm with a space complexity of O(n) means that it requires a linear amount of memory
to run as the size of the input grows.
It's important to consider both me and space complexity when analyzing algorithms, as they can have a
significant impact on the performance and efficiency of the algorithm. Generally, we want algorithms with lower me
and space complexi es, as they will be faster and require less memory to run.
9. Write a C Program