Nothing Special   »   [go: up one dir, main page]

PPS-1 Unit - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

PPS-1

I-B.TECH I-SEM ,VJIT


Introduction to Computer
Computer:
A computer is an electronic device, operating under the control of
instructions stored in its own memory that can accept data (input), process
the data according to specified rules, produce information (output), and
store the information for future use .
Functionalities of a computer:
Any digital computer carries out five functions in gross terms:
▪ Takes data as input.
▪ Stores the data/Instructions in its memory and use them when required
▪ Processes the data and converts it into useful information.
▪ Generates the output
▪ Controls all the above four steps
Computer Components:
Any kind of computers consists of HARDWARE AND SOFTWARE.

Computer System

Hardware Software

Hardware:
Computer hardware is the collection of physical elements that
constitutes a computer system. Computer hardware refers to the
physical parts or components of a computer such as the monitor,
mouse, keyboard, computer data storage, hard drive disk (HDD),
system unit (graphic cards, sound cards, memory, motherboard and
chips), etc. all of which are physical objects that can be touched.
Examples of Hardware are the following −
• Input devices − keyboard, mouse, etc.
• Output devices − printer, monitor, etc.
• Secondary storage devices − Hard disk, CD, DVD, etc.
• Internal components − CPU, motherboard, RAM, etc.
Input Unit
This unit contains devices with the help of which we enter
data into the computer. This unit creates a link between the
user and the computer. The input devices translate the
information into a form understandable by the computer.
Output Unit:
The output unit consists of devices with the help of which we
get the information from the computer. This unit is a link
between the computer and the users. Output devices translate
the computer's output into a form understandable by the users.
CPU (Central Processing Unit):
CPU is considered as the brain of the computer. CPU performs
all types of data processing operations. It stores data,
intermediate results, and instructions (program). It controls the
operation of all parts of the computer.
CPU itself has the following three components −
• ALU (Arithmetic Logic Unit)
• Memory Unit
• Control Unit
Memory is primarily of three types −
• Cache Memory
• Primary Memory/Main Memory
• Secondary Memory
Primary Memory (Main Memory)
• Primary memory holds only those data and instructions on which the computer is
currently working. It has a limited capacity and data is lost when power is switched
off. It is generally made up of semiconductor device. These memories are not as fast
as registers. The data and instruction required to be processed resides in the main
memory. It is divided into two subcategories RAM and ROM.
Secondary Memory
• This type of memory is also known as external memory or non-volatile. It is slower
than the main memory. These are used for storing data/information permanently. CPU
directly does not access these memories, instead they are accessed via input-output
routines. The contents of secondary memories are first transferred to the main
memory, and then the CPU can access it. For example, disk, CD-ROM, DVD, etc.
Cache Memory
• Cache memory is a very high speed semiconductor memory which can speed up the
CPU. It acts as a buffer between the CPU and the main memory. It is used to hold
those parts of data and program which are most frequently used by the CPU. The
parts of data and programs are transferred from the disk to cache memory by the
operating system, from where the CPU can access them.
SOFTWARE :
Software is a set of programs, which is designed to perform a well-defined
function. A program is a sequence of instructions written to solve a particular
problem.
There are two types of software −
• System Software
• Application Software
System Software
The system software is a collection of programs designed to operate, control,
and extend the processing capabilities of the computer itself. System
software is generally prepared by the computer manufacturers. These
software products comprise of programs written in low-level languages,
which interact with the hardware at a very basic level. System software
serves as the interface between the hardware and the end users.
Some examples of system software are Operating System, Compilers,
Interpreter, Assemblers, etc.
Application Software
• Application software products are designed to satisfy a particular need of a
particular environment. All software applications prepared in the computer
lab can come under the category of Application software.
• Application software may consist of a single program, such as Microsoft's
notepad for writing and editing a simple text. It may also consist of a
collection of programs, often called a software package, which work together
to accomplish a task, such as a spreadsheet package.
• Examples of Application software are the following −
• Payroll Software
• Student Record Software
• Inventory Management Software
• Income Tax Software
• Railways Reservation Software
• Microsoft Office Suite Software
• Microsoft Word
• Microsoft Excel
• Microsoft PowerPoint
Types of Languages
There are three main kinds of programming language:
• Machine language
• Assembly language
• High-level language
Machine language: is - it's the language of machines, consisting
of bits (1s and 0s) put together into chunks like bytes, a group
of 8 bits, and lots of other larger sizes. It's highly unlikely you
will ever have to write in machine language but in the old days,
we used to plot 1s and 0s on graph paper
Assembly language: is a little easier than machine language, but
not much! It uses more convenient numbers, symbols, and
abbreviations to describe the huge strings of 1s and 0s, to make
it both easier and more memorable to type in instructions.
Translators
• A translator is a programming language processor that converts a computer
program from one language to another. It takes a program written in source
code and converts it into machine code. It discovers and identifies the error
during translation.
Purpose of Translator:
It translates high-level language program into a machine language program
that the central processing unit (CPU) can understand. It also detects errors
in the program.
Different Types of Translators
There are 3 different types of translators as follows:
Compiler:
A compiler is a translator used to convert high-level programming language to
low-level programming language. It converts the whole program in one
session and reports errors detected after the conversion. Compiler takes time
to do its work as it translates high-level code to lower-level code all at once
and then saves it to memory.
Interpreter:
Just like a compiler, is a translator used to convert high-level
programming language to low-level programming language. It
converts the program one at a time and reports errors detected at once,
while doing the conversion. With this, it is easier to detect errors than
in a compiler. An interpreter is faster than a compiler as it
immediately executes the code upon reading the code.
Assembler:
• An assembler is a translator used to translate assembly language to
machine language. It is like a compiler for the assembly language but
interactive like an interpreter. Assembly language is difficult to
understand as it is a low-level programming language. An assembler
translates a low-level language, an assembly language to an even
lower-level language, which is the machine code. The machine code
can be directly understood by the CPU.
• Introduction to Algorithms
The word Algorithm means “a process or set of rules to be
followed in calculations or other problem-solving operations”.
Therefore Algorithm refers to a set of rules/instructions that
step-by-step define how a work is to be executed upon in order
to get the expected results.
Advantages of Algorithms:
• It is easy to understand.
• Algorithm is a step-wise representation of a solution to a given
problem.
• In Algorithm the problem is broken down into smaller pieces
or steps hence, it is easier for the programmer to convert it into
an actual program.
Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-
consuming.
• Branching and Looping statements are difficult to show in
Algorithms.
How to Design an Algorithm?
Algorithm to add 2 numbers and print their sum:
Step 1- start

Step 2- Accept the first integer as input from the user(num1)

Step 3- Accept the second integer as input from the user(num2)

Step 4- calculate the sum of the two numbers


sum=num1+num2

Step 5- Display sum as the result

Step 6- stop
What is a Flowchart?
A flowchart is a graphical representations of steps. It was originated from
computer science as a tool for representing algorithms and programming
logic but had extended to use in all other kinds of processes.
A flowchart can also be used to define a process or project to be
implemented.
• Flowchart Example – Simple Algorithms
Advantages Of Flowchart
1. It is a convenient method of communication.
2. It indicates very clearly just what is being done, where a
program has logical complexities.
3. A key to correct programming.
4 .It is an important tool for planning and designing a new system.
5. It clearly indicates the role-played at each level.
6. It saves the inconveniences in future and serves the purpose of
documentation for a system.
7. It provides an overview of the system and also demonstrates the
relationship between various steps.
8. Facilitates troubleshooting.
9. It promotes logical accuracy.
10.It makes sure that no logical path is left incomplete without
any action being taken.
• Disadvantages Of Flowchart
• The flowchart is a waste of time and slows down the process
of software development.
• The flowchart is quite costly to produce and difficult to use
and manage.
• Flowcharts are not meant for man to computer communication.
• Sometimes the Complex logic of the program logic is quite
complicated to draw out on by using different defined shapes.
In that case, flowchart becomes complex and clumsy. This will
become a pain for the user, resulting in a waste of time and
money trying to correct the problem
• If you need to modify or alternate the process then it will be
very hard to do in the flowchart. Because either you will have
to erase the end of the flowchart or start.
• What is Pseudocode?
Pseudocode is an informal high-level description of a computer
program or algorithm. It is written in symbolic code which must be
translated into a programming language before it can be executed.
• Advantages of Pseudocode
• Improves the readability of any approach. It’s one of the best
approaches to start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or
flowchart. Also works as a rough documentation, so the program of
one developer can be understood easily when a pseudo code is
written out. In industries, the approach of documentation is
essential. And that’s where a pseudo-code proves vital.
• The main goal of a pseudo code is to explain what exactly each line
of a program should do, hence making the code construction phase
easier for the programmer.
Add Two Numbers Program Pseudocode
Algorithm
BEGIN
DEFINE : Integer num1,num2,result
DISPLAY: “enter the values of num1,num2:”
READ : num1,num2
COMPUTE: result= num1+num2
DISPLAY: result
END
Pseudocode to find the total and average of 3 subject marks

BEGIN
DEFINE : Integer m1,m2,m3,total
DEFINE : Real average
DISPLAY : “Enter the marks in three subjects:”
READ : m1,m2,m3
COMPUTE : total=m1+m2+m3
COMPUTE : average=(m1+m2+m3)/3
DISPLAY : total,average
END
What is C programming?
C is a general-purpose programming language that is extremely popular,
simple and flexible. It is machine-independent, structured programming
language which is used extensively in various applications.

• 'C' was developed by Dennis Ritchie in 1972.


• It is a robust language.
• It is widely used in the software development field.
• It is a procedure and structure oriented language.
• It has the full support of various operating systems and hardware
platforms.
• Many compilers are available for executing programs written in 'C'.
• A compiler compiles the source file and generates an object file.
• A linker links all the object files together and creates one executable
file.
• It is highly portable.
Features of C Programming Language:
C is a procedural programming language. It was initially developed by
Dennis Ritchie in the year 1972. It was mainly developed as a system
programming language to write an operating system. The main features of C
language include low-level access to memory, a simple set of keywords, and
clean style, these features make C language suitable for system programming
like an operating system or compiler development.
Features of C Programming Language:
• Procedural Language
• Fast and Efficient
• Modularity
• Statically Type
• General Purpose Language
• Rich set of built in Operators
• Libraries with rich Functions
• Middle Level Language
• Portability
• Easy to Extend
Applications of C programming Language
• C programming language can be used to design the system
software like operating system and Compiler.
• To develop application software like database and spread sheets.
• For Develop Graphical related application like computer and
mobile games.
• To evaluate any kind of mathematical equation use c language.
• C programming language can be used to design the compilers.
• UNIX Kernal is completely developed in C Language.
• C programming language can be used to design Operating
System.
• C programming language can be used to design Network
Devices.
• To design GUI Applications. Adobe Photoshop, one of the most
popularly used photo editors since olden times, was created with
the help of C.
STRUCTURE OF A C PROGRAM:
A C program is divided into different sections. There are six
main sections to a basic c program.
The six sections are:
• Documentation
• Link
• Definition
• Global Declarations
• Main functions
• Subprograms
• Documentation Section
The documentation section is the part of the program where the
programmer gives the details associated with the program. He
usually gives the name of the program, the details of the author
and other details like the time of coding and description.
Example
/* ADDING TWO PROGRAMS */
• Link Section
This part of the code is used to declare all the header files that
will be used in the program. This leads to the compiler being
told to link the header files to the system libraries.
Example
#include<stdio.h>
• Definition Section
In this section, we define different constants. The keyword define is used in this
part.
Ex: #define PI 3.14
• Global Declaration Section
This part of the code is the part where the global variables are declared. All the
global variable used are declared in this part. The user-defined functions are also
declared in this part of the code.
Ex: float area(float r);
• Main Function Section
Every C-programs needs to have the main function. Each main function contains 2
parts. A declaration part and an Execution part. The declaration part is the part
where all the variables are declared. The execution part begins with the curly
brackets and ends with the curly close bracket. Both the declaration and execution
part are inside the curly braces.
int main(void)
{
int a=10;
printf(" %d", a);
return 0; }
Sub Program Section:
All the user-defined functions are defined in this section of the program.
int add(int a, int b)
{
return a+b;
}
Sample Program
/* a program to calculate area of circle*/
#include<stdio.h> //link section
#define pi 3.14; //defination section
float area(float r); //global declaration
int main() //main function
{
float r;
printf(" Enter the radius:n");
scanf("%f",&r);
printf("the area is: %f",area(r));
return 0;
}
float area(float r)
{
return pi * r * r; //sub program
}
C Tokens
Tokens are the smallest elements of a program, which are
meaningful to the compiler.
A token is divided into six different types. Keywords, Operators, Strings,
Constants, Special Characters, and Identifiers.
C Keywords and Identifiers:
Character set:
A character set is a set of alphabets, digits and some special
characters that are valid in C language.
• Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C accepts both lowercase and uppercase alphabets as variables and
functions.
• Digits
0123456789
• Special Characters
( ) $ ] [ # ? % / &
• White space Characters
Blank space, newline, horizontal tab, carriage return and form feed.
• C Keywords
Keywords are predefined, reserved words used in programming
that have special meanings to the compiler. Keywords are part
of the syntax and they cannot be used as an identifier. For
example:
As C is a case sensitive language, all keywords must be written
in lowercase. Here is a list of all keywords allowed in ANSI C.
Ex: auto,break, if,for,do,switch,int,while,void ,goto etc.
• C Identifiers
Identifier refers to name given to entities such as variables,
functions, structures etc.
Identifiers must be unique. They are created to give a unique
name to an entity to identify it during the execution of the
program. For example:
int money; double accountBalance;
Rules for naming identifiers
• A valid identifier can have letters (both uppercase and
lowercase letters), digits and underscores.
• The first letter of an identifier should be either a letter or an
underscore.
• You cannot use keywords like int, while etc. as identifiers.
• There is no rule on how long an identifier can be. However,
you may run into problems in some compilers if the identifier
is longer than 31 characters.
TYPES OF CONSTANTS:
1.Integer constants
2.Real or Floating point constants
3.Character constants
4.String constants
5.Backslash character constants
INTEGER CONSTANTS IN C:
An integer constant must have at least one digit.
• It must not have a decimal point.
• It can either be positive or negative.
• No commas or blanks are allowed within an integer constant.
• If no sign precedes an integer constant, it is assumed to be positive.
• The allowable range for integer constants is -32768 to 32767
REAL CONSTANTS IN C:
• A real constant must have at least one digit
• It must have a decimal point
• It could be either positive or negative
• If no sign precedes an integer constant, it is assumed to be positive.
• No commas or blanks are allowed within a real constant.
.
CHARACTER CONSTANTS IN C:
• A character constant is a single alphabet, a single digit or a single special
symbol enclosed within single quotes.
• The maximum length of a character constant is 1 character.
EX:’5’,’X’,’a’
STRING CONSTANTS IN C:
• String constants are enclosed within double quotes.
• String constants are sequence of characters.
Ex: “hello”,”123”,”a”
BACKSLASH CHARACTER CONSTANTS IN C:
• There are some characters which have special meaning in C language.
• They should be preceded by backslash symbol to make use of special
function of them.
\b Backspace
\f Form feed
\n New line
\\ Backslash
C Variables
• In programming, a variable is a container (storage area) to hold data.
• To indicate the storage area, each variable should be given a unique name
(identifier). Variable names are just the symbolic representation of a
memory location. For example:
• int playerScore = 95;Here, playerScore is a variable of int type. Here, the
variable is assigned an integer value 95.
• The value of a variable can be changed, hence the name variable.
• char ch = 'a';
Rules for naming a variable
• Variable name must begin with letter or underscore.
• Variables are case sensitive
• They can be constructed with digits, letters.
• No special symbols are allowed other than underscore.
• sum, height, _value are some examples for variable name
DECLARING & INITIALIZING C VARIABLE:
• Variables should be declared in the C program before to use.
• Memory space is not allocated for a variable while
declaration. It happens only on variable definition.
• Variable initialization means assigning a value to the variable.
Declaration of Variable
Declaration of variable in c can be done using following
syntax: data_type variable_name;
(or)
data_type variable1,variable2,…,variable n;
where data_type is any valid c data type and variable_name is
any valid identifier.
• For example
int a; float a, b; char c;
Initialization of Variable:
C variables declared can be initialized with the help of assignment operator
‘=’.
Syntax
• data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;
Example:
int a=10;
int a=b+c;
a=10;
a=b+c;
Multiple variables can be initialized in a single statement by single value,
for example:
a=b=c=d=e=10;
• C variables must be declared before they are used in the c program. Also,
since c is a case-sensitive programming language, therefore the c variables,
abc, Abc and ABC are all different.
Variable Declaration and Variable Initialization in two steps:
#include<stdio.h>
void main()
{
int age; // Declaration of variable age
age = 20; // initialization of the variable age
printf("%d\n",age);
}
#include<stdio.h>
void main()
{
int age=20; // Declaration and initialization of variable age
printf("%d \n",age);
}
• Variable Declaration and Variable Initialization in one line:
Data Types in C
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
ANSI C provides three types of data types:
1.Primary(Built-in) Data Types:
int, char, double and float.
2.Derived Data Types:
Array, References, and Pointers.
3. User Defined Data Types:
Structure, Union, and Enumeration.
BASIC DATA TYPES IN C LANGUAGE:
1.INTEGER DATA TYPE:
• “int” keyword is used to refer integer data type.
• The storage size of int data type is 2 or 4 byte
• int (2 byte) can store values from -32,768 to +32,767
• Integer data type allows a variable to store numeric values.
Data Types Memory Size Range
Short 1 byte -128 to 127
signed short 1 byte -128 to 127
unsigned short 1 byte 0 to 255
Int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
2.FLOATING POINT DATA TYPE:
Floating point data type consists of 2 types. They are,
1. float
2.double
1. FLOAT:
• Float data type allows a variable to store decimal values.
• Storage size of float data type is 4. This also varies depend upon the processor in
the CPU as “int” data type.
• We can use up-to 6 digits after decimal using float data type.
• For example, 10.456789 can be stored in a variable using float data type.
2. DOUBLE:
• Double data type is also same as float data type which allows up-to 10 digits after
decimal.
• The range for double datatype is from 1E–37 to 1E+37.
Float 4 byte
Double 8 byte
long double 10 byte
3.CHARACTER DATA TYPE:
• Character data type allows a variable to store only one character.
• Storage size of character data type is 1. We can store only one
character using character data type.
• “char” keyword is used to refer character data type.
• For example, ‘A’ can be stored using char datatype. You can’t store
more than one character using char data type.
Data Types Memory Size Range
Char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1byte 0 to 255
Data Types and Variable Declarations in C
Example:
#include <stdio.h>
int main()
{
int a = 4000; // positive integer data type
float b = 5.2324; // float data type
char c = 'Z'; // char data type
long d = 41657; // long positive integer data type
long e = -21556; // long -ve integer data type
int f = -185; // -ve integer data type
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data type
float j = -3.55; // float data type
return 0;
}
C programming Input Output functions (I/O):
Console simply means screen and keyboard. There are two types of a
console I/O functions:
• Formatted input-output function
• Unformatted input-output function
formatted input/output functions:
1.scanf( ) function:
scanf( ) is the standard library function that is used for precise
input formatting.
In C programming, scanf() is one of the commonly used
function to take input from the user. The scanf() function reads
formatted input from the standard input such as keyboards.
syntax of scanf( ) function:
scanf(“ format-control-string”, &v1,&v2,...);
Ex: scanf(“%d”,&a);
scanf(“%d%f”,&a,&b);
2.printf( ) function:
printf( ) is the standard library function that is used for precise output
formatting.
In C programming, printf() is one of the main output function. The function
sends formatted output to the screen.
• syntax of printf( ) function:
printf(“ format-control-string”, v1,v2,----- );
Ex: printf(“hello world”); // pintf() with no argument
printf(“%d”,a);
printf(“%d%d”,a,b);
//Write a C Program to Get Input from User
#include<stdio.h>
void main()
{
int i;
printf("Enter a value: ");
scanf("%d",&i);
printf( "\nYou entered: %d",i);
}

You might also like