0% found this document useful (0 votes)
27 views26 pages

PPS Module-1

Uploaded by

Shubh D. Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views26 pages

PPS Module-1

Uploaded by

Shubh D. Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

JC Bose University of Science & Technology,YMCA

PPS Lecture - Notes

Prepared by : Ms. Ritu Garg


(Assistant Professor,JCBUST,YMCA,Faridabad)
What is a Computer?
A computer is an electronic device that accepts data from the user, processes it,
produces results, displays them to the users, and stores the results for future usage.
Data is a collection of unorganized facts & figures and does not provide any further
information regarding patterns, context, etc. Hence data means "unstructured facts and
figures".
Information is a structured data i.e. organized meaningful and processed data. To
process the data and convert into information, a computer is used.

Functions of Computers
A computer performs the following functions −

Receiving Input
Data is fed into computer through various input devices like keyboard, mouse, digital
pens, etc. Input can also be fed through devices like CD-ROM, pen drive, scanner, etc.
Processing the information
Operations on the input data are carried out based on the instructions provided in the
programs.
Storing the information
After processing, the information gets stored in the primary or secondary storage area.

Producing output
The processed information and other details are communicated to the outside world
through output devices like monitor, printer, etc.

Components of Computer
Computer systems consist of three components as shown in below image: Central
Processing Unit, Input devices and Output devices. Input devices provide data input
to processor, which processes data and generates useful information that’s displayed to
the user through output devices. This is stored in computer’s memory.
Input devices help to get input or data from user. Some of input devices are −

Name Characteristics Image

Keyboard The keyboard was first peripheral


device to be used with computers.
It helps to input text and numbers into
computer.
It consists of 104 keys and 12
functional keys.

Mouse A mouse is an input device which is


also called as pointing device because
it helps to point data on screen.
It also helps to select, highlight content
and drag-drop controls.
Output devices help to display output to user. Some of output devices are −

Output Characteristics Image


Devices

Monitor A monitor is most common type of


output device.
It is also called as "Visual Display
Unit".
The inputs given by keyboard or any
other input devices will get displayed
on monitor.
Cathode Ray Tube (CRT) and Flat
panel display monitors are commonly
used monitors.

Printers Printers are most common type of


output devices which are used to take
a hard copy of any digital document.
The two types of printers are impact
and non-impact printers.
Non-impact printers such as laser and
inkjet printers are less noisy, more
reliable and faster and also offer high
quality compared to impact printers.

Sound systems are output devices


Sound
which are used to get multimedia
Systems
content such as voice, music, etc., as
output. Some of examples of sound
systems are speakers, headphones,
and microphones.

Central Processing Unit


The Central Processing Unit (CPU) is called "the brain of computer" as it controls
operation of all parts of computer. It consists of two components: Arithmetic Logic Unit
(ALU), and Control Unit.
Arithmetic Logic Unit (ALU)
Data entered into computer is sent to RAM, from where it is then sent to ALU, where
rest of data processing takes place. All types of processing, such as comparisons,
decision-making and processing of non-numeric information takes place here and once
again data is moved to RAM.

Control Unit
As name indicates, this part of CPU extracts instructions, performs execution, maintains
and directs operations of entire system.
Functions of Control Unit
Control unit performs following functions −

 It controls all activities of computer


 Supervises flow of data within CPU
 Directs flow of data within CPU
 Transfers data to Arithmetic and Logic Unit
 Transfers results to memory
 Fetches results from memory to output devices

Memory Unit
This is unit in which data and instructions given to computer as well as results given by
computer are stored. Unit of memory is "Byte".
1 Byte = 8 Bits
Computer memory refers to storage area where data is stored. It is of two types −

 Primary Memory
 Secondary Memory

Primary Memory
Primary memory is the main memory of computer present in motherboard. Primary
memory is of two types as shown in the image below.

Random Access Memory


RAM is referred as temporary memory, in which, information stored is lost once
computer is turned off. It is a volatile memory. Instructions written in this memory can be
modified; hence it is also known as programmable memory. The two types of RAM
are Static RAM (faster and costlier) and Dynamic RAM.

Functions of RAM are as follows −


 It stores data till it gets processed.
 It stores instructions for data processing.
 It acts as a working space where data processing takes place and intermediate
results are stored.
 It stores processed data/results before it is sent to output devices.
Read Only Memory
ROM is referred as permanent memory, in which information stored is available even if
computer is turned off. Instructions stored in this memory can only be read and cannot
be modified. Mostly ROM has a start-up instruction which is executed every time when
computer is switched on. Types of ROM are PROM (Programmable Read Only
Memory), EPROM (Erasable PROM), EEPROM (Electrically Erasable PROM) and flash
memory.

The below table jots down the major differences between RAM and ROM −

Sr.No RAM ROM

1 It is volatile memory. It is non-volatile memory.

2 The contents are temporary; data The contents are permanent; data is
is lost when electricity supply is not lost even when power is switched
lost. off.

3 Available in small storage capacity. Available in high storage capacity.

4 Processing speed is high. Processing speed is low.

5 User-defined programs can be Generally, operating system supporting


stored. programs can be stored.

6 Cost is very high. Cost effective.

7 It is of two types, SRAM and It comes in different types such as


DRAM. PROM, EPROM, EEPROM and flash
memory.

Secondary Memory
Sometimes when data to be processed is large, it cannot fit in primary memory as it is
limited, in such cases, we use supplement memory or secondary memory. Secondary
memory helps to store information permanently and is non-volatile. Examples of
secondary storage memory are compact disk, floppy disk, pen drive, external hard
drive, etc.

Operating System
Operating system is a software that controls system’s hardware and interacts with user
and application software.
In short, an operating system is computer’s chief control program.

Functions of Operating System


The operating system performs the following functions −
 It offers a user interface.
 Loads program into computer’s memory.
 Coordinates how program works with hardware and other software.
 Manages how information is stored and retrieved from the disk.
 Saves contents of file on to disk.
 Reads contents of file from disk to memory.
 Sends document to the printer and activates the printer.
 Provides resources that copy or move data from one document to another, or
from one program to another.
 Allocates RAM among the running programs.
 Recognizes keystrokes or mouse clicks and displayes characters or graphics on
the screen.

Introduction to Compiler
o A compiler is a translator that converts the high-level language into the machine
language.
o High-level language is written by a developer and machine language can be
understood by the processor.
o Compiler is used to show errors to the programmer.
o The main purpose of compiler is to change the code written in one language
without changing the meaning of the program.
o When you execute a program which is written in HLL programming language
then it executes into two parts.
o In the first part, the source program compiled and translated into the object
program (low level language).
o In the second part, object program translated into the target program through
the assembler.

Fig: Execution process of source program in Compiler

Algorithm: An algorithm is a step-by-step method for


solving some problem.
An algorithm is a process or set of rules which must be followed to complete a
particular task. This is basically the step-by-step procedure to complete any
task. All the tasks are followed a particular algorithm, from making a cup of tea
to make high scalable software. This is the way to divide a task into several
parts. If we draw an algorithm to complete a task then the task will be easier to
complete.
The algorithm is used for,
 To develop a framework for instructing computers.
 Introduced notation of basic functions to perform basic tasks.
 For defining and describing a big problem in small parts, so that it is very
easy to execute.

Characteristics of Algorithm
1. An algorithm should be defined clearly.
2. An algorithm should produce at least one output.
3. An algorithm should have zero or more inputs.
4. An algorithm should be executed and finished in finite number of steps.
5. An algorithm should be basic and easy to perform.
6. Each step started with a specific indentation like, “Step-1”,
7. There must be “Start” as the first step and “End” as the last step of the
algorithm.

Let’s take an example to make a cup of tea,

Step 1: Start
Step 2: Take some water in a bowl.
Step 3: Put the water on a gas burner.
Step 4: Turn on the gas burner
Step 5: Wait for some time until the water is boiled.
Step 6: Add some tea leaves to the water according to the requirement.
Step 7: Then again wait for some time until the water is getting colorful as tea.
Step 8: Then add some sugar according to taste.
Step 9: Again wait for some time until the sugar is melted.
Step 10: Turn off the gas burner and serve the tea in cups with biscuits.
Step 11: End

Here is an algorithm for making a cup of tea. This is the same for computer
science problems.

There are some basics steps to make an algorithm:


1. Start – Start the algorithm
2. Input – Take the input for values in which the algorithm will execute.
3. Conditions – Perform some conditions on the inputs to get the desired
output.
4. Output – Printing the outputs.
5. End – End the execution.

Example 1. Swap two numbers with a third variable


Step 1: Start
Step 2: Take 2 numbers as input.
Step 3: Declare another variable as “temp”.
Step 4: Store the first variable to “temp”.
Step 5: Store the second variable to the First variable.
Step 6: Store the “temp” variable to the 2nd variable.
Step 7: Print the First and second variables.
Step 8: End
Example 2. Find the area of a rectangle
Step 1: Start
Step 2: Take the Height and Width of the rectangle as input.
Step 3: Declare a variable as “area”
Step 4: Multiply Height and Width
Step 5: Store the multiplication to “Area”, (its look like area = Height x Width)
Step 6: Print “area”;
Step 7: End

WAP in C

#include<stdio.h>
#include<conio.h>

int main() {
int length, breadth, area;

printf("\nEnter the Length of Rectangle : ");


scanf("%d", &length);

printf("\nEnter the Breadth of Rectangle : ");


scanf("%d", &breadth);

area = length * breadth;


printf("\nArea of Rectangle : %d", area);

return (0);
}

Output:

Enter the Length of Rectangle : 5


Enter the Breadth of Rectangle : 4
Area of Rectangle : 20
Flow Chart

Example 3. Find the greatest between 3 numbers.


Step 1: Start
Step 2: Take 3 numbers as input, say A, B, and C.
Step 3: Check if(A>B and A>C)
Step 4: Then A is greater
Step 5: Print A
Step 6: Else
Step 7: Check if(B>A and B>C)
Step 8: Then B is greater
Step 9: Print B
Step 10: Else C is greater
Step 11: Print C
Step 12: End
Example: 4 Write an algorithm to print the numbers from 1 to 20.

Step 1: Initialize a variable 'x' as 0,

Step 2: Increment the variable 'x' by 1,

Step 3: Print the variable 'x'

Step 4: If 'x' is less than 20, then go back to step 2. Otherwise, go to the next step.

Step 5: Exit.

Advantages of Algorithm
 An algorithm uses a definite procedure.
 It is easy to understand because it is a step-by-step definition.
 The algorithm is easy to debug if there is any error happens.
 It is not dependent on any programming language
 It is easier for a programmer to convert it into an actual program because the
algorithm divides a problem into smaller parts.

Disadvantages of Algorithms
 An algorithm is Time-consuming, there is specific time complexity for
different algorithms.
 Large tasks are difficult to solve in Algorithms because the time complexity
may be higher, so programmers have to find a good efficient way to solve
that task.
 Looping and branching are difficult to define in algorithms.

Flowchart
The Flowchart is the most widely used graphical representation of an algorithm and
procedural design workflows. It uses various symbols to show the operations and
decisions to be followed in a program. It flows in sequential order. As an instance, a
parallelogram in the flowchart may be used to indicate input and output, a rectangular
box indicates a mathematical operation, a diamond symbol indicates the decision-
making statements, and several other symbols are used in flowcharts.
In many cases, a programmer usually makes a flowchart using paper and pencil or
makes it by connecting the shapes on a computer screen using the software. In a large
system, a flowchart is an important document for a system and individual program
because it summarizes a program's function in the form of symbols that are easy to
understand and clearly explained in English.

Advantages of flowchart

o Proper debugging
o Effective analysis
o Efficient coding
o Proper documentation
o Efficient program maintenance

Disadvantages of flowchart

o Time-consuming
o Complex
o Difficult to modify
o It has no standard
Programming Errors in C
Errors are the problems or the faults that occur in the program, which makes the
behavior of the program abnormal, and experienced developers can also make these
faults. Programming errors are also known as the bugs or faults, and the process of
removing these bugs is known as debugging.

These errors are detected either during the time of compilation or execution. Thus, the
errors must be removed from the program for the successful execution of the program.

There are mainly five types of errors exist in C programming:

o Syntax error
o Run-time error
o Linker error
o Logical error
o Semantic error
Syntax error
Syntax errors are also known as the compilation errors as they occurred at the
compilation time, or we can say that the syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing or do not follow the
syntax of the specified programming language. These mistakes are generally made by
beginners only because they are new to the language. These errors can be easily
debugged or corrected.

For example

1. If we want to declare the variable of type integer,


2. int a; // this is the correct form
3. Int a; // this is an incorrect form.

Commonly occurred syntax errors are:

o If we miss the parenthesis (}) while writing the code.


o Displaying the value of a variable without its declaration.
o If we miss the semicolon (;) at the end of the statement.

Let's understand through an example

1. #include <stdio.h>
2. int main()
3. {
4. a = 10;
5. printf("The value of a is : %d", a);
6. return 0;
7. }
In the above output, we observe that the code throws the error that 'a' is undeclared.
This error is nothing but the syntax error only.

There can be another possibility in which the syntax error can exist, i.e., if we make
mistakes in the basic construct. Let's understand this scenario through an example.

1. #include <stdio.h>
2. int main()
3. {
4. int a=2;
5. if(.) // syntax error
6.
7. printf("a is greater than 1");
8. return 0;
9. }

In the above code, we put the (.) instead of condition in 'if', so this generates the syntax
error as shown in the below screenshot.

Output

Logical error
The logical error is an error that leads to an undesired output. These errors produce the
incorrect output, but they are error-free, known as logical errors. These types of
mistakes are mainly done by beginners. The occurrence of these errors mainly depends
upon the logical thinking of the developer. If the programmers sound logically good,
then there will be fewer chances of these errors.
Let's understand through an example.

1. #include <stdio.h>
2. int main()
3. {
4. int sum=0; // variable initialization
5. int k=1;
6. for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop
7. {
8. sum=sum+k;
9. k++;
10. }
11. printf("The value of sum is %d", sum);
12. return 0;
13. }

Output

In the above code, we are trying to print the sum of 10 digits, but we got the wrong
output as we put the semicolon (;) after the for loop, so the inner statements of the for
loop will not execute. This produces the wrong output.
Source Code, Object Code and Executable Code

1. Source Code: Source code refers to high level code


or assembly code which is generated by
human/programmer. Source code is easy to read and
modify. It is written by programmer by using any
High Level Language or Intermediate language which
is human-readable. Source code contains comments
that programmer puts for better understanding.

Source code is provided to language translator which


converts it into machine understandable code which is
called machine code or object code. Computer can not
understand direct source code, computer understands machine
code and executes it. It is considered as fundamental
component of computer. In simple we can say source code is
a set of instructions/commands and statements which is
written by a programmer by using a computer programming
language like C, C++, Java, Python, Assembly language etc.
So statements written in any programming language is termed
as source code.

2. Object Code: Object code refers to low level code


which is understandable by machine. Object code is
generated from source code after going through
compiler or other translator. It is in executable
machine code format. Object code contains a
sequence of machine understandable instructions to
which Central Processing Unit understands and
executes
.
Object file contains object code. It is considered as one
more of machine code. Some object file examples are common
object file format (COFF), COM files and “.exe” files. It
is the output of a compiler or other translator. We can
understand source code but we can not understand object
code as it is not in plain text like source code rather it
is in binary formats.
The below figure illustrates the source code and object
code:
Difference between Source Code and Object Code :
S.
No. SOURCE CODE OBJECT CODE

Object code is
Source code is generated by generated by compiler
01. human or programmer. or other translator.

Object code is low


02. Source code is high level code. level code.

Object code is
Source code is written in plain translated code of
text by using some high level source code. It is in
03. programming language. binary format.

Source code is human Object code is not


04. understandable. human understandable.

Object code is machine


Source code is not directly understandable and
05. understandable by machine. executable.

It is written in a high-level It is written in


language like C, C++, Java, machine language
06. Python, etc., or assembly through compiler or
language. assembler or other
translator.

07. It can be easily modified. It can not be modified.

It does not contain


comments for
It contains comments for better understanding by
08. understanding by programmer. machine.

It contains more number


It contains less number of of statements than
09. statements than object code. source code.

It is less close. towards It is more close


10. machine. towards machine.

Performance of object
code is more than
Performance of source code is source code as it is
less than object code as it is more close towards
11. less close towards machine. machine.

Source code is input to Object code is output


compiler or any other of compiler or any
12. translator. other translator.

Source code is not system Object code is system


13. specific. specific.

Source code needs to be


compiled or translated
by any other translator
to get modified object
14. It can be changed over time. code.

Language translators like Object code is machine


compiler, assembler, code so it does not
15. interpreter are used to require any
translate source code to object translation.
code.

The source lines of code gives


the readability and
understandability to the user.
Use of fewer lines of code
gives better performance by
giving same results in most This is not the case
16. cases. with object code.

What is a compilation?
The compilation is a process of converting the source code into object code. It is done
with the help of the compiler. The compiler checks the source code for the syntactical or
structural errors, and if the source code is error-free, then it generates the object code.

The c compilation process converts the source code taken as input into the object code
or machine code. The compilation process can be divided into four steps, i.e., Pre-
processing, Compiling, Assembling, and Linking.

The preprocessor takes the source code as an input, and it removes all the comments
from the source code. The preprocessor takes the preprocessor directive and interprets
it. For example, if <stdio.h>, the directive is available in the program, then the
preprocessor interprets the directive and replace this directive with the content of
the 'stdio.h' file.

The following are the phases through which our program passes before being
transformed into an executable form:
o Preprocessor
o Compiler
o Assembler
o Linker

Preprocessor
The source code is the code which is written in a text editor and the source code file is
given an extension ".c". This source code is first passed to the preprocessor, and then
the preprocessor expands this code. After expanding the code, the expanded code is
passed to the compiler.

Compiler
The code which is expanded by the preprocessor is passed to the compiler. The
compiler converts this code into assembly code. Or we can say that the C compiler
converts the pre-processed code into assembly code.

Assembler
The assembly code is converted into object code by using an
assembler. The name of the object file generated by the
assembler is the same as the source file. The extension of the
object file in DOS is '.obj,' and in UNIX, the extension is 'o'.
If the name of the source file is 'hello.c', then the name of
the object file would be 'hello.obj'.

Linker
Mainly, all the programs written in C use library functions.
These library functions are pre-compiled, and the object code of
these library files is stored with '.lib' (or '.a') extension.
The main working of the linker is to combine the object code of
library files with the object code of our program. Sometimes the
situation arises when our program refers to the functions
defined in other files; then linker plays a very important role
in this. It links the object code of these files to our program.
Therefore, we conclude that the job of the linker is to link the
object code of our program with the object code of the library
files and other files. The output of the linker is the
executable file. The name of the executable file is the same as
the source file but differs only in their extensions. In DOS,
the extension of the executable file is '.exe', and in UNIX, the
executable file can be named as 'a.out'. For example, if we are
using printf() function in a program, then the linker adds its
associated code in an output file.

Let's understand through an example.

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello javaTpoint");
5. return 0;
6. }

Now, we will create a flow diagram of the above program:


In the above flow diagram, the following steps are taken to execute a program:
o Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the
preprocessor converts the source code into expanded source code. The extension
of the expanded source code would be hello.i.
o The expanded source code is passed to the compiler, and the compiler converts
this expanded source code into assembly code. The extension of the assembly
code would be hello.s.
o This assembly code is then sent to the assembler, which converts the assembly
code into object code.
o After the creation of an object code, the linker creates the executable file. The
loader will then load the executable file for the execution.

You might also like