PPS_I_Moule_Notes
PPS_I_Moule_Notes
Idea of Algorithm: Representation of Algorithm, Flowchart, Pseudo code with examples, From
algorithms to programs, source code.
Programming Basics: Structure of C program, writing and executing the first C program,
Syntax and logical errors in compilation, object and executable code. Components of C
language. Standard I/O in C, Fundamental data types, Variables and memory locations, Storage
classes.
MEMORY:
A memory unit is the collection of storage units or devices together. The memory unit stores the
binary information in the form of bits. Generally, memory/storage is classified into 2 categories:
Volatile Memory: This loses its data, when power is switched off.
Non-Volatile Memory: This is a permanent storage and does not lose any data when power
is switched off.
The total memory capacity of a computer can be visualized by hierarchy of components. The
memory hierarchy system consists of all storage devices contained in a computer system from the
slow Auxiliary Memory to fast Main Memory and to smaller Cache memory.
Auxillary memory access time is generally 1000 times that of the main memory, hence it is at the
bottom of the hierarchy.
The main memory occupies the central position because it is equipped to communicate directly with
the CPU and with auxiliary memory devices through Input/output processor (I/O).
When the program not residing in main memory is needed by the CPU, they are brought in from
auxiliary memory. Programs not currently needed in main memory are transferred into auxiliary
memory to provide space in main memory for other programs that are currently in use.
The cache memory is used to store program data which is currently being executed in the CPU.
Approximate access time ratio between cache memory and main memory is about 1 to 7~10
1. Random Access: Main memories are random access memories, in which each memory
location has a unique address. Using this unique address any memory location can be reached in
Main Memory
The memory unit that communicates directly within the CPU, Auxillary memory and Cache memory,
is called main memory. It is the central storage unit of the computer system. It is a large and fast
memory used to store data during computer operations. Main memory is made up
of RAM and ROM, with RAM integrated circuit chips holing the major share.
Auxiliary Memory
Devices that provide backup storage are called auxiliary memory. For example: Magnetic disks and
tapes are commonly used auxiliary devices. Other devices used as auxiliary memory are magnetic
drums, magnetic bubble memory and optical disks.
It is not directly accessible to the CPU, and is accessed using the Input/Output channels.
Cache Memory
The data or contents of the main memory that are used again and again by CPU, are stored in the
cache memory so that we can easily access that data in shorter time.
Whenever the CPU needs to access memory, it first checks the cache memory. If the data is not
OPERATING SYSTEM:
It is a software that works as an interface between a user and the computer hardware.
The primary objective of an operating system is to make computer system convenient to
use and to utilize computer hardware in an efficient manner.
The operating system performs the basic tasks such as receiving input from the keyboard,
processing instructions and sending output to the screen.
Examples of operating system are UNIX, MS-DOS, MS-Windows - 98/XP/Vista, Windows-
NT/2000, OS/2 and Mac OS.
Serial Processing:In Serial Processing operating system that use FIFO (First in First Out)
technique for processing the process.
Batch Processing:In batch processing a similar type of jobs prepared and processed.
Real Time System: Real Time System are used there Requires higher and Timely Response.
Distributed Operating System: In this Operating system Data is Stored and Processed on
Multiprocessing: In This type of operating system there are two or More CPU in a SingleOS.
Assembler: A computer will not understand any program written in a language, other than its
machine language. The programs written in other languages must be translated into the machine
language. Such translation is performed with the help of software. A program which
translates an assembly language program into a machine language program is
called an assembler. If an assembler which runs on a computer and produces the machine
codes for the same computer then it is called self assembler or resident assembler. If an
assembler that runs on a computer and produces the machine codes for other computer then it is
called Cross Assembler.
Compiler: It is a program which translates a high level language program into a machine
language program. A compiler is more intelligent than an assembler. It checks all kinds of limits,
ranges, errors etc. But its program run time is more and occupies a larger part of the memory. It
has slow speed. Because a compiler goes through the entire program and then translates the
entire program into machine codes. If a compiler runs on a computer and produces the machine
codes for the same computer then it is known as a self compiler or resident compiler. On the
other hand, if a compiler runs on a computer and produces the machine codes for other computer
then it is known as a cross compiler.
Linker: In high level languages, some built in header files or libraries are stored. These libraries
are predefined and these contain basic functions which are essential for executing the program.
These functions are linked to the libraries by a program called Linker. If linker does not find a
library of a function then it informs to compiler and then compiler generates an error. The
compiler automatically invokes the linker as the last step in compiling a program.
Not built in libraries, it also links the user defined functions to the user defined
libraries. Usually a longer program is divided into smaller subprograms called modules. And
these modules must be combined to execute the program. The process of combining the modules
is done by the linker.
Loader: Loader is a program that loads machine codes of a program into the system
memory. In Computing, a loader is the part of an Operating System that is responsible for
Idea of Algorithm:
Examples of Algorithm:
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
2. Write an algorithm to find the largest among three different numbers entered by
user.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D←b2-4ac
Step 4: If D≥0
r1←(-b+√D)/2a
r2←(-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp←b/2a
ip←√(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Step 1: Start
Step 2: Declare variables n,factorial and i.
Step 3: Initialize variables
factorial←1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorial←factorial*i
5.2: i←i+1
Step 6: Display factorial
Step 7: Stop
Step 1: Start
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term←0 second_term←1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term≤1000
5.1: temp←second_term
5.2: second_term←second_term+first term
5.3: first_term←temp
5.4: Display second_term
Step 6: Stop
FLOW CHART:
1. Draw flowchart to find the largest among three different numbers entered by user.
Pseudocode is an artificial and informal language that helps programmers develop algorithms.
Pseudocode is a "text-based" detail (algorithmic) design tool.
The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be
indented. These include while, do, for, if, switch.
Advantages of pseudocode –
• it enables the programmer to concentrate only on the algorithm part of the code development.
• It cannot be compiled into an executable program. Example, Java code : if (i < 10) { i++; } pseudocode
:if i is less than 10, increment i by 1.
EXAMPLES:
Example 1: Write pseudo code that reads two numbers and multiplies them together and print out
their product.
Read isfive
If(isfive = 5)
Write "your number is 5"
Else if (isfive = 6)
Write "your number is 6"
Else
Write "your number is not 5 or 6"
Programming Basics:
Structure of C program:
main( ) Every C program must have a main() function which is the starting
{ point of the program execution.
First C Program:
#include<stdio.h>
Int main()
{
printf(“Hello World\n”);
return 0;
}
#include<stdio.h>
With this line of code we include a file called stdio.h. (Standard Input/Output header file). This file lets us use certain
commands for input or output which we can use in our program. (Look at it as lines of code commands) that have
been written for us by someone else). For instance it has commands for input like reading from the keyboard and
output commands like printing things on the screen.
int main()
The int is what is called the return value (in this case of the type integer). Where it used for will be explained further
down. Every program must have a main(). It is the starting point of every program. The round brackets are there for a
reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.
{}
The two curly brackets (one in the beginning and one at the end) are used to group all commands together. In this
case all the commands between the two curly brackets belong to main(). The curly brackets are often used in the C
printf(“Hello World\n”);
The printf is used for printing things on the screen, in this case the words: Hello World. As you can see the data that is
to be printed is put inside round brackets. The words Hello World are inside inverted ommas, because they are what is
called a string. (A single letter is called a character and a series of characters is called a string).
return 0;
When we wrote the first line “int main()”, we declared that main must return an integer int main(). (int is short for
integer which is another word for number). With the command return 0; we can return the value null to the operating
system. When you return with a zero you tell the operating system that there were no errors while running the
program.
Errors in C
Error is an illegal operation performed by the user which results in abnormal working of the
program. Programming errors often remain undetected until the program is compiled or
executed. Some of the errors inhibit the program from getting compiled or executed. Thus
errors should be removed before compiling and executing.
Type of errors:
1.Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known
as syntax errors. This compiler error indicates something that must be fixed before the code can be
compiled. All these errors are detected by compiler and thus are known as compile-time errors.
Most frequent syntax errors are:
Missing Parenthesis (})
Printing the value of variable without declaring it
Missing semicolon like this:
2. Logical Errors : On compilation and execution of a program, desired output is not obtained
when certain input values are given. These types of errors which provide incorrect output but
appears to be error free are called logical errors. These are one of the most common errors done by
beginners of programming.
These errors solely depend on the logical thinking of the programmer and are easy to detect if we
follow the line of execution and determine why the program takes that path of execution.
int main()
{
int i = 0;
No output
3.Run-time Errors : Errors which occur during program execution(run-time) after successful
compilation are called run-time errors. One of the most common run-time error is division by zero
also known as Division error. These types of error are hard to find as the compiler doesn’t point to
the line at which the error occurs.
// C program to illustrate
// run-time error
#include<stdio.h>
void main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
4. Semantic errors : This error occurs when the statements written in the program are not
meaningful to the compiler.
void main()
{
int a, b, c;
a + b = c; //semantic error
}
Error
source code is written by programmers for input into a translator program (compiler or
assembler).
Object code is what you get when you compile a single source code module into
machine code.
object code is what is produced by the translator and it contains machine code and
linking information to be used as input into another program called a link editor
(linker).
Executable code is the result of linking all of the various object code modules
together - and this file can then be loaded into memory by the operating system
when the program is “executed”.
Executable code is what is produced by the link editor. this is a fully executable
program image (hence the “executable” part) for the OS that you are using.
1. The character set: Any alphabet ,digit or special symbol ,used to represent information is denoted
by character. The character in C are grouped into four categories:-
i) Letters A...Z and a...z
ii) Digits 0,1,2,.....9
iii) Special Symbol ~ ` ! @ # $ % ^ & * () . < > ? / " : ; { }[ ].
iv) White Space, blank space, Carriage return, form feed, newline, horizontal tab.
2. The data types: The power of the programming language depends, among other thing, on the
range of different types of data it can handle. We can use several basic types of data values such as
character, integer, float, double.
3. Constants: A constant is a fix value that doesn't change while program execution.There are
basically 2 types of constants
i) Primary constants:- Integer constants, Real constants, Character constaints.
ii) Secondary constants:- Array, Structure, Union, Pointer, etc.
Character constants are enclosed between single quotes. e.g. 'a' & '%' etc.
4. Variable: A variable is an entity whose value can change during program execution.Variable
names are names given to locations in memory. These locations can contain integer, real or character
constants.
ii) The first character in the variable name must be an alphabet or underscore.
iv) No special symbol other than an underscore can be used in a variable name.
5. Keywords: Keywords are the words whose meaning has already been explained to the C
compiler.The keywords cannot be used as variable names because if we do so we are trying to assign
a new meaning to the keyword, which is not allowed by the computer. The keywords are also called
reserved words.
auto, double, int, struct, break, else, long, switch, case, enum, register, typedef, char, extern, return,
union, const, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile, do, if,
static, while,
(b)
(c)
counter = 5;
balance = counter * 10;
cout << counter << endl;
a. The C++ code that defines and initializes a variable named "counter."
Storage Classes:
A storage class defines the scope (visibility) and life-time of variables and/or functions within a
C Program. They precede the type that they modify. We have four different storage classes in a
C program −
auto
register
static
extern
Automatic Register External Storage
Features Static Storage Class
Storage Class Storage Class Class
Keyword auto register static extern
Initial
Garbage Garbage Zero Zero
Value
Storage Memory CPU register Memory Memory
scope
scope limited,local scope limited,local to
Scope limited,local to Global
to block block
block
limited life of limited life of value of variable persist
Global,till the
Life block,where block,where between different
program execution
defined defined function calls
Memory
Stack Register memory Segment Segment
location