0% found this document useful (0 votes)
9 views

PPS_I_Moule_Notes

The document provides an introduction to programming concepts, focusing on computer system components, memory types, and the role of operating systems. It covers algorithms, flowcharts, and pseudocode, detailing their significance in programming and problem-solving. Additionally, it explains the functions of assemblers, compilers, interpreters, linkers, and loaders in the software development process.

Uploaded by

Ashish Sharma
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)
9 views

PPS_I_Moule_Notes

The document provides an introduction to programming concepts, focusing on computer system components, memory types, and the role of operating systems. It covers algorithms, flowcharts, and pseudocode, detailing their significance in programming and problem-solving. Additionally, it explains the functions of assemblers, compilers, interpreters, linkers, and loaders in the software development process.

Uploaded by

Ashish Sharma
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/ 21

Subject name: Programming for Problem Solving

Module – 1 : (Introduction to Programming)


Introduction to components of a computer system: Memory, processor, I/O Devices, storage,
operating system, Concept of assembler, compiler, interpreter, loader and linker.

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.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 1


Introduction to components of a computer system :

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.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 2


 Memory Hierarchy

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

Memory Access Methods


Each memory type, is a collection of numerous memory locations. To access data from any memory,
first it must be located and then the data is read from the memory location. Following are the
methods to access information from memory locations:

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 3


the same amount of time in any order.
2. Sequential Access: This methods allows memory access in a sequence or in order.
3. Direct Access: In this mode, information is stored in tracks, with each track having a
separate read/write head.

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.

 RAM: Random Access Memory


o DRAM: Dynamic RAM, is made of capacitors and transistors, and must be refreshed
every 10~100 ms. It is slower and cheaper than SRAM.
o SRAM: Static RAM, has a six transistor circuit in each cell and retains data, until
powered off.
o NVRAM: Non-Volatile RAM, retains its data, even when turned off. Example: Flash
memory.
 ROM: Read Only Memory, is non-volatile and is more like a permanent storage for
information. It also stores the bootstrap loader program, to load and start the operating system
when computer is turned on. PROM(Programmable ROM), EPROM(Erasable PROM)
and EEPROM(Electrically Erasable PROM) are some commonly used ROMs.

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 4


found in cache memory then the CPU moves onto the main memory. It also transfers block of recent
data into the cache and keeps on deleting the old data in cache to accomodate the new one.

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.

Different types of operating system


There are different types of operating system those are organized by their Working.

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.

Multi-Programming:In Multi programming Operating System Multiple Programs are Executed


on the System at a Time.

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 5


Multiple Locations.

Multiprocessing: In This type of operating system there are two or More CPU in a SingleOS.

ASSEMBLER / COMPILER / INTERPRETER / LINKER / LOADER:

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.

Interpreter: An interpreter is a program which translates statements of a program into machine


code. It translates only one statement of the program at a time. It reads only one statement of
program, translates it and executes it. Then it reads the next statement of the program again
translates it and executes it. In this way it proceeds further till all the statements are translated
and executed. On the other hand, a compiler goes through the entire program and then translates
the entire program into machine codes. A compiler is 5 to 25 times faster than an interpreter.

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 6


loading programs. It is one of the essential stages in the process of starting a program. Because it
places programs into memory and prepares them for execution. Loading a program involves
reading the contents of executable file into memory. Once loading is complete, the operating
system starts the program by passing control to the loaded program code. All operating systems
that support program loading have loaders. In many operating systems the loader is permanently
resident in memory.

Idea of Algorithm:

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations.


Algorithms are used for calculation, data processing, and automated reasoning.

Properties of the algorithm

1. Finiteness. An algorithm must always terminate after a finite number of steps.


2. Definiteness. Each step of an algorithm must be precisely defined; the actions to be carried out
must be rigorously and unambiguously specified for each case.
3. Input. An algorithm has zero or more inputs, i.e, quantities which are given to it initially before
the algorithm begins.
4. Output. An algorithm has one or more outputs i.e, quantities which have a specified relation to
the inputs.
5. Effectiveness. An algorithm is also generally expected to be effective. This means that all of the
operations to be performed in the algorithm must be sufficiently basic that they can in principle
be done exactly and in a finite length of time.

Examples of Algorithm:

1. Write an algorithm to add two numbers entered by user.

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.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 7


Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

3. Write an algorithm to find all roots of a quadratic equation ax2+bx+c=0.

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

4. Write an algorithm to find the factorial of a number entered by user.

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

5. Write an algorithm to check whether a number entered by user is prime or not.

Step 1: Start

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 8


Step 2: Declare variables n,i,flag.
Step 3: Initialize variables
flag←1
i←2
Step 4: Read n from user.
Step 5: Repeat the steps until i<(n/2)
5.1 If remainder of n÷i equals 0
flag←0
Go to step 6
5.2 i←i+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop

6. Write an algorithm to find the Fibonacci series till term≤1000.

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:

Flowchart is a diagrammatic representation of an algorithm. Flowchart are very helpful in writing


program and explaining program to others.

Symbols Used In Flowchart

Symbol Purpose Description

Used to indicate the flow of logic by connecting


Flow line
symbols.

Terminal(Stop/Start) Used to represent start and end of flowchart.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 9


Symbol Purpose Description

Input/Output Used for input and output operation.

Used for airthmetic operations and data-


Processing
manipulations.

Used to represent the operation in which there are


Desicion
two alternatives, true and false.

On-page Connector Used to join different flowline

Off-page Connector Used to connect flowchart portion on different page.

Predefined Used to represent a group of statements performing


Process/Function one processing task.

Examples of flowcharts in programming:-

1. Draw flowchart to find the largest among three different numbers entered by user.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 10


2.Draw a flowchart to find all the roots of a quadratic equation ax2+bx+c=0

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 11


PSUEDO CODE:

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 –

• Pseudocode is understood by the programmers of all types.

• 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 num1 , num2


Set multi to num1*num2
Write multi

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 12


Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.

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:

Following is the basic structure of a C program-


Documentation Consists of comments, some description of the program,
programmer name and any other useful points that can be
referenced later.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 13


Link Provides instruction to the compiler to link function from the
library function.

Definition Consists of symbolic constants.

Global Consists of function declaration and global variables.


declaration

main( ) Every C program must have a main() function which is the starting
{ point of the program execution.

Subprograms User defined functions.

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 14


language to group commands together. (To mark the beginning and end of a group or function.).

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.

The most common errors can be broadly classified as follows.

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:

// C program to illustrate syntax error //


#include<stdio.h>
void main()
{
int x = 10;
int y = 15;

printf("%d", (x, y)) // semicolon missed


}

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 15


Error:

error: expected ';' before '}' token

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.

// C program to illustrate logical error //

int main()
{
int i = 0;

// logical error : a semicolon after loop


for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 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.

For more understanding run the example given below.

// 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;

printf("resut = %d", div);


}

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 16


Error:

warning: division by zero [-Wdiv-by-zero]


div = n/0;

4. Semantic errors : This error occurs when the statements written in the program are not
meaningful to the compiler.

// C program to illustrate semantic error //

void main()
{
int a, b, c;
a + b = c; //semantic error
}

Error

error: lvalue required as left operand of assignment


a + b = c; //semantic error

Source Code ,Object Code & Executable Code:


 Source code is stuff that humans can read…in a language like C or something.

 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.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 17


Components of C language:

There are FIVE Components of C language:

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.

Rules for Constructing Variable Names –

i) A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers


allow variable names whose length could be up to 247 characters. Do not create unnecessarily long
variable names.

ii) The first character in the variable name must be an alphabet or underscore.

iii) No commas or blanks are allowed within a variable name.

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.

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 18


There are 32 keywords.

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,

Standard I/O functions in C:

SN Input Functions Output functions Details


1 For Reading,Writing single character.
getchar ( ) putchar()

2 For Reading,Writing a string.


gets ( ) puts ( )

3 getw ( ) putw ( ) For Reading,Writing single integer.

4 scanf ( ) printf ( ) For composite data types.

Variables and Memory Locations:


Variables are a named location in main memory that have several important characteristics: a
name, a content, and a memory address (it is customary to write addresses using hexadecimal
notation, e.g., 0xff456e). Variables also have a data type, but aside from determining how much
memory the compiler allocates for the variable, the type is not important for our current
discussion. Figure 1 presents a simple mental model of a variable that shows the relation between
the first three characteristics.

(a) int counter = 123;

(b)

(c)

counter = 5;
balance = counter * 10;
cout << counter << endl;

Various aspects and uses of a variable:

a. The C++ code that defines and initializes a variable named "counter."

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 19


b. A labeled abstract representation of how the variable "counter" appears in main memory.
From left to right:
i. the variable's name
ii. the variable's content
iii. the variable's address
c. A variable's name can refer to either its content or its address depending on where the
name appears in a statement.

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

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 20


void main() void add(); void main()
{ void main() void main() {
auto int i; { { extern int i;
printf("%d",i); register int i; add(); printf("%d",i);
} for(i=1; i<=5 ; add(); int i=5
i++); } }
printf("%d void add()
Example ",i); {
} static int i=1;
printf("\n%d",i);
i=i+1;
}
OUTPUT OUTPUT OUTPUT
OUTPUT
12345 1 5
124
2

Prepared By : R.K.Singh, HOD(CSE),ITM,Maharajganj -UP Page 21

You might also like