GE6151 COMPUTER PROGRAMMING
description
Transcript of GE6151 COMPUTER PROGRAMMING
GE6151
COMPUTER PROGRAMMING
GE6151 COMPUTER PROGRAMMING
UNIT I INTRODUCTION 8Generation and Classification of Computers- Basic Organization of a Computer –Number System –
Binary – Decimal – Conversion – Problems. Need for logical analysis and thinking – Algorithm –Pseudo code – Flow Chart.
UNIT II C PROGRAMMING BASICS 10Problem formulation – Problem Solving - Introduction to ‘ C’ programming –fundamentals –
structure of a ‘C’ program – compilation and linking processes – Constants, Variables – Data Types – Expressions using operators in ‘C’ – Managing Input and Output operations – Decision Making and Branching – Looping statements – solving simple scientific and statistical problems.
UNIT III ARRAYS AND STRINGS 9Arrays – Initialization – Declaration – One dimensional and Two dimensional arrays. String- String
operations – String Arrays. Simple programs- sorting- searching – matrix operations.
UNIT IV FUNCTIONS AND POINTERS 9Function – definition of function – Declaration of function – Pass by value – Pass by reference –
Recursion – Pointers - Definition – Initialization – Pointers arithmetic – Pointers and arrays- Example Problems.
UNIT V STRUCTURES AND UNIONS 9Introduction – need for structure data type – structure definition – Structure declaration – Structure
within a structure - Union - Programs using structures and Unions – Storage classes, Pre-processor directives.
UNIT I INTRODUCTION
Generation and Classification of Computers- Basic
Organization of a Computer –Number System – Binary –
Decimal – Conversion – Problems. Need for logical analysis
and thinking – Algorithm –Pseudo code – Flow Chart.
INTRODUCTION
INTRODUCTION
Early days people use fingers for computing purpose.
As years go, the computing needs also grew. This leads to the
invention of calculators and computers.
The term computer is derived from the word compute. The word
compute means to calculate
Definition A Computer is an electronic machine that accepts data from the user,
processes the data by performing calculations and operations on it,
and generates the desired output results.
Computer performs both simple and complex operations, with speed
and accuracy.
COMPUTERINPUT OUTPUT
Definition
1.”Computer is an electronic Device that is used for
performing calculations and controlling operations
that can be expressed either in logical or numerical
terms”.
2.”Computer is an electronic device which
automatically accepts and stores input data process
them and produce the desired result”.
3.“Computer may be defined as an electronic device,
that operates upon information or data”.
Basic functions or operationsInputProcessingOutputStoringControlling
Applications of computerBusiness IndustryHomeEducationPrinting & publishingEntertainment etc.,
CHARACTERISTICS OF COMPUTERSpeedAccuracyDiligenceVersatilityResource sharingStorage
CHAPTER 1
Generation and Classification of
Computers
Generations of Computers (Cont)
First Generation Computers:
These computers were vacuum tube based machines.
They used magnetic drums for memory.
Input were fed into the computer using Punched cards
The size of these computers were very large and it produce
more heat.
They lacked in versatility and speed.
They were more expensive.
Figure . Vacuum tube
Generations of Computers (Cont)Second Generation Computers
Here the Transistor replaced the bulky vacuum tubes.
Transistors are smaller than vacuum tubes and have
higher operating speed.
Thus the size of the computer got reduced considerably.
Manufacturing cost was also very low.
Fig. Transistors
Generations of Computers (Cont)
Third Generation Computers
These computers were based on Integrated Circuits (ICs) Technology.
A single IC has many transistors, registers and capacitors built on a
single thin slice of silicon.
So that the size of the computer got further reduced.
These Computers were small in size, low cost, large memory and
processing speed is very high.
Fig .IC CHIPS
Generations of Computers (Cont)Fourth Generation Computers
It uses large scale Integrated Circuits (LSIC) built on a single silicon
chip called microprocessors .
Later very large scale Integrated Circuits (VLSIC) replaced LSICs
These computers are called microcomputers.
Thus the size of the computer got reduced.
The personal computer (PC) are comes under the Fourth Generation.
Fig. Microprocessors
Generations of Computers (Cont)
Fifth Generation Computer
The speed is extremely high in fifth generation computer.
The concept of Artificial intelligence has been introduced to
allow the computer to take its own decision.
It is still in a developmental stage .
Classification of Computer
Classification of Computer •The digital computers that are available nowadays vary in their sizes
and types.
•The computers are broadly classified into four categories (Figure 1.8)
based on their size and type:
(1) Microcomputers
(2) Minicomputers
(3) Mainframe computers
(4) Supercomputer
Figure 1.8 . Classification of computers based on size and type
Slow Cheap Simple Small
Fast Expensive Complex Large
Microcomputer• Microcomputers are small, low cost and single-user digital computer
• consist of CPU,input unit, output unit, storage unit and the software.
• stand alone machines-can be connected together to create a network
Example:
• IBM PC based on Pentium microprocessor and Apple Macintosh
• Microcomputers include
desktop computers
notebook computers or laptop,
tablet computer
handheld computer
smart phones and netbook
PC
Figure 1.9 Microcomputers
Laptop
Smart phone
Netbook
Tablet PDA
Mini Computer
•This is designed to support more than one user at a time
•It possesses large storage capacity and operates at a higher speed
•This type of computer is generally used for processing large volume of
data in an organization.
Eg: Servers in Local Area Networks (LAN).
Mini computer
Mainframe Computers•They operate at very high speed, having very large storage capacity and
can handle the work load of many users
•They are generally used in centralized databases.
Mainframe computer
Supercomputers •They are the fastest and most expensive machines
•They have high processing speed compared to other computers
•They have also multiprocessing technique
• Supercomputers are mainly being used for whether forecasting,
biomedical research, Space Research and other areas of science and
technology
Supercomputer
CHAPTER2
BASIC ORGANISATION OF
COMPUTER
CONTROL
MEMORY
ALU
OUTPUTINPUT
Basic organization of computer
Basic organization of computer
INPUTThe input unit is used to information or instruction to the
computer.It accept the information or instruction from user or from
some where else.Convert it to a computer understandable form and send it to
the computer.
Eg:Keyboard,mouse,Joystick,MICR, etc,.
Basic organization of computer (cont)
CENTRAL PROCESSING UNIT (CPU)
It is the heart of the computer.
It performs all operations.
It contains the followingsCONTROL UNIT
ALU
MEMORY
Basic organization of computer (cont)
CONTROL UNIT
It controls all other units in the computer.
It directs the sequence in which operations to be performed.
It also controls the flow of data between various units.
Basic organization of computer (cont)
MEMORY UNIT
Place for holding the information.
Types
1.Primary
2.Secondary
Primary memory is used to store temporary data.
Eg:RAM,ROM etc,.
Secondary memory is used to store information permanently.
Eg:Hard disk.
Basic organization of computer
OUTPUT
The output unit is used to display the result of the process.
Eg:Monitor,printer,speakers, etc,.
CHAPTER 3
NUMBER SYSTEM
Data Representation
•The data stored in the computer may be of different kinds, as follows:
•Numeric data (0, 1, 2, ... , 9)
•Alphabetic data (A, B, C, ... , Z)
•Alphanumeric data-Combination of any of the symbols-(A, B, C ..
Z),
(0,1..9),or special characters (+,-, Blank), etc.
•All kinds of data, be it alphabets, numbers, symbols, sound data or video
data, is represented in terms of 0s and 1s, in the computer.
•Each symbol is represented as a unique combination of 0s and 1s.
Number system• A number system in base r or radix r uses unique symbols for r digits. One or
more digits are combined to get a number.
• base of the number decides the valid digits that are used to make a number.
• In a number, the position of digit starts from right-hand side of the number.
•Rightmost digit has position 0, the next digit on its left has position 1, and so
on.
•The digits of a number have two kinds of values:
• Face value• Position value
•The face value of a digit is the digit located at that position.
•For example, in decimal number 52, face value at position 0 is 2 and face value
at position 1 is 5.
•The position value of a digit is (base Position). For example, in decimal
number 52, the position value of digit 2 is 10° and the position value of digit 5
is 101. Decimal numbers have a base of 10.
•The number is calculated as the sum of, face value * base position, of each of
the digits
•For decimal number 52, the number is 5*101 + 2*10° = 50 + 2 = 52
•In computers, we concerned with four kinds of number systems, as follows:
• Decimal Number System -Base 10
• Binary Number System -Base 2
• Octal Number System - Base 8
• Hexadecimal Number System -Base 16
Number SystemNumber Base B => B symbols
Base 16 (Hexa) : 0, 1,……9, A ,…, E, F
Base 10 (Decimal) : 0, 1, 2,……, 7, 8, 9
Base 8(Octal) : 0, 1, 2, 3, 4, 5, 6, 7
Base 2 (Binary) : 0, 1
Number System (cont)
Number representation:
• d31d30 ... d2d1d0 is a 32 digit number
• value = d31x B31 + d30 x B30 + ... + d2 x B2 + d1 x B1 + d0
x B0
Decimal Numbers: Base 10
Base or Radix is 10
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Example:
41210 = 4x102 + 1x101 + 2x100
= 400 + 10 +2
= 412
Binary Numbers: Base 2
Base or Radix is 2
Digits: 0, 1
Example:
1012 = 1x22 + 0x21 + 1x20
= 4 + 0 +1
= 5
Octal Numbers: Base 8
Base or Radix is 8
Digits: 0, 1, 2, 3, 4, 5, 6, 7
Example:
1238 = 1x82 + 2x81 + 3x80
= 64 + 16 +3
= 83
Hexadecimal Numbers: Base 16
Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F:
A 10
B 11
C 12
D 13
E 14
F 15
Example:
1216 = 1x161 + 2x160
= 16 +2
= 18
Conversions
Decimal to Base N Base N to Decimal
Successive Division by N(Remainder Method)
Multiplication with power of N
Decimal to Binary
Conversion of 2510 to Binary
2 252 12 -12 6 - 02 3 - 0
1 - 1
2510=110012
Binary to decimal conversion
11001 1X20 = 1 0X21 = 0 0X22 = 0 1X23 = 8 1X24 = 16
2510
Decimal to Octal
Conversion of 12510 to Octal
8 1258 15 - 5
1 - 7
12510=1758
Octal to decimal conversion
175 5X80 = 5 7X81 = 56 1X82 = 64
125
1758 = 12510
Conversion of 8410 to Octal8 848 10 - 4
1 - 2
8410=1248
Octal to decimal conversion
124 4X80 = 4 2X81 = 16 1X82 = 64
84
1248 = 8410
Decimal to Hexa
Conversion of 45010 to Hexadecimal
16 45016 28 - 2
1 - 12
45010=1C216
Hexa to decimal conversion
1C2 2X160 = 2 12X161 =
192 1X162 = 256
450
1C216 = 45010
Conversion of 38510 to Hexadecimal
16 38516 24 - 1
1 - 8
38510=18116
Hexa to decimal conversion
181 1X160 = 1 8X161 = 128 1X162 = 256
385
18116 = 38510
Other ConversionsBINARYOCTALBINARYHEXAOCTALHEXA etc,.
BINARYOCTALOCTAL – BASE 8 = 23
3 DIGIT BINARY NUMBERExample:110012 OCTAL 11 001
011 001 3 1 110012=318
Example:11100112 OCTAL
1 110 011
001 110 011 1 6 3
11100112=1638
BINARYHEXAHEXA – BASE 16 = 24
4 DIGIT BINARY NUMBERExample:110012 HEXA 1 1001
0001 1001 1 9 110012=1916
Example:11100112 OCTAL
1 110 011
001 110 011 1 6 3
11100112=1638
OCTALHEXAOCTALBINARYHEXAExample:318 HEXA
3 1 011 001
0001 1001 1 9318=1916
HEXA OCTALExample: 1BD OCTAL
1 B D0001 1011 1101
000 110 111 101 0 6 7 5
1BD = 6758
Conversion of 25.12510 to Binary
2 252 12 -12 6 - 02 3 - 0
1 - 1
2510=110012
0.125 X 2 = 0.2500.250 X 2 = 0.5 0.5 X 2 = 1.0
25.12510 = 11001.0012
Binary to decimal conversion11001.001 1X2-3 = 0.125 0X2-2 = 0 0X2-1 = 0 1X20 = 1 0X21 = 0 0X22 = 0 1X23 = 8 1X24 = 16 25.12510
Conversion of 84.25010 to Octal
8 848 10 - 4
1 - 2
8410=1248
0.250 X 8 = 2.000
25.12310 = 124.28
Octal to decimal conversion
124.2
2X8-1 = 0.25 4X80 = 1 2X81 = 0 1X82 = 0
25.25010
ASSIGNMENT
DECIMAL TO BINARY4379107241476
DECIMAL TO OCTAL391522847582534
DECIMAL TO HEXA75826312873594756
DECIMAL TO OTHER63 BASE4279 BASE6161 BASE7
BINARY OCTAL,HEXA1010101110101010111110101101111011110111101000101
OCTAL HEXA473123572457744625
75.12510 BASE2356.52310 OCTAL527.4210 HEXA
CHAPTER 4NEED FOR LOGICAL ANALYSIS AND
THINKING
NEED FOR LOGICAL ANALYSIS AND THINKING •Logical thinking is the process of understanding something; we put together
our judgment, reasoning, and other forms of thinking to reflect the reality of
knowledge.
•Logics and reasoning are also known as concepts of understanding something
logically.
•It is recognized as a structure of thinking and the analysis of the appearance
and development of something.
•Only through logical thinking, we can achieve the specific goals or targets.
•It is the advanced stage of understanding something, different thoughts may
apply on the same problem to get the specific task to be done.
•It is characterized by abstract, emotional, material, and analytical thinking.
•When finalizing the goal or target of something, we have put together our
judgment and reasoning, indirectly reflecting reality.
•Our brain is powerful in logical thinking, this enables us to scientifically
expose the essence of something in an abstract and logical way with our
consciousness.
•Logic is the basic form of the concept, judgment and reasoning. Logical
ways of thinking are mainly induction, deduction, analysis and synthesis, from
abstract to the concrete.
•Logic is also known as abstract thinking, a higher form of thinking. Its
characteristics are abstract concepts, judgment and reasoning as basic forms of
thinking, such as analysis, synthesis, generalization, and so on. Thereby, it helps
to expose the essence of the characteristics and laws of the things that we
observe.
•The same logic applies to in computer programming.
ALGORITHM
ALGORITHM •Algorithm is an ordered sequence of finite, well defined, unambiguous
instructions for completing a task.
•Algorithm is an English-like representation of the logic which is used to
solve the problem.
•It is a step-by-step procedure for solving a task or a problem.
•The steps must be ordered, unambiguous and finite in number.
•For accomplishing a particular task, different algorithms can be written.
•The different algorithms differ in their requirements of time and space.
•The programmer selects the best-suited algorithm for the given task to be
solved.
•Let's now look at two simple algorithms to find the greatest among three
numbers, as follows:
Algorithm to find the greatest among three numbers:
ALGORITHM 1
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B. If A is greater perform step 4 else perform step 5.
Step 4: Compare A and C. If A is greater, output "A is greatest" else output "C
is greatest". Perform step 6.
Step 5: Compare Band C. If B is greater, output "B is greatest" else output "C
is greatest".
Step 6: Stop
ALGORITHM 2
Step 7: Start
Step 8: Read the three numbers A, B, C
Step 9: Compare A and B. If A is greater, store A in MAX, else store B in MAX.
Step 10: Compare MAX and C. If MAX is greater, output "MAX is greatest"
else output "C is greatest".
Step 11: Stop
•Both the algorithms accomplish the same goal, but in different ways. The
programmer selects the algorithm based on the advantages and disadvantages of
each algorithm.
•For example, the first algorithm has more number of comparisons, whereas in
the second algorithm an additional variable MAX is required.
FLOWCHART
FLOWCHART• A flowchart is a diagrammatic representation of the logic for solving a
task.
• drawn using boxes of different shapes with lines connecting them to show
the flow of control.
• The purpose of drawing a flowchart is to make the logic of the program
clearer in a visual form.
•There is a famous saying "A photograph is equivalent to thousand
words". The same can be said of flowchart.
•The logic of the program is communicated in a much better way
•Since it is a diagrammatic representation-forms a common medium of
communication.
Flowchart Symbols
Sequence Selection
Control structures in flowchart
Examples of flowchart
1. The first flowchart computes the product of any two numbers and gives
the result. flowchart is a simple sequence of steps to be performed in a
sequential order.
2. The second flowchart compares three numbers and finds the maximum of
the three numbers. This flowchart uses selection. In this flowchart,
decision is taken based upon a condition, which decides the next path to
be followed, i.e. If A is greater than B the true (Yes) path is followed else
the false (No) path is followed. Another decision again made while
comparing MAX with C.
3. The third flowchart finds the sum of first 100 integers. Here, iteration
(loop) is formed so that some steps are executed repetitively until they
fulfill some condition exit from the repetition.
• In the decision box, the value of I is compared with 100. If false (No), a
loop is created which breaks when the condition becomes true (Yes).
• Flowcharts have their own benefits; however, they have some
limitations too.
• A com and long flowchart may run into multiple pages, which becomes
difficult to understand follow. Moreover, updating a flowchart with the
changing requirements is a challenging job.
PSEUDO CODE
Pseudo code •Pseudo code consists of short, readable and formally-styled English language
used for explaining an algorithm.
•Pseudo code does not include details like variable declarations, subroutines
•Pseudo code is a short-hand way of describing a computer program.
•Using pseudo code, it is easier for a programmer or a non-programmer to
understand the general working of the program, since it is not based on any
programming language.
•It is used to give a sketch of the structure of the program, before the actual
coding. It uses the structured constructs of the programming language but is
not machine readable•Pseudo code cannot be compiled or executed
Preparing a Pseudo Code 1.Pseudo code is written using structured English.
2.In a pseudo code, some terms are commonly used to represent the
various actions.
• Input (INPUT, GET, READ)
• output (OUTPUT, PRINT, DISPLAY)
• Calculations (COMPUTE, CALCULATE)
• incrementing (INCREMENT)
• ADD, SUBTRACT, INITIALIZE used for addition, subtraction, and
initialization, respectively.
3. Figure 2.5 shows the different pseudo code structures.
• The sequence structure is simply a sequence of steps to be executed in linear order.
• There are two main selection constructs
• if-statement
• case statement
• In the if-statement, if the condition is true then the THEN part is executed otherwise
the ELSE part is executed. There can be variations of the if-statement also, like
there may not be any ELSE part or there may be nested ifs.
• The case statement is used where there are a number of conditions to be checked. In
a case statement, depending on the value of the expression, one of the conditions is
true, for which the corresponding statements are executed. If no match for the
expression occurs, then the OTHERS option which is also the default option, is
executed.
Control structures for pseudo code
Examples of pseudo code
THANK YOU