Sie sind auf Seite 1von 47

Fundamentals of

Computing
(IT 101)
Dr. Manish Kumar Bajpai

Course Structure

IT101
100 marks

Theory

Lab

(Dr. M. K. Bajpai)

(Dr. Mamta Samal )

Quize1

Quiz2

Mid Sem

End Sem

Weekly lab

End Sem

(10 marks)

(10 marks)

(20 marks)

(30 marks)

(total 20 marks)

(10 marks)

Suggested reading
E Balaguruswamy, Programming in ANSI C, 6th edition.
(Available in Library)
Y Kanitkar, Let Us C, (Available in Library)
Kelley, Pohl, A Book on C, (Available in Library)

Important points
75% attendance is compulsory.
Have to score more than 30% not to get F Grade
Maintain the discipline in the class

Computer
A computer is an electronic machine that takes input from the
user, processes the given input and generates output in the
form of useful information.
The unique capabilities and characteristics of a computer:
Speed
Storage capacity
Accuracy
Reliability
Versatility
Diligence

Parts of computer

Computer

Input Devices
Output Devices
Central Processing
Unit
Storage Devices
Communication
Interface

Input Devices
Input devices are electromechanical devices that are used to
provide data to a computer for storing and further processing,
if necessary.
We can provide the input to a computer in two ways: (1)
Manually through devices such as keyboard and mouse; (2)
Directly from documents using devices such as scanners

Input Devices
Depending upon the type or method of input, the input device
may belong to one of the following categories:
(1) Keyboard
(2) Pointing devices
(3) Scanning devices
(4) Optical recognition devices
(5) Digital camera
(6) Voice recognition devices
(7) Media input devices

Central processing unit


The main operations of the CPU include four phases:
(1) Fetching instructions from the memory
(2) Decoding the instructions to decide what operations to be
performed
(3) Executing the instructions
(4) Storing the results back in the memory

Central processing unit


Arithmetic Unit is a part
of the CPU that
performs arithmetic
operations on the data.
The arithmetic
operations can be
addition, subtraction,
multiplication or
division.

CPU contains a few special


purpose, temporary storage
units known as registers.
They are high-speed
memory locations used for
holding instructions, data
and intermediate results
that are currently being
processed.

Logic Unit is a part of the


CPU that performs logical
operations on the data

Registers

Arithmetic
Unit

Logic Unit

Control
Unit
Control Unit is an important
component of CPU that
controls the flow of data
and information. It
maintains the sequence of
operations being performed
by the CPU.

Output Devices
Output devices receive the processed data (information) from
the CPU and present it to the user in a desired form. They act
as an interface between the computer and the user. The main
task of an output device is to convert the machine readable
information into human-readable from which may be in the
form of text, graphics, audio or video. Depending upon the
form of output required, the output device may belong to one
of the following categories:
Display monitors
Printers
Plotters
Voice output systems
Projectors

Storage
Cache memory
(registers
memory)
Primary memory
(RAM)
Secondary memory
(HDD)
Magnetic tape
(VCR tape, CD, DVD)

Storage Evaluation Criteria


Access Mode: random access mode, sequential access mode,
direct access mode
Access Time: the time taken by the processor in completing
the requests made by the user for performing the read and
write operations.
Storage Capacity: the size of the memory available for storing
the data, and measured in terms of bytes.
Storage Type: Temporary and permanent memory.
Cost: the cost of the storage device used in the computer
system for holding the data.

Communication Interface
The internal communication of a processor in the computer
system can be divided into two major categories:
(1) Processor to memory communication
(2) Processor to I/O devices communication

Problem Solving

Problem
Statement

Algorithm

Code

Testing

Feedback

Problem statement

You have to analyse the problem statement to know


Input required (What?)
How the input has to be processed (How?)
In what form you need output (which?)

Algorithm
Pseudo code: it is a language made for human only.
Neither a programming language
Nor a speaking language
Algorithm: step by step procedure to solve a problem
E.g.: making a cup of tea

How to write an Algorithm


Every algorithm has a unique name
Name is a function of I/p and O/P parameters
Every function used in an algorithm must be declared before it has
been used.
All the steps of an algorithm should be written between BEGIN and
END.
Keywords have to be written either in BOLD or in CAPATILE letters
E.g. for, while, do, if, else, if else, begin, end, end for, end if, end if
else, calculate/compute etc.
Every selection and loop constraints must be ended with END
One statement per line.
One process in each statement.
To get input, use GET
For output, use PUT/PRINT

Pseudo code
READ: used when algorithm is to receive i/p from a file or record
GET: used when algorithm is to receive i/p from i/p devices
(keyboard)
PRINT: used when o/p is to be sent to the printer
WRITE: used when o/p is to be written to a file
PUT, OUTPUT, DISPLAY: used when o/p is to be written to the screen
PROMPT: required before an i/p instruction GET. Causes the
message to be sent to the screen which requires user response
COMPUTE: used when have to perform a computation
CALCULATE: used for performing calculations based on a formulae
You can call a function and also return a value from a function
Definition of function must be provided at start of an algorithm

For initialisation of a variable use INITIALISE or SET keywords


Can also use = (equal operator) or -> (arrow operator)
= is used to assign a result of computation to a variable.
-> is used for general purpose assignment.
SAVE, STORE: is used to keep the value of a variable for further
use
Arithmetic Operators:
+
Addition
subtraction
*
multiplication
/
division
% modulo/reminder
^
exponentiation

Conditional statement
relational Operators: >, <, >=, <=, !=
Logical operator: !, &&, ||
IF
IF (amount < 1000)
Interest = 8
END IF

IF ELSE
IF (amount < 1000)
Interest = 8
ELSE
Interest = 6
END IF ELSE

Loop (Repetitive work)


FOR, WHILE: used to perform repetitive work
c <- 1, b <- 10
FOR i <- 0 to 10
c=c+b
END FOR
i<- 0, c <- 1, b <- 10
WHILE i < 10
c=c+b
END WHILE

Exercise
Problem Statement: Write an algorithm to find the tallest
person among three.
Analysis:
Input required: height of three person
Output : person identity who is tallest
Logic: ???????

Solution

Tallest(a, b, c, tallest_person)
INPUT: Height a, b, c
OUTPUT: tallest_person
BEGIN
GET a, b, c
IF (a > b)

IF (a > c)
Tallest_person = a
ELSE
tallest_person = c
END IF ELSE

ELSE

IF (b> c)
Tallest_person = b
ELSE
tallest_person = c
END IF ELSE

END IF ELSE
PUT tallest_person
END

C programming---basic
1 Introduction to C
2 C Fundamentals
3 Formatted Input/Output
4 Expression
5 Selection Statement
6 Loops
7 Basic Types
8 Arrays
9 Functions
10 Pointers
11 String Arrays
12 Structure & Union
13 File Management
14 Header File

Introduction to C
Intended use and underlying philosophy

1 C is a low-level language
---suitable language for systems programming

2 C is a small language
---relies on a library of standard functions

3 C is a permissive language
---it assumes that you know what youre doing, so it allows
you a wider degree of latitude than many languages. It
doesnt mandate the detailed error-checking found in
other language

Introduction to C
Strengths:
+ Efficiency: intended for applications where assembly
language had traditionally been used.
+ Portability: hasnt splintered into incompatible dialects;
small and easily written
+ Power: large collection of data types and operators
+ Flexibility: not only for system but also for embedded system
commercial data processing
+ Standard library
+ Integration with UNIX

Introduction to C
Weaknesses:
+ error-prone

+ difficult to understand

+ difficult to modify

Similarities of C to java
/* Comments */
Variable declarations
if / else statements
for loops
while loops
function definitions (like methods)
Main function starts program

Differences between C and java


C does not have objects
There are structures
C is a functional programming language
C allows pointer manipulation
Input / Output with C
Output with printf function
Input with scanf function

C Fundamentals
First program

Pre-processor
Directive
Header File

Main
functi
on

#include <stdio.h>
Incursion
main()
operator
{
printf(To C, or not to C: that is the question);
}
Semi
colon
Curly braces

In-build function

C Fundamentals

#include<stdio.h>
#define period 10
#define principal 5000.00
void main()
{

int year;
Float amount, value, inrate;
amount=principal;
inrate=0.11;
year=0;
while(year<=period)
{

}
}

printf("%2d %8.2f\n", year, amount);


value=amount+inrate*amount;
year=year+1;
amount=value;

C Fundamentals
Compiling and Linking
Preprocessing: the program is given to a preprocessor,
which obeys commands that begin with #(directives)
add things to the program and make modifications
Compiling: modified program compiler object
code
Linking: add library functions to yield a complete
executable program

C Fundamentals
Compilation
cc o nameofoutputfile program.c
gcc o nameofoutputfile program.c

Execution
./nameofoutputfile

C tokens

keywords

Identifiers

strings

constants

Special
symbols

operators

C Fundamentals
Keywords
auto
break
case
char
const
continue
default
do

double
else
enum
extern
float
for
goto
if

int
struct
long
switch
register typedef
return
union
short
unsigned
signed
void
sizeof
volatile
static
while

Variables or identifiers
It is the identity of a memory location where you want to store
something.
Its not memory address
Variable name must have any combination of following
Alphabets a, b, c,.,z
A,B, C, Z,
Underscore _
Numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
It must have some meaning
Some relation to its value
First character must be alphabet
Only 31 characters are significant
Keywords can not be used
Should not contain white spaces

Variable Type
Data type: provides the information about the values
stored in a particular variable
C has the following simple data types:
Data Type

Minimum
Size (bytes)

Lower bound

Upper bound

char

unsigned char 1

255

(short) int

-32768

+32767

(unsigned
short int)

65536

(long) int

-2

+2 -1

float

-3.2 10

+3.2 10

double

-1.7 10

+1.7 10

Constants
integer
Numeric
constants
real
Constants
Character
constants

Single character
constants
String constants

Strings
Collection of characters
Enclosed in

Special Symbols

[]
{}
()
;
#

Operators precedence

Expression Evaluation
a = b += c++ - d + --e / -f
a = b += (c++) - d + --e / -f
a = b += (c++) - d + (--e) / -f
a = b += (c++) - d + (--e) / (-f)
a = b += (c++) - d + ((--e) / (-f))
a = b += ((c++) d) + ((--e) / (-f))
a = b += (((c++) d) + ((--e) / (-f)))
a = (b += (((c++) d) + ((--e) / (-f))))
(a = (b += (((c++) d) + ((--e) / (-f)))))

Comments
// used for single line comments
/* .. */ used for multi line comments

printf (Control string, arguments); //used to print to console(screen)


scanf (Control string, Address); //used to take an input from
console(user).
example: printf(%c, a); scanf(%d, &a);
More format specifiers/ control string
%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%o The unsigned octal format specifier.
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier.
%% Outputs a percent sign.

C Course, Programming club, Fall


2008

Input / Output

45

Reading and Writing Integers


unsigned int u;
scanf(%u, &u);
printf(%u, u);
scanf(%o, &u);
printf(%o, u);
scanf(%x, &u);
printf(%x, u);
short int x;
scanf(%hd, &x);
printf(%hd, x);
long int x;
scanf(%ld, &x);
printf(%ld, x);

/* reads u in base 10 */
/* writes u in base 10 */
/* reads u in base 8 */
/* writes u in base 8 */
/* reads u in base 16 */
/* writes u in base 16*/

Examples
#include <stdio.h>
int main()
{
/* this program adds
two numbers */
int a = 4; //first number
int b = 5; //second number
int res = 0; //result
res = a + b;
return 0;
}

#include <stdio.h>
int main()
{
int first, second, add;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
divide = first / (float)second;
printf("Sum = %d\n",add);
printf("Division = %.2f\n",divide);
return 0;
}

Das könnte Ihnen auch gefallen