Unit 1 PSC Verified
Unit 1 PSC Verified
UNIT 1
INTRODUCTION TO COMPUTERS
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Agen
da
Problem Solving
Compilers Technique:
• Interprete Memory, Programming
Overview
Introducti rs and Variables, Logic,
of Number
on of Values, Sequence,
operating programm system Selection and
computer Instruction Repetition,
systems ing s. Flow chart-
Algorithm -
languages Pseudocode.
Introduction of
Computer
Our society depends upon computer systems and the people
who develop and maintain them.
3
What is Computer?
Speed
• A computer works with much higher speed and accuracy compared to humans while
performing mathematical calculations.
• Computers can process millions (1,000,000) of instructions per second.
• The time taken by computers for their operations is microseconds and nanoseconds.
Accuracy
• Computers perform calculations with 100% accuracy
• Errors may occur due to data inconsistency or inaccuracy
Versatility
• It refers to the capability of a computer to perform different kinds of works with same
accuracy and efficiency
• For example at a moment a computer can be used to draft a letter. Next moment it can be
used print a document or play a music file etc..,
Automation
• Computer perform all the tasks automatically i.e. it performs tasks without
manual intervention.
Storage
• A computer has built-in memory called primary memory where it stored data.
• Secondary storage are removable devices such as CD, pen drives, etc., which are
also used to store data.
• It uses very large scale Integrated Circuits (VLSI) built on a single silicon chip
called microprocessors .
• These computers are called microcomputers.
• Thus the size of the computer got reduced.
• The personal computer (PC) are comes under the Fourth Generation
Arithmetic Section
• Function of arithmetic section is to perform arithmetic operations like
addition, subtraction, multiplication, and division.
• All complex operations are done by making repetitive use of the above
operations.
Logic Section
• Function of logic section is to perform logic operations such as comparing,
selecting, matching, and merging of data.
OUTPUT
•
DEVICES
An output device is any piece of computer hardware equipment which converts
information into human-readable form. It can be text, graphics, tactile, audio, and video.
The commonly used output devices are:
What is an Operating system
• An Operating System (OS) is a software that acts as an interface between computer hardware
components and the user.
• Every computer system must have at least one operating system to run other programs.
Applications like Browsers, MS Office, Notepad Games, etc., need some environment to run
and perform its tasks.
• The name "compiler" is primarily used for programs that translate source code from a
high-level programming language to a lower level language (e.g. assembly language,
object code, or machine code) to create an executable program
1A
7
021
C
1A
7
• Naming Variables
• Declaring Variables
• Using Variables
• The Assignment Statement
Reading
x=a+b
z + 2 = 3(y - 5)
#define PI 3.14159
#define AGE 52
Case
• CSensitivity
is case sensitive
• It matters whether an identifier, such as a variable name, is
uppercase or lowercase.
• Example:
area
Area
AREA
ArEa
are all seen as
different
variables by
the compiler.
Which Are Legal
Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Declaring Variables
• Before using a variable, you must give the compiler
some information about the variable; i.e., you
must declare it.
• The declaration statement includes the data type
of the variable.
• Examples of variable declarations:
int meatballs ;
float area ;
Declaring Variables
• When we declare a variable
(con’t)
• Space is set aside in memory to hold a value of the specified
data type
• That space is associated with the variable name
• That space is associated with a unique address
• Visualization of the declaration
int meatballs ; float num;
meatballs
garbage
FE07
More About
C has three basic predefined data types:
Variables
• Integers (whole numbers)
• int, long int, short int, unsigned int
• Floating point (real numbers)
• float, double
• Characters
• char
• At this point, you need only be concerned with the data
types that are bolded.
Using Variables:
• Variables may be given initial values, or initialized, when
Initialization
declared. Examples:
length
int length = 7 ; 7
diamet
float diameter = 5.9 ; er 5.9
initial
char initial = ‘A’ ;
‘A’
Using Variables:
Initialization
(con’t)
• Do not “hide” the initialization
• put initialized variables on a separate line
• a comment is always a good idea
• Example:
int height ; /* rectangle height */
int width = 6 ; /* rectangle width
int area ; */
/* rectangle area */
NOT int height, width = 6,
area ;
Using Variables:
• Variables may have values assigned to them through the
Assignment
use of an assignment statement.
• Such a statement uses the assignment operator =
• This operator does not denote equality. It assigns
the value of
the right hand side of the statement (the
expression) to the variable on the left hand side.
• Examples:
diameter = 5.9 ;
area = length * width ;
Note that only single variables may appear on the left
hand side of the assignment operator.
Example: Declarations and
Assignments
#include <stdio.h> inche
sgarbage
int main( ) fee
tgarbage
{
fathom
int sgarbage
inches, feet,
fathoms
fathoms ; = 7 ; fathom
s 7
feet = 6 * fathoms ; fee
inches = 12 * feet ; t 42
inche
•
s 504
•
•
Example: Declarations and
Assignments (cont’d)
•
•
•
printf (“Its depth at sea: \
n”)(“; %d fathoms \n”, fathoms) ;
printf
printf (“ %d feet \n”, feet) ;
printf (“ %d inches \n”, inches) ;
return 0 ;
}
Enhancing Our Example
• What if the depth were really 5.75 fathoms? Our
program, as it is, couldn’t handle it.
• Unlike integers, floating point numbers can contain
decimal portions. So, let’s use floating point,
rather than integer.
• Let’s also ask the user to enter the number of
fathoms, rather than “hard-coding” it in.
How the instruction executes?
1/31/202
COMPUTER SCIENCE 8
5
Problem Solving Techniques
Problem solving arriving
at decisions based prior
knowledge and reasoning.
COMPUTER SCIENCE 90
1/202
Problem Solving Techniques
COMPUTER SCIENCE 91
1/202
Problem Solving Techniques
Flowchart Pseudocode
Selection Structure-if then
Selection Structure. Used to make a decision
or comparison and then, based on the result
of that decision or comparison, to select one
of two paths. The condition must result in
either a true (yes) or false (no) answer. If the
condition is true, the program performs one
set of tasks.
Ex:
If ..Then
If ..Then.. Else
Case Type
Selection Structure-if then
If ..Then
If ..Then.. Else
Case Type
Selection Structure-if
then
Selection Structure-if
then
If-
ThenIfThen
condition is True , the statements following
are executed
Flowchart
Pseudocode
Exam
ple
Start
Read a
yes
If a>0
n
Print a is Positive o
Stop
Department of Computer Science and 11
Engineering 2
Selection Structure-if-
then-else
Selection Structure-if-
then-else
If-Then-Else
Flowcha Pseudocod
rt e
Exam
ple Start
Read a,b
yes
If a>b
no
Print a is Greater
Print b is Greater
Stop
Biggest of 3 numbers
CASE
STRUCTURE
Case Structure
Syntax
Pseudocode Flowchart
Example: Finding the Grade
start
Read
m1,m2,m3
Avg=(m1+m2+m3
)/3
If Avg>=60 Print
First
Class
If Print
Avg>= Second
50 Class
Print
If Third
Avg>= Class
35
Fail
12
stoDpepartment of Computer Science and 0
Repetition
structure
Repetition
structure
Repetition
structure
• Repetition structures are used to repeat statements or
blocks of code.
• WHILE loop
• Do…WHILE loop
• For loop
While Loop and Do-
WHILE
WHILE Loop Do-
Loo
WHILE
Loop p
While
Loop
• A while loop is one of the most common types of loop.
• The main characteristic of a while loop is that it will repeat a set of
instructions based on a condition.
• As far as the loop returns a boolean value of TRUE, the code inside it will keep
repeating.
• We use this kind of loop when we don't know the exact number of times a
code needs to be executed.
pseudocod Flowcha
e rt
While
Loop
• A while loop is one of the most common types of loop.
• The main characteristic of a while loop is that it will repeat a set of
instructions based on a condition.
• As far as the loop returns a boolean value of TRUE, the code inside it will keep
repeating.
• We use this kind of loop when we don't know the exact number of times a
code needs to be executed.
pseudocod
e
Do-WHILE Loop
• In the Do-While structure, the statement given in the Do block are executed
atleast once and after that repeatedly executed until the given condition is true.
• In a do…while loop, the condition is always executed after the body of a loop. It
is also called an exit-controlled loop.
• At each instance of execution of block statements, the condition is checked.
• If the condition is true, then only block statement are executed; otherwise the
repetition structure is terminated.
pseudocode Flowchart
Do-WHILE Loop
• In the Do-While structure, the statement given in the Do block are executed
atleast once and after that repeatedly executed until the given condition is true.
• In a do…while loop, the condition is always executed after the body of a loop. It
is also called an exit-controlled loop.
• At each instance of execution of block statements, the condition is checked.
• If the condition is true, then only block statement are executed; otherwise the
repetition structure is terminated.
pseudocode Flowchart
For Loop
• In a for loop, the initial value is performed only once, then the condition tests
and compares the counter to a fixed value after each iteration, stopping the for
loop when false is returned.
For Loop
Problem Solving in
computer
Problems that can be solved through a computer may range in
size and complexity.
Flowchart Symbols
• There are 6 basic symbols commonly used in flowcharting of Programs:
1. Terminal
2. Process
3. input/output
4. Decision
5. Connector
6. Predefined Process.
Flowchart Symbols
Connector Symbol
Flowchart to calculate the sumof two
numbers.
Example:
Advantage and Disadvantage of Flowchart
Defining the pseudocode
• Step 1: start
• Step 2: Read the value r
• Step 3: calculate area of the circle
area=3.14*r*r
• step 4: Display area
• step 5: Stop
Algorithm for sequence
structure To display the
square root of a number
• Step 1: Start
• Step 2: Get a number from the user (n)
• Step 3: calculate square root of n, result=sqrt(n)
• Step 4: Display result
• step 5: stop
Algorithm for selection structure
To find whether the given number is even
or odd
• Step 1: Start
• Step 2: Read the value Number
• Step 3: Check: If Number%2 == 0 then goto step 4
else goto step 5
• Step 4: Display "N is an Even Number" goto step 6
• Step 5: Display "N is an Odd Number”
• Step 6: stop
Algorithm for selection structure
• Step1: Start
• Step2: Initialize i=1, sum=0
• Step3: Repeat Steps 4-5 until i<=100 is true, else
goto Step6
• Step4: Compute sum=sum+i
• Step5: Increment i=i+2
• Step6: Display the value of sum
• Step7:
Stop
Pseudocode for sequence
structure Display square
root of a number
BEGIN
READ: num
result=SQRT(num)
DISPLAY: result
END
Pseudocode for sequence
structure To find the area
of the circle
BEGIN
READ: radius
area=3.14*radius*radius
DISPLAY: area
END
Pseudocode for sequence
structure To find the
average of 3 numbers
BEGIN
READ: m1, m2, m3
average=(m1+m2+m3)/3
DISPLAY: average
END
Pseudocode for selection structure
To find the given number is positive
or negative
BEGIN
Get the value from the user (num)
if num>0
DISPLAY: “number
is positive”
ELSE
DISPLAY: “Number
is a negative”
END
END IF
Pseudocode for selection
structure To find the given
number is odd or even
BEGIN
READ: num
IF num%2==0
DISPLAY: “number is
even”
ELSE
DISPLAY: “Number is
END a odd”
END IF
Pseudocode for Repetition
structure Pseudocode for sum of first n
numbers (1+2+…+n)
BEGIN
Read the value (n)
Set sum=0, i=1
while i<=n
sum=sum+i
i=i+1
end while
Print sum
End
Pseudocode for Repetition structure
Pseudocode the sum of all odd numbers
from 1 to 100.
Begin
Set i=1, sum=0
While i<=100
sum=sum+i
i=i+2
End while
Print sum
End
Flowchart to Print your name 5 times
Print the natural number up to 10
Print the odd natural number up
to 100
Print the even natural number up to 100
Sum of the series of 1+2+3+---n
print the sum of all odd numbers from 1 to 100
Thank
You